-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.cs
139 lines (122 loc) · 5.37 KB
/
Log.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace johnshope.Sync {
public class Log {
public SyncJob Job = null;
public int Uploads = 0;
public int Downloads = 0;
public long UploadSize = 0;
public long DownloadSize = 0;
public int Errors = 0;
const int KB = 1024;
const int MB = 1024 * KB;
const int GB = 1024 * MB;
const int MaxLogSize = 10*MB;
bool checkDir = true;
StringBuilder buffer = new StringBuilder();
public string Size(long size) {
if (size > GB) return string.Format("{0:F2} GB", size / (1.0 * GB));
if (size > 100 * KB) return string.Format("{0:F2} MB", size / (1.0 * MB));
return string.Format("{0:F2} KB", size / (1.0 * KB));
}
public void Debug(string text) {
if (Job.Verbose) Text(text);
}
public void Flush() {
if (Job.LogFile != null) {
try {
if (checkDir) {
checkDir = false;
var dir = Path.GetDirectoryName(Job.LogFile);
if (!string.IsNullOrEmpty(dir) && !System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir);
}
System.IO.File.AppendAllText(Job.LogFile, buffer.ToString(), UTF8Encoding.UTF8);
#if NET4
buffer.Clear();
#else
buffer = new StringBuilder();
#endif
} catch (Exception ex) {
Console.WriteLine("Error writing to the logfile " + Job.LogFile);
Console.WriteLine(ex.Message);
}
}
}
public void LogText(string text, bool newline) {
lock (this) {
if (Job.LogFile != null) {
if (newline) buffer.AppendLine(text);
else buffer.Append(text);
}
}
}
public void Text(string text) { lock (this) { Console.WriteLine(text); LogText(text, true); } }
public void RedText(string text) { lock (this) { var oldc = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Text(text); Console.ForegroundColor = oldc; } }
public void CyanText(string text) { lock (this) { var oldc = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Cyan; Text(text); Console.ForegroundColor = oldc; } }
public void GreenText(string text) { lock (this) { var oldc = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; Text(text); Console.ForegroundColor = oldc; } }
public void YellowText(string text) { lock (this) { var oldc = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Text(text); Console.ForegroundColor = oldc; } }
public void YellowLabel(string text) { lock (this) { var oldc = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(text); LogText(text, false); Console.ForegroundColor = oldc; } }
public void RedLabel(string text) { lock (this) { var oldc = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.Write(text); LogText(text, false); Console.ForegroundColor = oldc; } }
public void Label(string text) { lock (this) { Console.Write(text); LogText(text, false); } }
public void Dot() { lock (this) { var oldc = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; Console.Write("."); LogText(".", false); Console.ForegroundColor = oldc; } }
public void Exception(Exception e) {
if (e is System.Threading.ThreadAbortException) throw e;
lock (this) {
Errors++;
RedText("Error"); RedText(e.Message);
if (Job.Verbose) { RedText(e.StackTrace); }
System.Diagnostics.Debugger.Break();
}
}
public void Exception(FtpClient ftp, Exception e) {
if (e is System.Threading.ThreadAbortException) throw e;
if (ftp == null) Exception(e);
else {
lock (this) {
var prefix = Job.Connections.FTPTag(ftp.Index) + "! ";
Errors++; RedLabel(prefix); RedText("Error");
var lines = e.Message.Split('\n');
foreach (var line in lines) { RedLabel(prefix); RedText(line); }
if (Job.Verbose) {
lines = e.StackTrace.Split('\n');
foreach (var line in lines) { RedLabel(prefix); RedText(line); }
}
System.Diagnostics.Debugger.Break();
}
}
}
public void Upload(string path, long size, TimeSpan time) {
if (Job.Quiet) Dot();
else GreenText(string.Format("Uploaded {0} => {1} at {2:F3}/s.", path, Size(size), Size((long)(size / time.TotalSeconds + 0.5))));
Uploads++; UploadSize += size;
}
public void Download(string path, long size, TimeSpan time) {
if (Job.Quiet) Dot();
else GreenText(string.Format("Downloaded {0} => {1} at {2:F3}/s.", path, Size(size), Size((long)(size / time.TotalSeconds + 0.5))));
Downloads++; DownloadSize += size;
}
public void Progress(string path, long size, long part, TimeSpan time) {
if (Job.Quiet) Dot();
else GreenText(string.Format("Transfer of {0} => {1:F1}% at {2:F3}/s.", path, (part * 100.0 / size), Size((long)(part / time.TotalSeconds + 0.5))));
}
public void Summary(TimeSpan t) {
Text("");
GreenText(string.Format("#### => {0} Files and {1} transfered in {2:F3} seconds at {3}/s. {4} Errors.",
Math.Max(Uploads, Downloads), Size(UploadSize + DownloadSize), t.TotalSeconds, Size((long)(Math.Max(UploadSize, DownloadSize) / t.TotalSeconds + 0.5)), Errors));
Text("");
Text("");
Text("");
if (Job.LogFile != null) {
var log = new FileInfo(Job.LogFile);
if (log.Length > MaxLogSize) {
var loglines = File.ReadAllLines(Job.LogFile).ToList();
loglines.RemoveRange(0, loglines.Count / 2);
File.WriteAllLines(Job.LogFile, loglines.ToArray());
}
}
}
}
}