Merge pull request #654 from 772807886/master

日志相关修改
This commit is contained in:
Syrone Wong
2016-08-13 17:54:40 +08:00
committed by GitHub
3 changed files with 48 additions and 24 deletions
+18 -3
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)
{
@@ -19,6 +19,7 @@ namespace Shadowsocks.Model
public int height;
public int top;
public int left;
public bool maximized;
public LogViewerConfig()
{
@@ -33,6 +34,7 @@ namespace Shadowsocks.Model
height = 400;
left = GetBestLeft();
top = GetBestTop();
maximized = true;
}
// Use GetBestTop() and GetBestLeft() to ensure the log viwer form can be always display IN screen.
+28 -21
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);
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);
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;
}
if (changed)
{
LogMessageTextBox.ScrollToCaret();
}
lastOffset = reader.BaseStream.Position;
} catch(FileNotFoundException) {
}
this.Text = I18N.GetString("Log Viewer") +
@@ -190,6 +190,9 @@ namespace Shadowsocks.View
Width = config.width;
Top = config.GetBestTop();
Left = config.GetBestLeft();
if(config.maximized) {
WindowState = FormWindowState.Maximized;
}
topMostTriggerLock = true;
TopMost = TopMostMenuItem.Checked = TopMostCheckBox.Checked = topMostTrigger;
@@ -215,10 +218,12 @@ namespace Shadowsocks.View
config.SetFont(LogMessageTextBox.Font);
config.SetBackgroundColor(LogMessageTextBox.BackColor);
config.SetTextColor(LogMessageTextBox.ForeColor);
config.top = Top;
config.left = Left;
config.height = Height;
config.width = Width;
if(!(config.maximized = WindowState == FormWindowState.Maximized)) {
config.top = Top;
config.left = Left;
config.height = Height;
config.width = Width;
}
controller.SaveLogViewerConfig(config);
}
@@ -242,6 +247,8 @@ namespace Shadowsocks.View
#region Clean up the content in LogMessageTextBox.
private void DoCleanLogs()
{
Logging.clear();
lastOffset = 0;
LogMessageTextBox.Clear();
}