Browse Source

fixed: standardize Logging usage

1. Add Logging.Info()
2. Console.WriteLine() -> Logging.Info()
3. Console.WriteLine() in exceptions -> Logging.LogUsefulException()
tags/3.0
kimw 9 years ago
parent
commit
0a733c61b8
16 changed files with 42 additions and 41 deletions
  1. +2
    -3
      shadowsocks-csharp/Controller/FileManager.cs
  2. +6
    -2
      shadowsocks-csharp/Controller/Logging.cs
  3. +1
    -1
      shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs
  4. +3
    -3
      shadowsocks-csharp/Controller/Service/Listener.cs
  5. +4
    -4
      shadowsocks-csharp/Controller/Service/PACServer.cs
  6. +2
    -2
      shadowsocks-csharp/Controller/Service/PolipoRunner.cs
  7. +1
    -1
      shadowsocks-csharp/Controller/Service/PortForwarder.cs
  8. +9
    -10
      shadowsocks-csharp/Controller/Service/TCPRelay.cs
  9. +5
    -5
      shadowsocks-csharp/Controller/Strategy/HighAvailabilityStrategy.cs
  10. +3
    -4
      shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs
  11. +1
    -1
      shadowsocks-csharp/Encryption/PolarSSL.cs
  12. +1
    -1
      shadowsocks-csharp/Encryption/Sodium.cs
  13. +1
    -1
      shadowsocks-csharp/Model/Configuration.cs
  14. +1
    -1
      shadowsocks-csharp/Util/Util.cs
  15. +1
    -1
      shadowsocks-csharp/View/LogForm.cs
  16. +1
    -1
      shadowsocks-csharp/View/MenuViewController.cs

+ 2
- 3
shadowsocks-csharp/Controller/FileManager.cs View File

@@ -19,10 +19,9 @@ namespace Shadowsocks.Controller
_FileStream.Close(); _FileStream.Close();
return true; return true;
} }
catch (Exception _Exception)
catch (Exception e)
{ {
Console.WriteLine("Exception caught in process: {0}",
_Exception.ToString());
Logging.LogUsefulException(e);
} }
return false; return false;
} }


+ 6
- 2
shadowsocks-csharp/Controller/Logging.cs View File

@@ -32,11 +32,15 @@ namespace Shadowsocks.Controller
} }
} }
public static void Debug(object o)
public static void Info(object o)
{ {
Console.WriteLine(o);
}
public static void Debug(object o)
{
#if DEBUG #if DEBUG
Console.WriteLine(o);
Console.WriteLine("[D] " + o);
#endif #endif
} }


+ 1
- 1
shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs View File

@@ -81,7 +81,7 @@ namespace Shadowsocks.Controller
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine($"An exception occured when eveluating {server.FriendlyName()}");
Logging.Info($"An exception occured while eveluating {server.FriendlyName()}");
Logging.LogUsefulException(e); Logging.LogUsefulException(e);
} }
} }


+ 3
- 3
shadowsocks-csharp/Controller/Service/Listener.cs View File

@@ -78,7 +78,7 @@ namespace Shadowsocks.Controller
_tcpSocket.Listen(1024); _tcpSocket.Listen(1024);
// Start an asynchronous socket to listen for connections. // Start an asynchronous socket to listen for connections.
Console.WriteLine("Shadowsocks started");
Logging.Info("Shadowsocks started");
_tcpSocket.BeginAccept( _tcpSocket.BeginAccept(
new AsyncCallback(AcceptCallback), new AsyncCallback(AcceptCallback),
_tcpSocket); _tcpSocket);
@@ -163,7 +163,7 @@ namespace Shadowsocks.Controller
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e);
Logging.LogUsefulException(e);
} }
finally finally
{ {
@@ -208,7 +208,7 @@ namespace Shadowsocks.Controller
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e);
Logging.LogUsefulException(e);
conn.Close(); conn.Close();
} }
} }


+ 4
- 4
shadowsocks-csharp/Controller/Service/PACServer.cs View File

