Browse Source

日志清理改为直接清理文件

日志文件越来越大,原有的清空日志只是清理记录窗口中显示的日志,实际并没有真的删除。
tags/3.2
金黎明 8 years ago
parent
commit
81188103f2
No known key found for this signature in database GPG Key ID: 278A6C7095AD90F2
2 changed files with 38 additions and 21 deletions
  1. +18
    -3
      shadowsocks-csharp/Controller/Logging.cs
  2. +20
    -18
      shadowsocks-csharp/View/LogForm.cs

+ 18
- 3
shadowsocks-csharp/Controller/Logging.cs View File

@@ -12,14 +12,17 @@ namespace Shadowsocks.Controller
{
public static string LogFilePath;
private static FileStream fs;
private static StreamWriterWithTimestamp sw;
public static bool OpenLogFile()
{
try
{
LogFilePath = Utils.GetTempPath("shadowsocks.log");
FileStream fs = new FileStream(LogFilePath, FileMode.Append);
StreamWriterWithTimestamp sw = new StreamWriterWithTimestamp(fs);
fs = new FileStream(LogFilePath, FileMode.Append);
sw = new StreamWriterWithTimestamp(fs);
sw.AutoFlush = true;
Console.SetOut(sw);
Console.SetError(sw);
@@ -35,7 +38,10 @@ namespace Shadowsocks.Controller
private static void WriteToLogFile(object o)
{
Console.WriteLine(o);
try {
Console.WriteLine(o);
} catch(ObjectDisposedException) {
}
}
public static void Error(object o)
@@ -48,6 +54,15 @@ namespace Shadowsocks.Controller
WriteToLogFile(o);
}
public static void clear() {
sw.Close();
sw.Dispose();
fs.Close();
fs.Dispose();
File.Delete(LogFilePath);
OpenLogFile();
}
[Conditional("DEBUG")]
public static void Debug(object o)
{


+ 20
- 18
shadowsocks-csharp/View/LogForm.cs View File

@@ -149,25 +149,25 @@ namespace Shadowsocks.View
private void UpdateContent()
{
using (StreamReader reader = new StreamReader(new FileStream(filename,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite)))
{
reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin);
string line = "";
bool changed = false;
while ((line = reader.ReadLine()) != null)
{
changed = true;
LogMessageTextBox.AppendText(line + Environment.NewLine);
}
if (changed)
{
LogMessageTextBox.ScrollToCaret();
try {
using(StreamReader reader = new StreamReader(new FileStream(filename,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite))) {
reader.BaseStream.Seek(lastOffset, SeekOrigin.Begin);
string line = "";
bool changed = false;
while((line = reader.ReadLine()) != null) {
changed = true;
LogMessageTextBox.AppendText(line + Environment.NewLine);
}
if(changed) {
LogMessageTextBox.ScrollToCaret();
}
lastOffset = reader.BaseStream.Position;
}
lastOffset = reader.BaseStream.Position;
} catch(FileNotFoundException) {
}
this.Text = I18N.GetString("Log Viewer") +
@@ -247,6 +247,8 @@ namespace Shadowsocks.View
#region Clean up the content in LogMessageTextBox.
private void DoCleanLogs()
{
Logging.clear();
lastOffset = 0;
LogMessageTextBox.Clear();
}


Loading…
Cancel
Save