-
Notifications
You must be signed in to change notification settings - Fork 0
/
Output.cs
222 lines (179 loc) · 7.12 KB
/
Output.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using Reality.Config;
using System.Text;
namespace Reality
{
public enum OutputLevel
{
CriticalError = 3,
Warning = 2,
Notification = 1,
Informational = 0,
DebugInformation = -1
}
public static class Output
{
private static bool mEnableLogging;
private static string mLogFilePath;
private static OutputLevel mVerbosityLevel;
private static object mWritebackSyncRoot;
public static void InitializeStream(bool EnableLogging, OutputLevel VerbosityLevel)
{
mEnableLogging = EnableLogging;
mVerbosityLevel = VerbosityLevel;
mWritebackSyncRoot = new object();
if (EnableLogging)
{
DateTime Now = DateTime.Now;
string LogDirectory = Environment.CurrentDirectory + Constants.LogFileDirectory + "\\";
mLogFilePath = LogDirectory + Now.Day + Now.Month + Now.Year + Now.Hour + Now.Minute + Now.Second + ".log";
try
{
if (!Directory.Exists(LogDirectory))
{
Directory.CreateDirectory(LogDirectory);
}
File.WriteAllText(mLogFilePath, ComposeDefaultLogHeader(), Constants.DefaultEncoding);
}
catch (Exception)
{
mEnableLogging = false;
}
}
ClearStream();
WriteBanner();
}
public static void ClearStream()
{
Console.Clear();
Console.Title = Constants.ConsoleTitle;
Console.WindowWidth = Constants.ConsoleWindowWidth;
Console.WindowHeight = Constants.ConsoleWindowHeight;
ResetColorScheme();
}
private static void ResetColorScheme()
{
ApplyColorScheme(ConsoleColor.Gray, ConsoleColor.Black);
}
private static void ApplyColorScheme(ConsoleColor ForegroundColor)
{
ApplyColorScheme(ForegroundColor, ConsoleColor.Black);
}
private static void ApplyColorScheme(ConsoleColor ForegroundColor, ConsoleColor BackgroundColor)
{
Console.ForegroundColor = ForegroundColor;
Console.BackgroundColor = BackgroundColor;
}
private static void SetColorSchemeForSeverity(OutputLevel SeverityLevel)
{
switch (SeverityLevel)
{
case OutputLevel.CriticalError:
ApplyColorScheme(ConsoleColor.Red);
break;
case OutputLevel.Warning:
ApplyColorScheme(ConsoleColor.Yellow);
break;
case OutputLevel.Notification:
ApplyColorScheme(ConsoleColor.Green);
break;
case OutputLevel.DebugInformation:
ApplyColorScheme(ConsoleColor.DarkGray);
break;
case OutputLevel.Informational:
default:
ResetColorScheme();
break;
}
}
public static void WriteBanner()
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.White;
List<string> Lines = new List<string>();
Lines.Add(",------. ,--.,--. ,--. ");
Lines.Add("| .--. ' ,---. ,--,--.| |`--',-' '-.,--. ,--. " + " Reality server");
Lines.Add("| '--'.'| .-. :' ,-. || |,--.'-. .-' \\ ' / " + " Development version");
Lines.Add("| |\\ \\ \\ --.\\ '-' || || | | | \\ ' "+ " Version G" + Assembly.GetExecutingAssembly().GetName().Version.ToString() + "-RP");
Lines.Add("`--' '--' `----' `--`--'`--'`--' `--' .-' / " + " THIS PROGRAM IS FREE!");
Lines.Add(" `---' ");
foreach (string Line in Lines)
{
int Spacing = Console.WindowWidth - Line.Length - 1;
string Ln = Line;
for (int s = 0; s < Spacing; s++)
{
Ln += ' ';
}
Console.WriteLine(Ln);
}
Console.WriteLine();
ApplyColorScheme(ConsoleColor.DarkGray);
Console.WriteLine("RealityRP, Copyright (C) 2012 Divide.\n");
Console.WriteLine("This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you");
Console.WriteLine("are welcome to redistribute it - You must include me in the credits (Divide @ RaGEZONE)");
Console.WriteLine();
ApplyColorScheme(ConsoleColor.DarkGray);
if (mVerbosityLevel < 0)
{
Console.WriteLine("* WARNING: Verbose logging is currently active. This may cause a decrease in");
Console.WriteLine("* performance. It is recommended you set logging verbosity to 0 (Info) or higher.");
Console.WriteLine();
}
ResetColorScheme();
}
private static string ComposeDefaultLogHeader()
{
StringBuilder Builder = new StringBuilder("## Reality" + Constants.LineBreakChar);
Builder.Append("## Server output log file" + Constants.LineBreakChar);
Builder.Append("## " + DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString() + Constants.LineBreakChar);
Builder.Append(Constants.LineBreakChar);
return Builder.ToString();
}
private static void WriteLogIfNeeded(string Line)
{
if (mEnableLogging)
{
lock (mWritebackSyncRoot)
{
File.AppendAllText(mLogFilePath, FormatTimestamp() + Line + Constants.LineBreakChar,
Constants.DefaultEncoding);
}
}
}
public static void WriteLine()
{
if (mVerbosityLevel <= OutputLevel.Notification)
{
Console.WriteLine();
WriteLogIfNeeded(Constants.LineBreakChar.ToString());
}
}
public static void WriteLine(string Line)
{
WriteLine(Line, OutputLevel.Informational);
}
public static void WriteLine(string Line, OutputLevel Level)
{
if (mVerbosityLevel <= Level)
{
Console.Write(FormatTimestamp());
SetColorSchemeForSeverity(Level);
Console.WriteLine(Line);
ResetColorScheme();
WriteLogIfNeeded(Line);
}
}
private static string FormatTimestamp()
{
return "[" + DateTime.Now.ToShortTimeString() + "] ";
}
public static void SetVerbosityLevel(OutputLevel OutputLevel)
{
mVerbosityLevel = OutputLevel;
}
}
}