@@ -153,7 +153,7 @@ Connection: Close
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e);
Logging.LogUsefulException(e);
socket.Close(); socket.Close();
} }
} }
@@ -217,7 +217,7 @@ Connection: Close
{ {
if (PACFileChanged != null) if (PACFileChanged != null)
{ {
Console.WriteLine("Detected: PAC file '{0}' was {1}.", e.Name, e.ChangeType.ToString().ToLower());
Logging.Info($"Detected: PAC file '{e.Name}' was {e.ChangeType.ToString().ToLower()}.");
PACFileChanged(this, new EventArgs()); PACFileChanged(this, new EventArgs());
} }
@@ -236,7 +236,7 @@ Connection: Close
{ {
if (UserRuleFileChanged != null) if (UserRuleFileChanged != null)
{ {
Console.WriteLine("Detected: User Rule file '{0}' was {1}.", e.Name, e.ChangeType.ToString().ToLower());
Logging.Info($"Detected: User Rule file '{e.Name}' was {e.ChangeType.ToString().ToLower()}.");
UserRuleFileChanged(this, new EventArgs()); UserRuleFileChanged(this, new EventArgs());
} }
//lastly we update the last write time in the hashtable //lastly we update the last write time in the hashtable
@@ -259,7 +259,7 @@ Connection: Close
//} //}
//catch (Exception e) //catch (Exception e)
//{ //{
// Console.WriteLine(e);
// Logging.LogUsefulException(e);
//} //}
return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";"; return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";";
} }


+ 2
- 2
shadowsocks-csharp/Controller/Service/PolipoRunner.cs View File

@@ -56,7 +56,7 @@ namespace Shadowsocks.Controller
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.ToString());
Logging.LogUsefulException(e);
} }
} }
string polipoConfig = Resources.privoxy_conf; string polipoConfig = Resources.privoxy_conf;
@@ -94,7 +94,7 @@ namespace Shadowsocks.Controller
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.ToString());
Logging.LogUsefulException(e);
} }
_process = null; _process = null;
} }


+ 1
- 1
shadowsocks-csharp/Controller/Service/PortForwarder.cs View File

