-
Notifications
You must be signed in to change notification settings - Fork 10
/
Output.cs
132 lines (113 loc) · 2.49 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
using LibHac;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace nsZip
{
public class Output : IProgressReport
{
StreamWriter debug;
StreamWriter error;
long current;
long total;
long cooldown = 500;
DateTime cooldownStart;
public Output()
: this(1)
{
}
public Output(int log)
{
if(log != 0)
{
debug = new StreamWriter(File.Open("debug.log", FileMode.Append, FileAccess.Write, FileShare.ReadWrite), Encoding.UTF8);
error = new StreamWriter(File.Open("error.log", FileMode.Append, FileAccess.Write, FileShare.ReadWrite), Encoding.UTF8);
debug.WriteLine();
error.WriteLine();
}
}
public void Print(string text)
{
Console.Write(text);
}
public void Log(string text)
{
Console.Write(text);
if(debug != null)
{
debug.Write(text);
debug.Flush();
}
}
private void LogImportant(string text)
{
var time = DateTime.Now.ToString("[yyyy-MM-dd HH:mm:ss] ");
var timedText = time + text;
if(error != null)
{
error.Write(timedText);
error.Flush();
}
Log(timedText);
}
public void Warn(string text)
{
Console.ForegroundColor = ConsoleColor.Yellow;
LogImportant(text);
Console.ForegroundColor = ConsoleColor.White;
}
public void Event(string text)
{
Console.ForegroundColor = ConsoleColor.Cyan;
LogImportant(text);
Console.ForegroundColor = ConsoleColor.White;
}
public void Error(string text)
{
Console.ForegroundColor = ConsoleColor.Red;
LogImportant(text);
Console.ForegroundColor = ConsoleColor.White;
}
public void LogException(Exception ex)
{
Print("\r\n");
Error($"{ex.GetType()} StackTrace:\r\n"
+ $"{ex.StackTrace}\r\n\r\n"
+ $"{ex.GetType()}:\r\n"
+ $"{ex.Message}\r\n");
}
public void Report(long value)
{
current = value;
ReportAdd(0);
}
public void ReportAdd(long value)
{
current += value;
var timeNow = DateTime.Now;
var timeDiff = timeNow - cooldownStart;
if (timeDiff.Milliseconds > cooldown && current < total)
{
cooldownStart = timeNow;
var percentage = ((float)current / (float)total) * 100f;
Console.WriteLine(string.Format("{0:00.00}%", percentage));
}
}
public void SetTotal(long value)
{
current = 0;
total = value;
cooldownStart = DateTime.Now;
}
public void LogMessage(string message)
{
Console.Write($"{message}\r\n");
if(debug != null)
{
debug.Write($"{message}\r\n");
debug.Flush();
}
}
}
}