-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cs
More file actions
175 lines (154 loc) · 5.26 KB
/
Log.cs
File metadata and controls
175 lines (154 loc) · 5.26 KB
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
namespace SimpleBackup
{
/// <summary>
/// Used for logging text to the console and external log file, as well as reading the external log file. Also holds any accumulated errors until cleared
/// </summary>
public static class Log
{
private static readonly string fileName = "log.txt";
private static readonly int maxLogFileSizeKB = 30;
public static event EventHandler OnLogUpdated;
private static List<string> sessionErrors = new List<string>();
private static readonly int maxSessionErrors = 100;
public static void WriteLine(string text, bool isError = false, bool addTimeStamp = true)
{
string directory = GetDirectory();
if (directory != null)
{
try
{
List<string> lines = new List<string>();
string fullPath = Path.Combine(directory, fileName);
if (isError)
{
if (sessionErrors.Count <= maxSessionErrors)
{
sessionErrors.Add("ERROR: " + text);
}
else
{
WriteLine("Reached max amount of recorded errors, no new errors will be saved until cleared");
}
}
if (addTimeStamp)
{
text = "[" + DateTime.Now.ToString() + "] " + text;
}
Console.WriteLine(text);
if (Settings.data.writeToLog)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
if (File.Exists(fullPath))
{
lines = File.ReadAllLines(fullPath).ToList();
}
lines.Insert(0, text);
if (maxLogFileSizeKB > 0)
{
while (GetSizeInKiloBytes(ref lines) > maxLogFileSizeKB)
{
lines.RemoveAt(lines.Count - 1);
}
}
File.WriteAllLines(fullPath, lines);
}
OnLogUpdated?.Invoke(null, new OnLogUpdatedEventArgs(text, isError));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
public static float GetSizeInKiloBytes(ref List<string> list)
{
float size = 0;
foreach (string line in list)
{
size += ASCIIEncoding.UTF8.GetByteCount(line);
}
if (size > 0)
{
size /= 1000f;
}
return size;
}
public static string ReadLogFile()
{
string logFileText = "";
try
{
string directory = GetDirectory();
if (directory != null)
{
string fullPath = Path.Combine(directory, fileName);
if (File.Exists(fullPath))
{
logFileText = File.ReadAllText(fullPath);
}
}
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
return logFileText;
}
public static List<string> GetSessionErrors()
{
return sessionErrors;
}
public static string GetSessionErrorsString()
{
string errorString = "";
if (sessionErrors.Count > 0)
{
errorString = sessionErrors[0];
for (int i = 1; i < sessionErrors.Count; i++)
{
errorString += Environment.NewLine + sessionErrors[i];
}
}
return errorString;
}
public static void ClearSessionErrors()
{
sessionErrors.Clear();
}
public static void ClearLog()
{
string directory = GetDirectory();
if (directory != null)
{
string fullPath = Path.Combine(directory, fileName);
if (File.Exists(fullPath))
{
File.WriteAllText(fullPath, "");
}
}
}
public static string GetDirectory()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SimpleBackup");
}
}
public class OnLogUpdatedEventArgs : EventArgs
{
public string text { get; set; }
public bool isError { get; set; }
public OnLogUpdatedEventArgs(string text, bool isError)
{
this.text = text;
this.isError = isError;
}
}
}