@@ -140,7 +140,7 @@ namespace Shadowsocks.Controller
} }
else else
{ {
//Console.WriteLine("bytesRead: " + bytesRead.ToString());
Logging.Debug($"bytes read: {bytesRead}");
_local.Shutdown(SocketShutdown.Send); _local.Shutdown(SocketShutdown.Send);
_localShutdown = true; _localShutdown = true;
CheckClose(); CheckClose();


+ 9
- 10
shadowsocks-csharp/Controller/Service/TCPRelay.cs View File

@@ -49,7 +49,7 @@ namespace Shadowsocks.Controller
lock (this.Handlers) lock (this.Handlers)
{ {
this.Handlers.Add(handler); this.Handlers.Add(handler);
Logging.Debug($"connections: {Handlers.Count}");
Logging.Debug($"TCP connections: {Handlers.Count}");
DateTime now = DateTime.Now; DateTime now = DateTime.Now;
if (now - _lastSweepTime > TimeSpan.FromSeconds(1)) if (now - _lastSweepTime > TimeSpan.FromSeconds(1))
{ {
@@ -65,10 +65,10 @@ namespace Shadowsocks.Controller
} }
foreach (Handler handler1 in handlersToClose) foreach (Handler handler1 in handlersToClose)
{ {
Logging.Debug("Closing timed out connection");
Logging.Debug("Closing timed out TCP connection.");
handler1.Close(); handler1.Close();
} }
return true;
return true;
} }
} }
@@ -148,7 +148,7 @@ namespace Shadowsocks.Controller
{ {
lock (relay.Handlers) lock (relay.Handlers)
{ {
Logging.Debug($"connections: {relay.Handlers.Count}");
Logging.Debug($"TCP connections: {relay.Handlers.Count}");
relay.Handlers.Remove(this); relay.Handlers.Remove(this);
} }
lock (this) lock (this)
@@ -212,7 +212,7 @@ namespace Shadowsocks.Controller
{ {
// reject socks 4 // reject socks 4
response = new byte[] { 0, 91 }; response = new byte[] { 0, 91 };
Console.WriteLine("socks 5 protocol error");
Logging.Info("socks 5 protocol error");
} }
connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(HandshakeSendCallback), null); connection.BeginSend(response, 0, response.Length, 0, new AsyncCallback(HandshakeSendCallback), null);
} }
@@ -280,7 +280,7 @@ namespace Shadowsocks.Controller
} }
else else
{ {
Console.WriteLine("failed to recv data in handshakeReceive2Callback");
Logging.Info("failed to recv data in handshakeReceive2Callback");
this.Close(); this.Close();
} }
} }
@@ -423,7 +423,7 @@ namespace Shadowsocks.Controller
{ {
strategy.SetFailure(server); strategy.SetFailure(server);
} }
Console.WriteLine(String.Format("{0} timed out", server.FriendlyName()));
Logging.Info($"{server.FriendlyName()} timed out");
remote.Close(); remote.Close();
RetryConnect(); RetryConnect();
} }
@@ -462,8 +462,7 @@ namespace Shadowsocks.Controller
connected = true; connected = true;
//Console.WriteLine("Socket connected to {0}",
// remote.RemoteEndPoint.ToString());
Logging.Debug($"Socket connected to {remote.RemoteEndPoint}");
var latency = DateTime.Now - _startConnectTime; var latency = DateTime.Now - _startConnectTime;
IStrategy strategy = controller.GetCurrentStrategy(); IStrategy strategy = controller.GetCurrentStrategy();
@@ -545,7 +544,7 @@ namespace Shadowsocks.Controller
} }
else else
{ {
//Console.WriteLine("bytesRead: " + bytesRead.ToString());
Logging.Debug($"bytes read: {bytesRead}");
connection.Shutdown(SocketShutdown.Send); connection.Shutdown(SocketShutdown.Send);
connectionShutdown = true; connectionShutdown = true;
CheckClose(); CheckClose();


+ 5
- 5
shadowsocks-csharp/Controller/Strategy/HighAvailabilityStrategy.cs View File

@@ -132,14 +132,14 @@ namespace Shadowsocks.Controller.Strategy
if (_currentServer == null || max.score - _currentServer.score > 200) if (_currentServer == null || max.score - _currentServer.score > 200)
{ {
_currentServer = max; _currentServer = max;
Console.WriteLine("HA switching to server: {0}", _currentServer.server.FriendlyName());
Logging.Info($"HA switching to server: {_currentServer.server.FriendlyName()}");
} }
} }
} }
public void UpdateLatency(Model.Server server, TimeSpan latency) public void UpdateLatency(Model.Server server, TimeSpan latency)
{ {
Logging.Debug(String.Format("latency: {0} {1}", server.FriendlyName(), latency));
Logging.Debug($"latency: {server.FriendlyName()} {latency}");
ServerStatus status; ServerStatus status;
if (_serverStatus.TryGetValue(server, out status)) if (_serverStatus.TryGetValue(server, out status))
@@ -151,7 +151,7 @@ namespace Shadowsocks.Controller.Strategy
public void UpdateLastRead(Model.Server server) public void UpdateLastRead(Model.Server server)
{ {
Logging.Debug(String.Format("last read: {0}", server.FriendlyName()));
Logging.Debug($"last read: {server.FriendlyName()}");
ServerStatus status; ServerStatus status;
if (_serverStatus.TryGetValue(server, out status)) if (_serverStatus.TryGetValue(server, out status))
@@ -162,7 +162,7 @@ namespace Shadowsocks.Controller.Strategy
public void UpdateLastWrite(Model.Server server) public void UpdateLastWrite(Model.Server server)
{ {
Logging.Debug(String.Format("last write: {0}", server.FriendlyName()));
Logging.Debug($"last write: {server.FriendlyName()}");
ServerStatus status; ServerStatus status;
if (_serverStatus.TryGetValue(server, out status)) if (_serverStatus.TryGetValue(server, out status))
@@ -173,7 +173,7 @@ namespace Shadowsocks.Controller.Strategy
public void SetFailure(Model.Server server) public void SetFailure(Model.Server server)
{ {
Logging.Debug(String.Format("failure: {0}", server.FriendlyName()));
Logging.Debug($"failure: {server.FriendlyName()}");
ServerStatus status; ServerStatus status;
if (_serverStatus.TryGetValue(server, out status)) if (_serverStatus.TryGetValue(server, out status))


+ 3
- 4
shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs View File

@@ -47,7 +47,7 @@ namespace Shadowsocks.Controller.Strategy
try try
{ {
var path = AvailabilityStatistics.AvailabilityStatisticsFile; var path = AvailabilityStatistics.AvailabilityStatisticsFile;
Logging.Debug(string.Format("loading statistics from{0}", path));
Logging.Debug($"loading statistics from {path}");
statistics = (from l in File.ReadAllLines(path) statistics = (from l in File.ReadAllLines(path)
.Skip(1) .Skip(1)
let strings = l.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries) let strings = l.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
@@ -113,7 +113,7 @@ namespace Shadowsocks.Controller.Strategy


if (_controller.GetCurrentStrategy().ID == ID && _currentServer != bestResult.server) //output when enabled if (_controller.GetCurrentStrategy().ID == ID && _currentServer != bestResult.server) //output when enabled
{ {
Console.WriteLine("Switch to server: {0} by package loss:{1}", bestResult.server.FriendlyName(), 1 - bestResult.score);
Logging.Info($"Switch to server: {bestResult.server.FriendlyName()} by package loss:{1 - bestResult.score}");
} }
_currentServer = bestResult.server; _currentServer = bestResult.server;
} }
@@ -154,7 +154,7 @@ namespace Shadowsocks.Controller.Strategy


public void SetFailure(Server server) public void SetFailure(Server server)
{ {
Logging.Debug(String.Format("failure: {0}", server.FriendlyName()));
Logging.Debug($"failure: {server.FriendlyName()}");
} }


public void UpdateLastRead(Server server) public void UpdateLastRead(Server server)
@@ -171,6 +171,5 @@ namespace Shadowsocks.Controller.Strategy
{ {
//TODO: combine this part of data with ICMP statics //TODO: combine this part of data with ICMP statics
} }

} }
} }

+ 1
- 1
shadowsocks-csharp/Encryption/PolarSSL.cs View File

@@ -30,7 +30,7 @@ namespace Shadowsocks.Encryption
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.ToString());
Logging.LogUsefulException(e);
} }
LoadLibrary(dllPath); LoadLibrary(dllPath);
} }


