Skip to content

Commit

Permalink
第二章:Hello World,提交打印日志到文件的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiang Yin committed Dec 9, 2019
1 parent 178fc2e commit deb578b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Assets/GameMain/Scripts/Utility.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions Assets/GameMain/Scripts/Utility/FileLogHelper.cs
@@ -0,0 +1,51 @@
using GameFramework;
using System;
using System.IO;
using System.Text;
using UnityEngine;
using UnityGameFramework.Runtime;

namespace Tutorial
{
/// <summary>
/// 带有文件打印的日志辅助器。
/// </summary>
internal class FileLogHelper : DefaultLogHelper
{
private readonly string CurrentLogPath = Utility.Path.GetRegularPath(Path.Combine(Application.persistentDataPath, "current.log"));
private readonly string PreviousLogPath = Utility.Path.GetRegularPath(Path.Combine(Application.persistentDataPath, "previous.log"));

public FileLogHelper()
{
Application.logMessageReceived += OnLogMessageReceived;

try
{
if (File.Exists(PreviousLogPath))
{
File.Delete(PreviousLogPath);
}

if (File.Exists(CurrentLogPath))
{
File.Move(CurrentLogPath, PreviousLogPath);
}
}
catch
{
}
}

private void OnLogMessageReceived(string logMessage, string stackTrace, LogType logType)
{
string log = Utility.Text.Format("[{0}][{1}] {2}{4}{3}{4}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), logType.ToString(), logMessage ?? "<Empty Message>", stackTrace ?? "<Empty StackTrace>", Environment.NewLine);
try
{
File.AppendAllText(CurrentLogPath, log, Encoding.UTF8);
}
catch
{
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/GameMain/Scripts/Utility/FileLogHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Tutorial Launcher.unity
Expand Up @@ -238,7 +238,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 11499388, guid: adb3eb1c35fcff14f89fba7b05c9d71c, type: 3}
propertyPath: m_LogHelperTypeName
value: UnityGameFramework.Runtime.DefaultLogHelper
value: Tutorial.FileLogHelper
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: adb3eb1c35fcff14f89fba7b05c9d71c, type: 3}

0 comments on commit deb578b

Please sign in to comment.