forked from Montellese/xbmc-on-imon
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Logging.cs
155 lines (131 loc) · 5.13 KB
/
Logging.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using iMon.XBMC.Properties;
namespace iMon.XBMC
{
internal static class Logging
{
#region Private variables
//internal const string ErrorLog = "error.log";
//internal const string DebugLog = "debug.log";
//internal const string OldLog = ".old";
//internal const string ErrorLog = Settings.Default.ErrorLogFilename;
internal static string DebugLog = Settings.Default.DebugLogFilename;
internal static string OldLog = Settings.Default.OldLogExtension;
// For testing purposes only - should be set to true in final releases
internal const bool iMonNotRespondingLog = false;
#endregion
#region Public functions
public static void initialize(string logPath)
{
initialize(logPath, FileMode.Create);
}
public static void initialize(string logPath, FileMode mode)
{
TextWriterTraceListener traceListener;
FileStream logFile;
logFile = File.Open(Path.Combine(logPath, DebugLog), mode, FileAccess.Write, FileShare.Read);
traceListener = new TextWriterTraceListener(logFile, "XbmcOniMonLogger");
Trace.Listeners.Add(traceListener);
Trace.AutoFlush = true;
}
public static void deinitialize()
{
Trace.Close();
}
public static void Error(string message)
{
Error("GUI", message, null);
}
public static void Error(string area, string message)
{
Error(area, message, null);
}
public static void Error(string message, Exception exception)
{
Error("GUI", message, exception);
}
public static void Error(string area, string message, Exception exception)
{
// Do not log "iMonNotResponding" messages when forbidden
if (iMonNotRespondingLog == false && message.Contains("iMonNotResponding"))
{
return;
}
//if (Settings.Default.GeneralDebugEnable)
//{
// Log(area, "ERROR " + message, exception);
// return;
//}
// New logging mechanism
Trace.TraceError("{0} [{1}] {2}", DateTime.Now, area, message);
if (exception != null)
{
Trace.TraceError(" {0}: {1}" + Environment.NewLine +
" {2}", exception.GetType().Name, exception.Message, exception.StackTrace);
}
//try
//{
// using (StreamWriter file = new StreamWriter(ErrorLog, true, Encoding.UTF8))
// {
// file.WriteLine("{0} [{1}] {2}", DateTime.Now, area, message);
// if (exception != null)
// {
// file.WriteLine(" {0}: {1}" + Environment.NewLine +
// " {2}", exception.GetType().Name, exception.Message, exception.StackTrace);
// }
// }
//}
//catch (Exception)
//{ }
}
public static void Log(string message)
{
Log("GUI", message, null);
}
public static void Log(string area, string message)
{
Log(area, message, null);
}
public static void Log(string message, Exception exception)
{
Log("GUI", message, exception);
}
public static void Log(string area, string message, Exception exception)
{
if (!Settings.Default.GeneralDebugEnable)
{
return;
}
// Do not log "iMonNotResponding" messages when forbidden
if (iMonNotRespondingLog == false && message.Contains("iMonNotResponding"))
{
return;
}
// New logging mechanism
Trace.TraceInformation("{0} [{1}] {2}", DateTime.Now, area, message);
if (exception != null)
{
Trace.TraceInformation(" {0}: {1}" + Environment.NewLine +
" {2}", exception.GetType().Name, exception.Message, exception.StackTrace);
}
//try
//{
// using (StreamWriter file = new StreamWriter(DebugLog, true, Encoding.UTF8))
// {
// file.WriteLine("{0} [{1}] {2}", DateTime.Now, area, message);
// if (exception != null)
// {
// file.WriteLine(" {0}: {1}" + Environment.NewLine +
// " {2}", exception.GetType().Name, exception.Message, exception.StackTrace);
// }
// }
//}
//catch (Exception)
//{ }
}
#endregion
}
}