Created
April 17, 2015 20:14
-
-
Save mohemohe/93c91a806fe564efe77d to your computer and use it in GitHub Desktop.
WPFでコンソールにログを表示したいときとかに
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.Diagnostics; | |
using System.Runtime.InteropServices; | |
namespace GomokuCharhan.Helpers | |
{ | |
static class DebugHelper | |
{ | |
[Conditional("DEBUG")] | |
[DllImport("Kernel32")] | |
private static extern void AllocConsole(); | |
[Conditional("DEBUG")] | |
[DllImport("Kernel32")] | |
private static extern void FreeConsole(); | |
private static Stopwatch _stopwatch; | |
[Conditional("DEBUG")] | |
public static void Initialize() | |
{ | |
_stopwatch = new Stopwatch(); | |
AllocConsole(); | |
WriteLine("start tracing"); | |
_stopwatch.Start(); | |
} | |
[Conditional("DEBUG")] | |
public static void Dispose() | |
{ | |
FreeConsole(); | |
} | |
[Conditional("DEBUG")] | |
private static void WriteTimestamp() | |
{ | |
Console.Write(@"[" + _stopwatch.Elapsed.ToString(@"hh\:mm\:ss\.fffffff") + @"] "); | |
} | |
[Conditional("DEBUG")] | |
public static void Write(dynamic value) | |
{ | |
WriteTimestamp(); | |
Console.Write(value); | |
} | |
[Conditional("DEBUG")] | |
public static void Write(string format, object arg0) | |
{ | |
WriteTimestamp(); | |
Console.Write(format, arg0); | |
} | |
[Conditional("DEBUG")] | |
public static void Write(string format, object arg0, object arg1) | |
{ | |
WriteTimestamp(); | |
Console.Write(format, arg0, arg1); | |
} | |
[Conditional("DEBUG")] | |
public static void Write(string format, object arg0, object arg1, object arg2) | |
{ | |
WriteTimestamp(); | |
Console.Write(format, arg0, arg1, arg2); | |
} | |
[Conditional("DEBUG")] | |
public static void Write(string format, object arg0, object arg1, object arg2, object arg3, __arglist) | |
{ | |
var argList = new ArgIterator(__arglist); | |
object[] args = new object[argList.GetRemainingCount() + 4]; | |
args[0] = arg0; | |
args[1] = arg1; | |
args[2] = arg2; | |
args[3] = arg3; | |
for (var i = 4; argList.GetRemainingCount() > 0; i++) | |
{ | |
var arg = argList.GetNextArg(); | |
args[i] = TypedReference.ToObject(arg); | |
} | |
Console.Write(format, args); | |
} | |
[Conditional("DEBUG")] | |
public static void WriteLine(dynamic value) | |
{ | |
WriteTimestamp(); | |
Console.WriteLine(value); | |
} | |
[Conditional("DEBUG")] | |
public static void WriteLine(string format, object arg0) | |
{ | |
WriteTimestamp(); | |
Console.WriteLine(format, arg0); | |
} | |
[Conditional("DEBUG")] | |
public static void WriteLine(string format, object arg0, object arg1) | |
{ | |
WriteTimestamp(); | |
Console.WriteLine(format, arg0, arg1); | |
} | |
[Conditional("DEBUG")] | |
public static void WriteLine(string format, object arg0, object arg1, object arg2) | |
{ | |
WriteTimestamp(); | |
Console.WriteLine(format, arg0, arg1, arg2); | |
} | |
[Conditional("DEBUG")] | |
public static void WriteLine(string format, object arg0, object arg1, object arg2, object arg3, __arglist) | |
{ | |
var argList = new ArgIterator(__arglist); | |
object[] args = new object[argList.GetRemainingCount() + 4]; | |
args[0] = arg0; | |
args[1] = arg1; | |
args[2] = arg2; | |
args[3] = arg3; | |
for (var i = 4; argList.GetRemainingCount() > 0; i++) | |
{ | |
var arg = argList.GetNextArg(); | |
args[i] = TypedReference.ToObject(arg); | |
} | |
Console.WriteLine(format, args); | |
} | |
[Conditional("DEBUG")] | |
public static void SetForegroundColor(ConsoleColor color) | |
{ | |
WriteTimestamp(); | |
Console.ForegroundColor = color; | |
} | |
[Conditional("DEBUG")] | |
public static void ResetConsoleColor() | |
{ | |
WriteTimestamp(); | |
Console.ResetColor(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment