purpose
Automatic log file writing function, but can not affect the game performance, so use multithreading to write log.
The source code
/// <summary>
///Log file module, using multithreading to write log files
/// </summary>
public class LogFileModule
{
private static LogFileModule sLogFileModule;
/// <summary>
///Enabling Log Writing
/// </summary>
public static void Open()
{
if (sLogFileModule == null)
{
sLogFileModule = newLogFileModule(); }}public static void Close()
{
if(sLogFileModule ! =null)
{
sLogFileModule.Dispose();
sLogFileModule = null; }}#regionprivate
private class LogData
{
public string log { get; set; }
public string trace { get; set; }
public LogType level { get; set; }}private StreamWriter m_StreamWriter;
private readonly ManualResetEvent m_ManualResetEvent;
private readonly ConcurrentQueue<LogData> m_ConcurrentQueue; // Secure queue
private bool m_ThreadRunning;
private LogFileModule()
{
var logFileName = string.Format("zysy-logmsg-{0}.log", DateTime.Now.ToString("yyyyMMddHHmmss"));
var logFilePath = Path.Combine(Application.persistentDataPath, logFileName);
m_StreamWriter = new StreamWriter(logFilePath);
m_ManualResetEvent = new ManualResetEvent(false);
m_ConcurrentQueue = new ConcurrentQueue<LogData>();
m_ThreadRunning = true;
Application.logMessageReceivedThreaded += OnLogMessageReceivedThreaded;
var fileThread = new Thread(FileLogThread);
fileThread.Start();
}
private void Dispose()
{
Application.logMessageReceivedThreaded -= OnLogMessageReceivedThreaded;
m_ThreadRunning = false;
m_ManualResetEvent.Set();
m_StreamWriter.Close();
m_StreamWriter = null;
}
private void OnLogMessageReceivedThreaded(string logString, string stackTrace, LogType type)
{
m_ConcurrentQueue.Enqueue(new LogData() { log = logString, trace = stackTrace, level = type });
m_ManualResetEvent.Set();
}
private void FileLogThread()
{
while (m_ThreadRunning)
{
m_ManualResetEvent.WaitOne();
if (m_StreamWriter == null)
{
break;
}
LogData msg;
while (m_ConcurrentQueue.Count > 0 && m_ConcurrentQueue.TryDequeue(out msg))
{
if (msg.level == LogType.Log)
{
m_StreamWriter.Write("LogStart1----");
m_StreamWriter.Write(msg.log);
}
else if (msg.level == LogType.Warning)
{
m_StreamWriter.Write("LogStart2----");
m_StreamWriter.Write(msg.log);
}
else
{
m_StreamWriter.Write("LogStart3----");
m_StreamWriter.Write(msg.log);
m_StreamWriter.Write('\n');
m_StreamWriter.Write(msg.trace);
}
m_StreamWriter.Write("\r\n");
}
m_StreamWriter.Flush();
m_ManualResetEvent.Reset();
Thread.Sleep(1); }}#endregion
}
Copy the code
The viewer
Use”The Unity Console Console plug-in enhances the displayThis plugin allows you to import logs to view: