Skip to content

Instantly share code, notes, and snippets.

@sassembla
Created February 25, 2015 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sassembla/ae508e5d2ae7e0122cb2 to your computer and use it in GitHub Desktop.
Save sassembla/ae508e5d2ae7e0122cb2 to your computer and use it in GitHub Desktop.
LogWriter
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);
}
}
@sassembla
Copy link
Author

UnityでのログをPROJECT_FOLDER直下とか好きなところに書き出すやつ。
UnityEditorのログが見やすければ作らなかった。

@sassembla
Copy link
Author

ちなみにUnityDebug.Application.RegisterLogCallback は呼ぶたびに上書きされるので、他の人が使ってるかどうか確認してから使わないと戦争になって面白い。

@sassembla
Copy link
Author

コンパイルのWarn、Errorとかも書かれる。
ただしコンパイル状況とかは書かれない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment