forked from dotnet/crank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.cs
118 lines (95 loc) · 3.65 KB
/
Log.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
namespace Microsoft.Crank.Controller
{
public class Log
{
public static bool IsVerbose { get; set; }
public static bool IsQuiet { get; set; }
public static void Quiet(string message)
{
Console.WriteLine(message);
}
public static void WriteError(string message, bool notime = false)
{
Write(message, notime, ConsoleColor.Red);
}
public static void WriteWarning(string message, bool notime = false)
{
Write(message, notime, ConsoleColor.DarkYellow);
}
public static void Write(string message, bool notime = false, ConsoleColor color = ConsoleColor.White)
{
if (color != ConsoleColor.White)
{
Console.ForegroundColor = color;
}
if (!IsQuiet)
{
var time = DateTime.Now.ToString("hh:mm:ss.fff");
if (notime)
{
Console.WriteLine(message);
}
else
{
Console.WriteLine($"[{time}] {message}");
}
}
Console.ResetColor();
}
public static void Verbose(string message)
{
if (IsVerbose && !IsQuiet)
{
Write(message);
}
}
public static void DisplayOutput(string content)
{
if (String.IsNullOrEmpty(content))
{
return;
}
#region Switching console mode on Windows to preserve colors for stdout
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
var iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (GetConsoleMode(iStdOut, out uint outConsoleMode))
{
var tempConsoleMode = outConsoleMode;
outConsoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | DISABLE_NEWLINE_AUTO_RETURN;
if (!SetConsoleMode(iStdOut, outConsoleMode))
{
Console.WriteLine($"failed to set output console mode, error code: {GetLastError()}");
}
if (!SetConsoleMode(iStdOut, tempConsoleMode))
{
Console.WriteLine($"failed to restore console mode, error code: {GetLastError()}");
}
}
}
#endregion
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Convert LF
content = content?.Replace("\n", Environment.NewLine) ?? "";
}
Log.Write(content.Trim(), notime: true);
}
private const int STD_OUTPUT_HANDLE = -11;
private const uint ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x0004;
private const uint DISABLE_NEWLINE_AUTO_RETURN = 0x0008;
[DllImport("kernel32.dll")]
private static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
private static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
public static extern uint GetLastError();
}
}