1个基类,3个继承类,一个管理类。3个继承类在打印时,打印的是相同的log,只是打印的地方不同,也就是说只需要一个就可以了。
代码如下:
using UnityEngine;
using System.Collections;
using System;
using System.IO;
using System.Collections.Generic;
namespace MyGameNamespace
{
abstract class MyGameLog
{
public enum MyGameLogLevel
{
Info = 0, Warning, Error
}
static string[] LevelString = { "INFO", "WARNING", "ERROR" };//公有的信息静态处理
protected abstract void Log(string strFormat,params object[] args);
protected void Log(MyGameLogLevel level,string strFormat,params object[] args)
{
string strLog = string.Format("{0}:{1}:{2}",DateTime.Now.ToString(),LevelString[(int)level],strFormat);
Log(strLog,args);
}
public void LogInfo(string strFormat,params object[] args)
{
Log(MyGameLogLevel.Info,strFormat,args);
}
public void LogWarn(string strFormat,params object[] args)
{
Log(MyGameLogLevel.Warning,strFormat,args);
}
public void LogError(string strFormat,params object[] args)
{
Log(MyGameLogLevel.Error,strFormat,args);
}
public abstract bool Init();
public abstract void Release();
}
class MyDefaultLog : MyGameLog
{
protected override void Log(string strFormat, params object[] args)
{
Debug.LogFormat(strFormat,args);
}
public override bool Init()
{
return true;
}
public override void Release()
{
return;
}
}
class MyFileLog : MyGameLog
{
private const string fileName = "mygame";
private string logPath = Application.dataPath + "/../";
private string newFileTime = DateTime.Now.ToString("yyyyMMddHHmmss");
private StreamWriter writer = null;
protected override void Log(string strFormat, params object[] args)
{
string strLog = string.Format(strFormat + "\r\n", args);
writer.Write(strLog);
writer.Flush();
}
public override bool Init()
{
try
{
writer = File.CreateText(fileName+newFileTime+".log");
}
catch (Exception ex)
{
writer = null;
LogError("create log file fail : "+ex);
return false;
}
if (writer == null) return false;
DeleteOlderFile();
LogInfo("xxxxxxxxxxxxx log init xxxxxxxxxxxxxx");
return true;
}
public override void Release()
{
if(writer!=null)
{
writer.Close();
}
DeleteOlderFile();
}
private void DeleteOlderFile()
{
string[] filePaths = Directory.GetFiles(logPath,"mygame*.log",SearchOption.TopDirectoryOnly);
if (filePaths.Length < 3) return;
List<long> timelist = new List<long>();
int preIndex = 0;
int lastIndex = 0;
foreach (var path in filePaths)
{
preIndex = path.LastIndexOf("mygame");//拿到g的位置
lastIndex = path.LastIndexOf(".log");
if(preIndex==-1||lastIndex==-1||lastIndex-preIndex==4)
{
continue;
}
long logtime = long.Parse(path.Substring(preIndex + 6, lastIndex - preIndex - 6));//拿掉了.log
timelist.Add(logtime);
}
timelist.Sort();
for (int i = 0; i < filePaths.Length-3; i++)
{
try
{
File.Delete(filePaths[i]);
}
catch (Exception ex) { LogError("delect log file is error : " + ex); }
}
}
}
class MyUIlog : MyGameLog
{
public override bool Init()
{
return true;
}
public override void Release()
{
}
protected override void Log(string strFormat, params object[] args)
{
//uiLogDebug
}
}
public class MyGameLogManager
{
private readonly static MyGameLogManager m_Instance = new MyGameLogManager();
public static MyGameLogManager Instance { get { return m_Instance; } }
private MyGameLogManager() { }
List<MyGameLog> m_logs = new List<MyGameLog>();//3种不同的log写的内容是一样的,只是写的位置不同而已
public bool Init()
{
#if DEBUG
MyDefaultLog log = new MyDefaultLog();
if (log.Init() == false) return false;
m_logs.Add(log);
#endif
MyFileLog fileLog = new MyFileLog();
if (fileLog.Init() == false) return false;
m_logs.Add(fileLog);
return true;
}
private MyUIlog uiLog = null;//因为是单独初始化,所以可以多次初始化,所以写成全局的。
public bool InitUILog()
{
if (uiLog == null)
{
uiLog = new MyUIlog();
if (uiLog.Init() == false) return false;
m_logs.Add(uiLog);
}
return true;
}
public void release()
{
for (int i = 0; i < m_logs.Count; i++)
{
m_logs[i].Release();
}
m_logs.Clear();
}
public void LogInfo(string strformat,params object[] args)
{
for (int i = 0; i < m_logs.Count; i++)
{
m_logs[i].LogInfo(strformat,args);
}
}
public void LogWarn(string strformat,params object[] args)
{
for (int i = 0; i < m_logs.Count; i++)
{
m_logs[i].LogWarn(strformat, args);
}
}
public void LogError(string strformat, params object[] args)
{
for (int i = 0; i < m_logs.Count; i++)
{
m_logs[i].LogError(strformat, args);
}
}
}
}