blob: 6a1f7bb6ff9b511ee9150fdbf995a47c477f50c7 (
plain)
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
|
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LogHandler.cs" company="HandBrake Project (http://handbrake.fr)">
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
// The log service.
// The Interop Classes are not very OO friendly. This is a shim over the logging code to allow a nice Web API to be built on top.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrake.Worker.Logging
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using HandBrake.Interop.Interop;
using HandBrake.Interop.Interop.EventArgs;
using HandBrake.Worker.Logging.Interfaces;
using HandBrake.Worker.Logging.Models;
public class LogHandler : ILogHandler
{
private readonly string logFile;
private readonly object lockObject = new object();
private readonly object fileWriterLock = new object();
private readonly StringBuilder logBuilder = new StringBuilder();
private readonly List<LogMessage> logMessages = new List<LogMessage>();
private bool isLoggingEnabled = true;
private bool isDiskLoggingEnabled = false;
private int messageIndex;
private StreamWriter fileWriter;
public LogHandler(string logDirectory, string logFileName, bool enableDiskLogging)
{
this.isDiskLoggingEnabled = enableDiskLogging;
if (this.isDiskLoggingEnabled)
{
lock (this.fileWriterLock)
{
// Todo Handle no log directory.
this.fileWriter = new StreamWriter(logFileName) { AutoFlush = true };
}
}
HandBrakeUtils.MessageLogged += this.HandBrakeUtils_MessageLogged;
HandBrakeUtils.ErrorLogged += this.HandBrakeUtils_ErrorLogged;
}
public IEnumerable<LogMessage> LogMessages
{
get
{
lock (this.lockObject)
{
return this.logMessages.ToList();
}
}
}
public string GetFullLog()
{
lock (this.lockObject)
{
return this.logBuilder.ToString();
}
}
public List<LogMessage> GetLogMessages()
{
lock (this.lockObject)
{
return new List<LogMessage>(this.logMessages);
}
}
public List<LogMessage> GetLogMessagesFromIndex(int index)
{
List<LogMessage> log = new List<LogMessage>();
lock (this.lockObject)
{
// Note messageIndex is not 0 based.
for (int i = index; i < this.messageIndex; i++)
{
log.Add(this.logMessages[i]);
}
}
return log;
}
public void LogMessage(string content)
{
Console.WriteLine(content);
if (!this.isLoggingEnabled)
{
return;
}
LogMessage msg = new LogMessage(content, this.messageIndex);
lock (this.lockObject)
{
this.messageIndex = this.messageIndex + 1;
this.logMessages.Add(msg);
this.logBuilder.AppendLine(msg.Content);
this.LogMessageToDisk(msg);
if (this.logMessages.Count > 50000)
{
this.messageIndex = this.messageIndex + 1;
msg = new LogMessage("Log Service Pausing. Too Many Log messages. This may indicate a problem with your encode.", this.messageIndex);
this.logMessages.Add(msg);
this.logBuilder.AppendLine(msg.Content);
this.LogMessageToDisk(msg);
this.isLoggingEnabled = false;
}
}
}
public void Reset()
{
lock (this.lockObject)
{
this.logMessages.Clear();
this.logBuilder.Clear();
this.messageIndex = 0;
}
}
public void ShutdownFileWriter()
{
try
{
lock (this.fileWriterLock)
{
if (this.fileWriter != null)
{
this.fileWriter.Flush();
this.fileWriter.Close();
this.fileWriter.Dispose();
}
this.fileWriter = null;
}
}
catch (Exception exc)
{
Debug.WriteLine(exc); // This exception doesn't warrant user interaction, but it should be logged
}
}
private void LogMessageToDisk(LogMessage msg)
{
if (!this.isDiskLoggingEnabled)
{
return;
}
try
{
lock (this.fileWriterLock)
{
if (this.fileWriter != null && this.fileWriter.BaseStream.CanWrite)
{
this.fileWriter.WriteLine(msg.Content);
}
}
}
catch (Exception exc)
{
Debug.WriteLine(exc); // This exception doesn't warrant user interaction, but it should be logged
}
}
private void HandBrakeUtils_ErrorLogged(object sender, MessageLoggedEventArgs e)
{
if (e == null || string.IsNullOrEmpty(e.Message))
{
return;
}
this.LogMessage(e.Message);
}
private void HandBrakeUtils_MessageLogged(object sender, MessageLoggedEventArgs e)
{
if (e == null || string.IsNullOrEmpty(e.Message))
{
return;
}
this.LogMessage(e.Message);
}
}
}
|