+ 1
- 1
shadowsocks-csharp/Encryption/Sodium.cs View File

@@ -27,7 +27,7 @@ namespace Shadowsocks.Encryption
} }
catch (Exception e) catch (Exception e)
{ {
Console.WriteLine(e.ToString());
Logging.LogUsefulException(e);
} }
LoadLibrary(dllPath); LoadLibrary(dllPath);
} }


+ 1
- 1
shadowsocks-csharp/Model/Configuration.cs View File

@@ -70,7 +70,7 @@ namespace Shadowsocks.Model
{ {
if (!(e is FileNotFoundException)) if (!(e is FileNotFoundException))
{ {
Console.WriteLine(e);
Logging.LogUsefulException(e);
} }
return new Configuration return new Configuration
{ {


+ 1
- 1
shadowsocks-csharp/Util/Util.cs View File

@@ -21,7 +21,7 @@ namespace Shadowsocks.Util
Directory.CreateDirectory(Application.StartupPath + "\\temp"); Directory.CreateDirectory(Application.StartupPath + "\\temp");
} catch (Exception e) } catch (Exception e)
{ {
Console.WriteLine(e);
Logging.LogUsefulException(e);
} }
// don't use "/", it will fail when we call explorer /select xxx/temp\xxx.log // don't use "/", it will fail when we call explorer /select xxx/temp\xxx.log
return Application.StartupPath + "\\temp"; return Application.StartupPath + "\\temp";


+ 1
- 1
shadowsocks-csharp/View/LogForm.cs View File

@@ -156,7 +156,7 @@ namespace Shadowsocks.View
private void OpenLocationMenuItem_Click(object sender, EventArgs e) private void OpenLocationMenuItem_Click(object sender, EventArgs e)
{ {
string argument = "/select, \"" + filename + "\""; string argument = "/select, \"" + filename + "\"";
Console.WriteLine(argument);
Logging.Debug(argument);
System.Diagnostics.Process.Start("explorer.exe", argument); System.Diagnostics.Process.Start("explorer.exe", argument);
} }


+ 1
- 1
shadowsocks-csharp/View/MenuViewController.cs View File

@@ -231,7 +231,7 @@ namespace Shadowsocks.View
void controller_UpdatePACFromGFWListError(object sender, System.IO.ErrorEventArgs e) void controller_UpdatePACFromGFWListError(object sender, System.IO.ErrorEventArgs e)
{ {
ShowBalloonTip(I18N.GetString("Failed to update PAC file"), e.GetException().Message, ToolTipIcon.Error, 5000); ShowBalloonTip(I18N.GetString("Failed to update PAC file"), e.GetException().Message, ToolTipIcon.Error, 5000);
Logging.LogUsefulException(e.GetException());
Logging.LogUsefulException(e);
} }
void controller_UpdatePACFromGFWListCompleted(object sender, GFWListUpdater.ResultEventArgs e) void controller_UpdatePACFromGFWListCompleted(object sender, GFWListUpdater.ResultEventArgs e)


Loading…
Cancel
Save