-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogger.cs
122 lines (107 loc) · 3.24 KB
/
Logger.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
119
120
121
122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ModLib
{
public static class Logger
{
public static string OutputHeader;
public static string OutputFile
{
get
{
return _outputFile;
}
set
{
if (outputModFile != null)
{
outputModFile.Dispose();
}
_outputFile = value;
outputModFile = ModFile.Create(_outputFile);
if (OutputHeader != null)
{
outputModFile.WriteString("Log file for " + OutputHeader + ":\n");
}
else
{
outputModFile.WriteString("Log file:\n");
}
}
}
private static readonly object ConsoleWriterLock = new object();
private static string _outputFile;
private static ModFile outputModFile;
public static void SendToFile(string value)
{
if (outputModFile != null)
{
outputModFile.WriteString(DateTime.Now.ToLongTimeString() + " ");
outputModFile.WriteString(value + '\n');
}
}
public static void Log(string value, params object[] args)
{
if (args.Length > 0)
{
value = string.Format(value, args);
}
lock (ConsoleWriterLock)
{
Console.WriteLine(value);
SendToFile(value);
}
}
public static void Warn(string value, params object[] args)
{
Log(new LogSeg("Warning: ", ConsoleColor.Yellow), new LogSeg(String.Format(value, args)));
}
public static void Error(string value, params object[] args)
{
Log(new LogSeg("Error: ", ConsoleColor.Red), new LogSeg(String.Format(value, args)));
}
private static StringBuilder SegBuilder(params LogSeg[] args)
{
StringBuilder str = new StringBuilder();
foreach (LogSeg arg in args)
{
str.Append(arg.value);
Console.ForegroundColor = arg.color;
Console.Write(arg.value);
}
Console.ForegroundColor = ConsoleColor.White;
return str;
}
public static void Log(params LogSeg[] args)
{
lock (ConsoleWriterLock)
{
var str = SegBuilder(args);
Console.Write('\n');
SendToFile(str.ToString());
}
}
public static void ToConsole(string value, params object[] args)
{
if (args.Length > 0)
{
value = string.Format(value, args);
}
lock (ConsoleWriterLock)
{
Console.WriteLine(value);
}
}
public static void ToConsole(params LogSeg[] args)
{
lock (ConsoleWriterLock)
{
SegBuilder(args);
Console.Write('\n');
}
}
}
}