Created
April 7, 2016 03:13
-
-
Save sassembla/e44e937f2ae90b378b1177b3274d39d5 to your computer and use it in GitHub Desktop.
C#でいろんなPeerでログをファイルに吐いたり特定の関数で吐いたりするやつ
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 System; | |
using System.IO; | |
using System.Collections.Generic; | |
using System.Globalization; | |
namespace XrossPeerUtility { | |
public class XrossPeer { | |
static string logPath = string.Empty; | |
private static Action<string> logAction; | |
public static void SetupLog (string logOutputPath=null, Action<string> LogAct=null) { | |
if (!string.IsNullOrEmpty(logOutputPath)) logPath = logOutputPath; | |
logAction = WriteLog; | |
if (LogAct != null) logAction = LogAct; | |
} | |
public static void Log (string message) { | |
if (logAction == null) logAction = WriteLog; | |
logAction(message); | |
} | |
public static void LogWarning (string message) { | |
Log("WARNING:" + message); | |
} | |
public static void LogError (string message) { | |
Log("ERROR:" + message); | |
Log("stacktrace:" + Environment.StackTrace); | |
} | |
public static void WriteLog (string message) { | |
if (string.IsNullOrEmpty(logPath)) return; | |
// file write | |
using (var fs = new FileStream( | |
logPath, | |
FileMode.Append, | |
FileAccess.Write, | |
FileShare.ReadWrite) | |
) { | |
using (var sr = new StreamWriter(fs)) { | |
sr.WriteLine("log:" + message); | |
} | |
} | |
} | |
public static void LogDict (string message, Dictionary<string, string> dictObj, string peerId="undef") { | |
WriteLog("log:dict:" + message + " count:" + dictObj.Count); | |
foreach (var key in dictObj.Keys) { | |
WriteLog(" key:" + key + " val:" + dictObj[key]); | |
} | |
} | |
/** | |
assert which extends bool. | |
e.g. | |
Assert(false, "hereComes"); | |
will fail. | |
Assert(mustNotNull != null, "but null,,"); | |
*/ | |
public static void Assert (bool condition, string reason) { | |
if (condition) return; | |
OutputStackThenDown(reason); | |
} | |
/** | |
assert which extends string of date | |
e.g. | |
TimeAssert("2014/07/08 00:00:00", "hereComes"); | |
will fail after 2014/07/08 00:00:00. | |
TimeAssert("2014/07/07 00:00:00", "time's up!"); | |
*/ | |
public static void TimeAssert (string limitDate, string reason, int additionalSec = 0) { | |
DateTime parsedDate; | |
var fullhead_time_result = DateTime.TryParseExact(limitDate, "yyyy/MM/dd hh:mm:ss", null, DateTimeStyles.None, out parsedDate); | |
if (!fullhead_time_result) { | |
var no_head_time_result = DateTime.TryParseExact(limitDate, "yyyy/MM/dd h:mm:ss", null, DateTimeStyles.None, out parsedDate); | |
if (!no_head_time_result) { | |
WriteLog("assertion passed:" + reason + ", until " + limitDate); | |
return; | |
} | |
} | |
var now = DateTime.Now; | |
var diff = now - parsedDate; | |
var diffSec = Math.Floor(diff.TotalSeconds); | |
if (diffSec < additionalSec) { | |
return; | |
} | |
OutputStackThenDown(reason + " passed:" + (additionalSec - diffSec) + "sec"); | |
} | |
/** | |
utility. | |
*/ | |
private static void OutputStackThenDown (string reason) { | |
System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(true); | |
// at least 2 stack exists in st. 0 is "System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(true);", 1 is where the assertion faild. | |
var assertFaildPointDescription = st.GetFrame(2).ToString(); | |
// get specific data from stacktrace. | |
var descriptions = assertFaildPointDescription.Split(':'); | |
var fileName = descriptions[2].Split(' ')[1]; | |
var line = descriptions[3]; | |
LogError("assertion failed:" + fileName + ":" + line + ":" + reason); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage:
output log to file:
will write some lines to EXPECTED_PATH_OF_LOG_OUTPUT file.
output log with Specific method:
will use Debug.Log method to show log.
extra usage:
XrossPeer.LogDict(new Dictionary<string, string>{{"key", "val"}}); shows flatten log of dictionary.