@@ -1,8 +1,6 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.IO; | |||
using System.IO.Compression; | |||
using System.Text; | |||
namespace Shadowsocks.Controller | |||
{ | |||
@@ -12,9 +10,7 @@ namespace Shadowsocks.Controller | |||
{ | |||
try | |||
{ | |||
System.IO.FileStream _FileStream = | |||
new System.IO.FileStream(fileName, System.IO.FileMode.Create, | |||
System.IO.FileAccess.Write); | |||
FileStream _FileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write); | |||
_FileStream.Write(content, 0, content.Length); | |||
_FileStream.Close(); | |||
return true; | |||
@@ -31,7 +27,7 @@ namespace Shadowsocks.Controller | |||
{ | |||
FileStream destinationFile = File.Create(fileName); | |||
// Because the uncompressed size of the file is unknown, | |||
// Because the uncompressed size of the file is unknown, | |||
// we are using an arbitrary buffer size. | |||
byte[] buffer = new byte[4096]; | |||
int n; | |||
@@ -39,17 +35,33 @@ namespace Shadowsocks.Controller | |||
using (GZipStream input = new GZipStream(new MemoryStream(content), | |||
CompressionMode.Decompress, false)) | |||
{ | |||
while (true) | |||
while ((n = input.Read(buffer, 0, buffer.Length)) > 0) | |||
{ | |||
n = input.Read(buffer, 0, buffer.Length); | |||
if (n == 0) | |||
{ | |||
break; | |||
} | |||
destinationFile.Write(buffer, 0, n); | |||
} | |||
} | |||
destinationFile.Close(); | |||
} | |||
public static void CompressFile(string fileName, byte[] content) | |||
{ | |||
FileStream destinationFile = File.Create(fileName); | |||
MemoryStream ms = new MemoryStream(content); | |||
// Because the compressed size of the file is unknown, | |||
// we are using an arbitrary buffer size. | |||
byte[] buffer = new byte[4096]; | |||
int n; | |||
using (GZipStream output = new GZipStream(destinationFile, | |||
CompressionMode.Compress, false)) | |||
{ | |||
while ((n = ms.Read(buffer, 0, buffer.Length)) > 0) | |||
{ | |||
output.Write(buffer, 0, n); | |||
} | |||
} | |||
destinationFile.Close(); | |||
} | |||
} | |||
} |
@@ -10,17 +10,28 @@ namespace Shadowsocks.Controller | |||
public class Logging | |||
{ | |||
public static string LogFilePath; | |||
private static DateTime LogFileCreationTime; | |||
public static bool OpenLogFile() | |||
{ | |||
try | |||
{ | |||
LogFilePath = Utils.GetTempPath("shadowsocks.log"); | |||
FileStream fs = new FileStream(LogFilePath, FileMode.Append); | |||
StreamWriterWithTimestamp sw = new StreamWriterWithTimestamp(fs); | |||
sw.AutoFlush = true; | |||
Console.SetOut(sw); | |||
Console.SetError(sw); | |||
if (!File.Exists(LogFilePath)) | |||
using (File.Create(LogFilePath)) { } | |||
LogFileCreationTime = File.GetCreationTime(LogFilePath); | |||
if ((DateTime.Now - LogFileCreationTime).Days >= 1) | |||
RollLogFile(); | |||
else | |||
{ | |||
FileStream fs = new FileStream(LogFilePath, FileMode.Append); | |||
StreamWriterWithTimestamp sw = new StreamWriterWithTimestamp(fs); | |||
sw.AutoFlush = true; | |||
Console.SetOut(sw); | |||
Console.SetError(sw); | |||
} | |||
return true; | |||
} | |||
@@ -31,20 +42,53 @@ namespace Shadowsocks.Controller | |||
} | |||
} | |||
private static void RollLogFile() | |||
{ | |||
Console.Out.Close(); | |||
Console.Error.Close(); | |||
MemoryStream ms = new MemoryStream(); | |||
StreamWriterWithTimestamp sw = new StreamWriterWithTimestamp(ms); | |||
sw.AutoFlush = true; | |||
Console.SetOut(sw); | |||
Console.SetError(sw); | |||
byte[] logContents = File.ReadAllBytes(LogFilePath); | |||
string datestr = DateTime.Now.AddDays(-1).ToString("yyyyMMdd"); | |||
string filepath = Utils.GetTempPath($"shadowsocks.{datestr}.log.zip"); | |||
FileManager.CompressFile(filepath, logContents); | |||
File.Delete(LogFilePath); | |||
FileStream fs = new FileStream(LogFilePath, FileMode.CreateNew); | |||
LogFileCreationTime = DateTime.Now; | |||
ms.CopyTo(fs); | |||
StreamWriterWithTimestamp sw2 = new StreamWriterWithTimestamp(fs); | |||
sw2.AutoFlush = true; | |||
Console.SetOut(sw2); | |||
Console.SetError(sw2); | |||
} | |||
private static void WriteToLogFile(object o) | |||
{ | |||
if ((DateTime.Now - LogFileCreationTime).Days >= 1) | |||
RollLogFile(); | |||
Console.WriteLine(o); | |||
} | |||
public static void Error(object o) | |||
{ | |||
Console.WriteLine("[E] " + o); | |||
WriteToLogFile("[E] " + o); | |||
} | |||
public static void Info(object o) | |||
{ | |||
Console.WriteLine(o); | |||
WriteToLogFile(o); | |||
} | |||
public static void Debug(object o) | |||
{ | |||
#if DEBUG | |||
Console.WriteLine("[D] " + o); | |||
WriteToLogFile("[D] " + o); | |||
#endif | |||
} | |||