Created
February 25, 2015 14:35
-
-
Save sassembla/ae508e5d2ae7e0122cb2 to your computer and use it in GitHub Desktop.
LogWriter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityDebug = UnityEngine; | |
using System.IO; | |
using System.Text; | |
using System.Diagnostics; | |
public class LogWriter { | |
private static string targetPath = string.Empty; | |
public static void StartTransLogging (string path) { | |
targetPath = path; | |
UnityDebug.Application.RegisterLogCallback(TransLog); | |
UnityDebug.Debug.Log("debugging translogger is ON. should cut off when publish."); | |
} | |
public static void Log (string message) { | |
if (message == null) { | |
message = "NULL!"; | |
} | |
// file write | |
using (var fs = new FileStream( | |
Path.Combine(targetPath, "log.txt"), | |
FileMode.Append, | |
FileAccess.Write, | |
FileShare.ReadWrite) | |
) { | |
using (var sr = new StreamWriter(fs)) { | |
sr.WriteLine("message:" + message); | |
} | |
} | |
} | |
public static void Stack (string location) { | |
Log("stack:location:" + location); | |
var st = new StackTrace(true); | |
for (int i =0; i< st.FrameCount; i++ ) { | |
StackFrame sf = st.GetFrame(i); | |
Log("stack:method:" + sf.GetMethod()); | |
Log("stack:line:" + sf.GetFileLineNumber()); | |
} | |
} | |
private static void TransLog (string logString, string stackTrace, UnityDebug.LogType type) { | |
if (string.IsNullOrEmpty(targetPath)) { | |
UnityEngine.Debug.LogError("targetPath is empty or null."); | |
return; | |
} | |
Log("trans:" + logString); | |
} | |
} |
ちなみにUnityDebug.Application.RegisterLogCallback は呼ぶたびに上書きされるので、他の人が使ってるかどうか確認してから使わないと戦争になって面白い。
コンパイルのWarn、Errorとかも書かれる。
ただしコンパイル状況とかは書かれない。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
UnityでのログをPROJECT_FOLDER直下とか好きなところに書き出すやつ。
UnityEditorのログが見やすければ作らなかった。