-
Notifications
You must be signed in to change notification settings - Fork 36
/
Logger.cs
88 lines (82 loc) · 2.47 KB
/
Logger.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
using System;
using System.IO;
namespace SamFirm
{
internal class Logger
{
public static bool nologging = false;
public static Form1 form;
private static string GetTimeDate()
{
string empty = string.Empty;
return DateTime.Now.ToString("dd/MM/yyyy") + " " + DateTime.Now.ToString("HH:mm:ss");
}
private static void CleanLog()
{
if (Utility.run_by_cmd)
return;
if (Logger.form.log_textbox.InvokeRequired)
{
Logger.form.log_textbox.Invoke((Delegate)((Action)(() =>
{
if (Logger.form.log_textbox.Lines.Length <= 30)
return;
Logger.form.log_textbox.Text.Remove(0, Logger.form.log_textbox.GetFirstCharIndexFromLine(1));
})));
}
else
{
if (Logger.form.log_textbox.Lines.Length <= 30)
return;
Logger.form.log_textbox.Text.Remove(0, Logger.form.log_textbox.GetFirstCharIndexFromLine(1));
}
}
public static void WriteLog(string str, bool raw = false)
{
if (Logger.nologging)
return;
Logger.CleanLog();
if (!raw)
str += "\n";
if (Utility.run_by_cmd)
Console.Write(str);
else if (Logger.form.log_textbox.InvokeRequired)
{
Logger.form.log_textbox.Invoke((Delegate)((Action)(() =>
{
Logger.form.log_textbox.AppendText(str);
Logger.form.log_textbox.ScrollToCaret();
})));
}
else
{
Logger.form.log_textbox.AppendText(str);
Logger.form.log_textbox.ScrollToCaret();
}
}
public static void SaveLog()
{
string AppLocation = System.AppDomain.CurrentDomain.BaseDirectory;
string LogFile = AppLocation + "SamFirm.log";
string OldLogFile = AppLocation + "SamFirm.log.old";
try
{
if (string.IsNullOrEmpty(Logger.form.log_textbox.Text))
return;
if (File.Exists(LogFile) && new FileInfo(LogFile).Length > 2097152L)
{
File.Delete(OldLogFile);
File.Move(LogFile, OldLogFile);
}
using (TextWriter textWriter = new StreamWriter(new FileStream(LogFile, FileMode.Append)))
{
textWriter.WriteLine();
textWriter.WriteLine(Logger.GetTimeDate());
foreach (string line in Logger.form.log_textbox.Lines)
textWriter.WriteLine(line);
}
}
catch { }
}
}
}