4 using System.Diagnostics;
6 using System.Reflection;
7 using System.Runtime.InteropServices;
8 #if SILICONSTUDIO_PLATFORM_ANDROID
11 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
12 using Microsoft.Win32.SafeHandles;
15 namespace SiliconStudio.Core.Diagnostics
22 private bool isConsoleActive;
41 if (!Debugger.IsAttached &&
49 if (!Debugger.IsAttached)
54 #if SILICONSTUDIO_PLATFORM_ANDROID
55 const string appliName =
"Paradox";
56 var exceptionMsg = GetExceptionText(logMessage);
57 var messageText = GetDefaultText(logMessage);
58 if (!
string.IsNullOrEmpty(exceptionMsg))
59 messageText += exceptionMsg;
62 switch (logMessage.
Type)
64 case LogMessageType.Debug:
65 Log.Debug(appliName, messageText);
67 case LogMessageType.Verbose:
68 Log.Verbose(appliName, messageText);
70 case LogMessageType.Info:
71 Log.Info(appliName, messageText);
73 case LogMessageType.Warning:
74 Log.Warn(appliName, messageText);
76 case LogMessageType.Error:
77 case LogMessageType.Fatal:
78 Log.Error(appliName, messageText);
82 #else // SILICONSTUDIO_PLATFORM_ANDROID
84 var exceptionMsg = GetExceptionText(logMessage);
86 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
91 switch (logMessage.
Type)
93 case LogMessageType.Debug:
94 Console.ForegroundColor = ConsoleColor.DarkGray;
96 case LogMessageType.Verbose:
97 Console.ForegroundColor = ConsoleColor.Gray;
99 case LogMessageType.Info:
100 Console.ForegroundColor = ConsoleColor.Green;
102 case LogMessageType.Warning:
103 Console.ForegroundColor = ConsoleColor.Yellow;
105 case LogMessageType.Error:
106 case LogMessageType.Fatal:
107 Console.ForegroundColor = ConsoleColor.Red;
112 bool useDebugLogger =
true;
114 #if SILICONSTUDIO_PLATFORM_MONO_MOBILE || SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
116 useDebugLogger = System.Diagnostics.Debugger.IsAttached;
119 Console.WriteLine(GetDefaultText(logMessage));
120 if (!
string.IsNullOrEmpty(exceptionMsg))
122 Console.WriteLine(exceptionMsg);
129 System.Diagnostics.Debug.WriteLine(GetDefaultText(logMessage));
130 if (!
string.IsNullOrEmpty(exceptionMsg))
132 System.Diagnostics.Debug.WriteLine(logMessage);
136 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
139 Console.ForegroundColor = initialColor;
141 #endif // !SILICONSTUDIO_PLATFORM_ANDROID
144 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
148 private void EnsureConsole()
150 if (Debugger.IsAttached || isConsoleActive || Environment.OSVersion.Platform != PlatformID.Win32NT)
156 var attachedToConsole = AttachConsole(-1);
157 if (!attachedToConsole)
163 isConsoleActive =
true;
166 public static void ShowConsole()
168 var handle = GetConsoleWindow();
170 var outputRedirected = IsHandleRedirected((IntPtr)StdOutConsoleHandle);
172 if (handle == IntPtr.Zero || outputRedirected)
174 Stream originalStream = null;
175 if (outputRedirected)
177 originalStream = Console.OpenStandardOutput();
182 Stream outputStream = Console.OpenStandardOutput();
183 if (originalStream != null)
185 outputStream =
new DualStream(originalStream, outputStream);
188 TextWriter writer =
new StreamWriter(outputStream) { AutoFlush =
true };
189 Console.SetOut(writer);
193 const int SW_SHOW = 5;
194 ShowWindow(handle, SW_SHOW);
198 private class DualStream :
Stream
205 this.stream1 = stream1;
206 this.stream2 = stream2;
209 public override void Flush()
215 public override long Seek(
long offset, SeekOrigin origin)
217 throw new NotImplementedException();
220 public override void SetLength(
long value)
222 throw new NotImplementedException();
225 public override int Read(byte[] buffer,
int offset,
int count)
227 throw new NotImplementedException();
230 public override void Write(byte[] buffer,
int offset,
int count)
232 stream1.Write(buffer, offset,
count);
233 stream2.Write(buffer, offset,
count);
236 public override bool CanRead
244 public override bool CanSeek
252 public override bool CanWrite
260 public override long Length
264 throw new NotImplementedException();
268 public override long Position {
get; set; }
271 public static void HideConsole()
273 var handle = GetConsoleWindow();
274 const int SW_HIDE = 0;
275 ShowWindow(handle, SW_HIDE);
278 private const int StdOutConsoleHandle = -11;
280 [DllImport(
"kernel32", SetLastError =
true)]
281 private static extern bool AttachConsole(
int dwProcessId);
283 [DllImport(
"kernel32.dll", SetLastError =
true)]
284 private static extern bool AllocConsole();
286 [DllImport(
"kernel32.dll")]
287 private static extern IntPtr GetConsoleWindow();
289 [DllImport(
"user32.dll")]
290 private static extern bool ShowWindow(IntPtr hWnd,
int nCmdShow);
292 [DllImport(
"kernel32.dll")]
293 private static extern IntPtr GetStdHandle(uint nStdHandle);
295 [DllImport(
"kernel32.dll")]
296 private static extern void SetStdHandle(uint nStdHandle, IntPtr handle);
298 [DllImport(
"kernel32.dll")]
299 private static extern int GetFileType(SafeFileHandle handle);
301 [DllImport(
"kernel32.dll", SetLastError =
true)]
302 private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out
int mode);
304 private static bool IsHandleRedirected(IntPtr ioHandle)
306 if ((GetFileType(
new SafeFileHandle(ioHandle,
false)) & 2) != 2)
313 return !GetConsoleMode(ioHandle, out mode);
318 private void EnsureConsole()
ConsoleLogMode
Defines how the console is opened.
LogMessageType
Type of a LogMessage.
A base class to implement a log listener
override void OnLog(ILogMessage logMessage)
Called when a log occurred.
The base interface for log messages used by the logging infrastructure.
LogMessageType Type
Gets or sets the type of this message.
ConsoleColor
Colors used by ConsoleProgram.Color
A LogListener implementation redirecting its output to the default OS console. If console is not supp...