-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginLog.cs
134 lines (109 loc) · 3.82 KB
/
PluginLog.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace GeniePlugin
{
public class PluginLog
{
private GeniePlugin.Interfaces.IHost _host;
private GeniePlugin.Interfaces.IPlugin _plugin;
private TextWriter _logWriter;
private string _logName;
private string _fileName;
private string _filePath;
private string _fullPath;
private bool _logGameTime;
private bool _clearLogAtStart;
private bool _newLogAtStart;
private bool _useTitleSeparator;
private string _logTitle;
private string _titleSeparator;
private bool _logStarted;
public PluginLog(GeniePlugin.Interfaces.IHost Host, GeniePlugin.Interfaces.IPlugin PlugIn)
{
_host = Host;
_plugin = PlugIn;
_logName = _plugin.Name.Replace(" ", "_");
_fileName = _logName + ".log";
_filePath = Application.StartupPath;
if (_filePath != "\\")
_filePath += "\\";
_filePath += "Logs\\";
_fullPath = _filePath + _fileName;
_logGameTime = false;
_clearLogAtStart = false;
_newLogAtStart = false;
_useTitleSeparator = true;
_titleSeparator = new String('=',70);
_logStarted = false;
}
public bool LogGameTime {
get { return _logGameTime; }
set { _logGameTime = value; }
}
public bool ClearLogAtStart
{
get { return _clearLogAtStart; }
set { _clearLogAtStart = value; }
}
public bool NewLogAtStart
{
get { return _newLogAtStart; }
set { _newLogAtStart = value; }
}
public void StartLog(string Title)
{
int i = 1;
if (_newLogAtStart)
while (File.Exists(_fullPath))
{
_fullPath = _filePath + _logName + "(" + i.ToString() + ").log";
i++;
}
_logWriter = new StreamWriter(_fullPath, !_clearLogAtStart);
_logTitle = Title;
if (_logTitle.Length > 0 && _useTitleSeparator)
{
_logWriter.WriteLine(_titleSeparator);
}
if (_logTitle.Length > 0)
{
_logWriter.WriteLine(string.Format("{0} started: {1}", _logTitle, DateTime.Now));
}
if (_logTitle.Length > 0 && _useTitleSeparator)
{
_logWriter.WriteLine(_titleSeparator);
}
_logWriter.Close();
_logStarted = true;
}
public void Log(string Text) {
string logText;
if (!_logStarted)
StartLog("");
_logWriter = new StreamWriter(_fullPath, true);
logText = string.Format("[{0:HH:mm:ss}]\t", DateTime.Now);
if (_logGameTime)
logText += string.Format("({0})\t", _host.get_Variable("gametime"));
logText += Text;
_logWriter.WriteLine(logText);
_logWriter.Close();
}
public void EndLog()
{
_logWriter = new StreamWriter(_fullPath, true);
if (_logTitle.Length > 0 && _useTitleSeparator)
{
_logWriter.WriteLine(_titleSeparator);
}
if (_logTitle.Length > 0)
{
_logWriter.WriteLine(string.Format("{0} ended: {1}", _logTitle, DateTime.Now));
}
_logWriter.Close();
_logStarted = false;
}
}
}