-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemLogger.cs
More file actions
251 lines (222 loc) · 11.2 KB
/
Copy pathSystemLogger.cs
File metadata and controls
251 lines (222 loc) · 11.2 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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace EasyVersionBackup
{
public static class SystemLogger
{
private static readonly object SyncRoot =
new object();
private static readonly string LogDirectoryPath =
Path.Combine(
AppContext.BaseDirectory,
"Logs");
private static readonly string LogFilePath =
Path.Combine(
LogDirectoryPath,
"EasyVersionBackup_system.log");
public static void WriteSettingsChanges(
AppSettings previousSettings,
AppSettings currentSettings)
{
WriteValueChange("Global.ZipDestinationFiles", previousSettings.ZipDestinationFiles, currentSettings.ZipDestinationFiles);
WriteValueChange("Global.DefaultVersioning", previousSettings.DefaultVersioning, currentSettings.DefaultVersioning);
WriteValueChange("Global.AutoIncrementVersion", previousSettings.AutoIncrementVersion, currentSettings.AutoIncrementVersion);
WriteValueChange("Global.MinimizeToSystray", previousSettings.MinimizeToSystray, currentSettings.MinimizeToSystray);
WriteValueChange("Global.CloseToSystray", previousSettings.CloseToSystray, currentSettings.CloseToSystray);
WriteValueChange("Global.AutoUpdateCheck", previousSettings.AutoUpdateCheck, currentSettings.AutoUpdateCheck);
WriteValueChange("Global.StartWithWindows", previousSettings.StartWithWindows, currentSettings.StartWithWindows);
WriteValueChange("Global.IgnoreCopyErrors", previousSettings.IgnoreCopyErrors, currentSettings.IgnoreCopyErrors);
WriteValueChange("Global.AutoPurgeEnabled", previousSettings.AutoPurgeEnabled, currentSettings.AutoPurgeEnabled);
WriteValueChange("Global.SourceCleanupEnabled", previousSettings.SourceCleanupEnabled, currentSettings.SourceCleanupEnabled);
WriteValueChange("Global.ShowRetentionWarningDialogue", previousSettings.ShowRetentionWarningDialogue, currentSettings.ShowRetentionWarningDialogue);
WriteValueChange("Global.BackupDestinationConflictHandling", previousSettings.BackupDestinationConflictHandling, currentSettings.BackupDestinationConflictHandling);
WriteValueChange("Global.AutoBackupEnabled", previousSettings.AutoBackupEnabled, currentSettings.AutoBackupEnabled);
WriteValueChange("Global.AutoBackupIntervalSeconds", previousSettings.AutoBackupIntervalSeconds, currentSettings.AutoBackupIntervalSeconds);
WriteValueChange("Global.LogLevel", previousSettings.LogLevel, currentSettings.LogLevel);
WriteListChange("Global.Tags", previousSettings.Tags, currentSettings.Tags);
WriteBackupPairChanges(
previousSettings.BackupPathPairs,
currentSettings.BackupPathPairs);
}
public static void WriteExperimentalConfirmation(
string featureName,
string enteredText)
{
WriteLine(
$"EXPERIMENTAL FEATURE CONFIRMATION | feature=\"{Escape(featureName)}\" | enteredText=\"{Escape(enteredText)}\"");
}
public static void WriteInitialDisclaimerConfirmation(
string enteredText)
{
WriteLine(
$"INITIAL DISCLAIMER ACCEPTED | enteredText=\"{Escape(enteredText)}\"");
}
public static void WriteRecurringDataLossWarning(
bool retentionEnabled,
bool sourceCleanupEnabled,
bool accepted,
string enteredText)
{
WriteLine(
$"RECURRING DATA-LOSS WARNING | retentionEnabled={retentionEnabled} | sourceCleanupEnabled={sourceCleanupEnabled} | accepted={accepted} | enteredText=\"{Escape(enteredText)}\"");
}
private static void WriteBackupPairChanges(
IReadOnlyList<BackupPathPair> previousPairs,
IReadOnlyList<BackupPathPair> currentPairs)
{
int sharedCount =
Math.Min(
previousPairs.Count,
currentPairs.Count);
for (int index = 0; index < sharedCount; index++)
{
BackupPathPair previousPair =
previousPairs[index];
BackupPathPair currentPair =
currentPairs[index];
string pairName =
$"BackupPair[{index}]";
WriteValueChange(pairName + ".IsEnabled", previousPair.IsEnabled, currentPair.IsEnabled);
WriteValueChange(pairName + ".SourceDirectory", previousPair.SourceDirectory, currentPair.SourceDirectory);
WriteValueChange(pairName + ".TargetDirectory", previousPair.TargetDirectory, currentPair.TargetDirectory);
WriteValueChange(pairName + ".Versioning", previousPair.Versioning, currentPair.Versioning);
WriteValueChange(pairName + ".IgnoreCopyErrors", previousPair.IgnoreCopyErrors, currentPair.IgnoreCopyErrors);
WriteValueChange(pairName + ".SkipDialogs", previousPair.SkipDialogs, currentPair.SkipDialogs);
WriteValueChange(pairName + ".AutoBackupIntervalSeconds", previousPair.AutoBackupIntervalSeconds, currentPair.AutoBackupIntervalSeconds);
WriteValueChange(pairName + ".RetentionKeepLastEnabled", previousPair.RetentionKeepLastEnabled, currentPair.RetentionKeepLastEnabled);
WriteValueChange(pairName + ".RetentionKeepLastCount", previousPair.RetentionKeepLastCount, currentPair.RetentionKeepLastCount);
WriteValueChange(pairName + ".RetentionKeepDaysEnabled", previousPair.RetentionKeepDaysEnabled, currentPair.RetentionKeepDaysEnabled);
WriteValueChange(pairName + ".RetentionKeepDaysCount", previousPair.RetentionKeepDaysCount, currentPair.RetentionKeepDaysCount);
WriteValueChange(pairName + ".RetentionMode", previousPair.RetentionMode, currentPair.RetentionMode);
WriteListChange(pairName + ".RetentionExcludedTags", previousPair.RetentionExcludedTags, currentPair.RetentionExcludedTags);
WriteListChange(pairName + ".ExcludedPaths", previousPair.ExcludedPaths, currentPair.ExcludedPaths);
WriteValueChange(pairName + ".SourceCleanupEnabled", previousPair.SourceCleanupEnabled, currentPair.SourceCleanupEnabled);
WriteValueChange(pairName + ".SourceCleanupRelativeDirectory", previousPair.SourceCleanupRelativeDirectory, currentPair.SourceCleanupRelativeDirectory);
WriteListChange(pairName + ".SourceCleanupFileExtensions", previousPair.SourceCleanupFileExtensions, currentPair.SourceCleanupFileExtensions);
WriteValueChange(pairName + ".SourceCleanupMode", previousPair.SourceCleanupMode, currentPair.SourceCleanupMode);
WriteValueChange(pairName + ".SourceCleanupKeepLastCount", previousPair.SourceCleanupKeepLastCount, currentPair.SourceCleanupKeepLastCount);
WriteValueChange(pairName + ".SourceCleanupKeepAfterDate", previousPair.SourceCleanupKeepAfterDate, currentPair.SourceCleanupKeepAfterDate);
}
for (int index = sharedCount; index < currentPairs.Count; index++)
{
BackupPathPair pair =
currentPairs[index];
WriteLine(
$"SETTING ADDED | setting=\"BackupPair[{index}]\" | source=\"{Escape(pair.SourceDirectory)}\" | target=\"{Escape(pair.TargetDirectory)}\"");
}
for (int index = sharedCount; index < previousPairs.Count; index++)
{
BackupPathPair pair =
previousPairs[index];
WriteLine(
$"SETTING REMOVED | setting=\"BackupPair[{index}]\" | source=\"{Escape(pair.SourceDirectory)}\" | target=\"{Escape(pair.TargetDirectory)}\"");
}
}
private static void WriteValueChange<T>(
string settingName,
T previousValue,
T currentValue)
{
if (EqualityComparer<T>.Default.Equals(
previousValue,
currentValue))
{
return;
}
WriteLine(
$"SETTING CHANGED | setting=\"{Escape(settingName)}\" | old=\"{Escape(FormatValue(previousValue))}\" | new=\"{Escape(FormatValue(currentValue))}\"");
}
private static void WriteListChange(
string settingName,
IEnumerable<string>? previousValues,
IEnumerable<string>? currentValues)
{
string previousValue =
FormatList(previousValues);
string currentValue =
FormatList(currentValues);
if (string.Equals(
previousValue,
currentValue,
StringComparison.Ordinal))
{
return;
}
WriteLine(
$"SETTING CHANGED | setting=\"{Escape(settingName)}\" | old=\"{Escape(previousValue)}\" | new=\"{Escape(currentValue)}\"");
}
private static string FormatList(
IEnumerable<string>? values)
{
return values == null
? string.Empty
: string.Join(
" | ",
values);
}
private static string FormatValue<T>(
T value)
{
if (value == null)
{
return string.Empty;
}
if (value is IFormattable formattable)
{
return formattable.ToString(
null,
CultureInfo.InvariantCulture) ??
string.Empty;
}
return value.ToString() ??
string.Empty;
}
private static string Escape(
string value)
{
return value
.Replace(
"\\",
"\\\\",
StringComparison.Ordinal)
.Replace(
"\"",
"\\\"",
StringComparison.Ordinal)
.Replace(
"\r",
"\\r",
StringComparison.Ordinal)
.Replace(
"\n",
"\\n",
StringComparison.Ordinal);
}
private static void WriteLine(
string message)
{
try
{
lock (SyncRoot)
{
Directory.CreateDirectory(
LogDirectoryPath);
string logLine =
$"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} | {message}{Environment.NewLine}";
File.AppendAllText(
LogFilePath,
logLine);
}
}
catch (Exception exception)
{
System.Diagnostics.Debug.WriteLine(
"System log entry could not be written: " +
exception.Message);
}
}
}
}