From 0a733c61b8de38378dcc6d15605d9146ee15a381 Mon Sep 17 00:00:00 2001 From: kimw <1@kimwong.me> Date: Mon, 14 Sep 2015 14:58:46 +0800 Subject: [PATCH] fixed: standardize Logging usage 1. Add Logging.Info() 2. Console.WriteLine() -> Logging.Info() 3. Console.WriteLine() in exceptions -> Logging.LogUsefulException() --- shadowsocks-csharp/Controller/FileManager.cs | 5 ++--- shadowsocks-csharp/Controller/Logging.cs | 8 ++++++-- .../Service/AvailabilityStatistics.cs | 2 +- .../Controller/Service/Listener.cs | 6 +++--- .../Controller/Service/PACServer.cs | 8 ++++---- .../Controller/Service/PolipoRunner.cs | 4 ++-- .../Controller/Service/PortForwarder.cs | 2 +- .../Controller/Service/TCPRelay.cs | 19 +++++++++---------- .../Strategy/HighAvailabilityStrategy.cs | 10 +++++----- .../SimplyChooseByStatisticsStrategy.cs | 7 +++---- shadowsocks-csharp/Encryption/PolarSSL.cs | 2 +- shadowsocks-csharp/Encryption/Sodium.cs | 2 +- shadowsocks-csharp/Model/Configuration.cs | 2 +- shadowsocks-csharp/Util/Util.cs | 2 +- shadowsocks-csharp/View/LogForm.cs | 2 +- shadowsocks-csharp/View/MenuViewController.cs | 2 +- 16 files changed, 42 insertions(+), 41 deletions(-) diff --git a/shadowsocks-csharp/Controller/FileManager.cs b/shadowsocks-csharp/Controller/FileManager.cs index cc8f3bf9..0dac0224 100755 --- a/shadowsocks-csharp/Controller/FileManager.cs +++ b/shadowsocks-csharp/Controller/FileManager.cs @@ -19,10 +19,9 @@ namespace Shadowsocks.Controller _FileStream.Close(); return true; } - catch (Exception _Exception) + catch (Exception e) { - Console.WriteLine("Exception caught in process: {0}", - _Exception.ToString()); + Logging.LogUsefulException(e); } return false; } diff --git a/shadowsocks-csharp/Controller/Logging.cs b/shadowsocks-csharp/Controller/Logging.cs index 2ef247a6..49fdde34 100755 --- a/shadowsocks-csharp/Controller/Logging.cs +++ b/shadowsocks-csharp/Controller/Logging.cs @@ -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 - Console.WriteLine(o); + Console.WriteLine("[D] " + o); #endif } diff --git a/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs b/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs index 08125e92..494e1afc 100644 --- a/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs +++ b/shadowsocks-csharp/Controller/Service/AvailabilityStatistics.cs @@ -81,7 +81,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine($"An exception occured when eveluating {server.FriendlyName()}"); + Logging.Info($"An exception occured while eveluating {server.FriendlyName()}"); Logging.LogUsefulException(e); } } diff --git a/shadowsocks-csharp/Controller/Service/Listener.cs b/shadowsocks-csharp/Controller/Service/Listener.cs index fe59fe98..06994787 100644 --- a/shadowsocks-csharp/Controller/Service/Listener.cs +++ b/shadowsocks-csharp/Controller/Service/Listener.cs @@ -78,7 +78,7 @@ namespace Shadowsocks.Controller _tcpSocket.Listen(1024); // Start an asynchronous socket to listen for connections. - Console.WriteLine("Shadowsocks started"); + Logging.Info("Shadowsocks started"); _tcpSocket.BeginAccept( new AsyncCallback(AcceptCallback), _tcpSocket); @@ -163,7 +163,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e); + Logging.LogUsefulException(e); } finally { @@ -208,7 +208,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e); + Logging.LogUsefulException(e); conn.Close(); } } diff --git a/shadowsocks-csharp/Controller/Service/PACServer.cs b/shadowsocks-csharp/Controller/Service/PACServer.cs index a4aa33b6..6f8fa414 100644 --- a/shadowsocks-csharp/Controller/Service/PACServer.cs +++ b/shadowsocks-csharp/Controller/Service/PACServer.cs @@ -153,7 +153,7 @@ Connection: Close } catch (Exception e) { - Console.WriteLine(e); + Logging.LogUsefulException(e); socket.Close(); } } @@ -217,7 +217,7 @@ Connection: Close { 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()); } @@ -236,7 +236,7 @@ Connection: Close { 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()); } //lastly we update the last write time in the hashtable @@ -259,7 +259,7 @@ Connection: Close //} //catch (Exception e) //{ - // Console.WriteLine(e); + // Logging.LogUsefulException(e); //} return (useSocks ? "SOCKS5 " : "PROXY ") + localEndPoint.Address + ":" + this._config.localPort + ";"; } diff --git a/shadowsocks-csharp/Controller/Service/PolipoRunner.cs b/shadowsocks-csharp/Controller/Service/PolipoRunner.cs index 5b784bcd..ad582d1e 100644 --- a/shadowsocks-csharp/Controller/Service/PolipoRunner.cs +++ b/shadowsocks-csharp/Controller/Service/PolipoRunner.cs @@ -56,7 +56,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.ToString()); + Logging.LogUsefulException(e); } } string polipoConfig = Resources.privoxy_conf; @@ -94,7 +94,7 @@ namespace Shadowsocks.Controller } catch (Exception e) { - Console.WriteLine(e.ToString()); + Logging.LogUsefulException(e); } _process = null; } diff --git a/shadowsocks-csharp/Controller/Service/PortForwarder.cs b/shadowsocks-csharp/Controller/Service/PortForwarder.cs index 63848079..46f2cf10 100644 --- a/shadowsocks-csharp/Controller/Service/PortForwarder.cs +++ b/shadowsocks-csharp/Controller/Service/PortForwarder.cs @@ -140,7 +140,7 @@ namespace Shadowsocks.Controller } else { - //Console.WriteLine("bytesRead: " + bytesRead.ToString()); + Logging.Debug($"bytes read: {bytesRead}"); _local.Shutdown(SocketShutdown.Send); _localShutdown = true; CheckClose(); diff --git a/shadowsocks-csharp/Controller/Service/TCPRelay.cs b/shadowsocks-csharp/Controller/Service/TCPRelay.cs index bf0497d4..2bb0eb98 100644 --- a/shadowsocks-csharp/Controller/Service/TCPRelay.cs +++ b/shadowsocks-csharp/Controller/Service/TCPRelay.cs @@ -49,7 +49,7 @@ namespace Shadowsocks.Controller lock (this.Handlers) { this.Handlers.Add(handler); - Logging.Debug($"connections: {Handlers.Count}"); + Logging.Debug($"TCP connections: {Handlers.Count}"); DateTime now = DateTime.Now; if (now - _lastSweepTime > TimeSpan.FromSeconds(1)) { @@ -65,10 +65,10 @@ namespace Shadowsocks.Controller } foreach (Handler handler1 in handlersToClose) { - Logging.Debug("Closing timed out connection"); + Logging.Debug("Closing timed out TCP connection."); handler1.Close(); } - return true; + return true; } } @@ -148,7 +148,7 @@ namespace Shadowsocks.Controller { lock (relay.Handlers) { - Logging.Debug($"connections: {relay.Handlers.Count}"); + Logging.Debug($"TCP connections: {relay.Handlers.Count}"); relay.Handlers.Remove(this); } lock (this) @@ -212,7 +212,7 @@ namespace Shadowsocks.Controller { // reject socks 4 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); } @@ -280,7 +280,7 @@ namespace Shadowsocks.Controller } else { - Console.WriteLine("failed to recv data in handshakeReceive2Callback"); + Logging.Info("failed to recv data in handshakeReceive2Callback"); this.Close(); } } @@ -423,7 +423,7 @@ namespace Shadowsocks.Controller { strategy.SetFailure(server); } - Console.WriteLine(String.Format("{0} timed out", server.FriendlyName())); + Logging.Info($"{server.FriendlyName()} timed out"); remote.Close(); RetryConnect(); } @@ -462,8 +462,7 @@ namespace Shadowsocks.Controller connected = true; - //Console.WriteLine("Socket connected to {0}", - // remote.RemoteEndPoint.ToString()); + Logging.Debug($"Socket connected to {remote.RemoteEndPoint}"); var latency = DateTime.Now - _startConnectTime; IStrategy strategy = controller.GetCurrentStrategy(); @@ -545,7 +544,7 @@ namespace Shadowsocks.Controller } else { - //Console.WriteLine("bytesRead: " + bytesRead.ToString()); + Logging.Debug($"bytes read: {bytesRead}"); connection.Shutdown(SocketShutdown.Send); connectionShutdown = true; CheckClose(); diff --git a/shadowsocks-csharp/Controller/Strategy/HighAvailabilityStrategy.cs b/shadowsocks-csharp/Controller/Strategy/HighAvailabilityStrategy.cs index fa05a0c1..ba25ee18 100644 --- a/shadowsocks-csharp/Controller/Strategy/HighAvailabilityStrategy.cs +++ b/shadowsocks-csharp/Controller/Strategy/HighAvailabilityStrategy.cs @@ -132,14 +132,14 @@ namespace Shadowsocks.Controller.Strategy if (_currentServer == null || max.score - _currentServer.score > 200) { _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) { - Logging.Debug(String.Format("latency: {0} {1}", server.FriendlyName(), latency)); + Logging.Debug($"latency: {server.FriendlyName()} {latency}"); ServerStatus status; if (_serverStatus.TryGetValue(server, out status)) @@ -151,7 +151,7 @@ namespace Shadowsocks.Controller.Strategy public void UpdateLastRead(Model.Server server) { - Logging.Debug(String.Format("last read: {0}", server.FriendlyName())); + Logging.Debug($"last read: {server.FriendlyName()}"); ServerStatus status; if (_serverStatus.TryGetValue(server, out status)) @@ -162,7 +162,7 @@ namespace Shadowsocks.Controller.Strategy public void UpdateLastWrite(Model.Server server) { - Logging.Debug(String.Format("last write: {0}", server.FriendlyName())); + Logging.Debug($"last write: {server.FriendlyName()}"); ServerStatus status; if (_serverStatus.TryGetValue(server, out status)) @@ -173,7 +173,7 @@ namespace Shadowsocks.Controller.Strategy public void SetFailure(Model.Server server) { - Logging.Debug(String.Format("failure: {0}", server.FriendlyName())); + Logging.Debug($"failure: {server.FriendlyName()}"); ServerStatus status; if (_serverStatus.TryGetValue(server, out status)) diff --git a/shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs b/shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs index 0cdfcfc5..ab684256 100644 --- a/shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs +++ b/shadowsocks-csharp/Controller/Strategy/SimplyChooseByStatisticsStrategy.cs @@ -47,7 +47,7 @@ namespace Shadowsocks.Controller.Strategy try { 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) .Skip(1) 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 { - 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; } @@ -154,7 +154,7 @@ namespace Shadowsocks.Controller.Strategy public void SetFailure(Server server) { - Logging.Debug(String.Format("failure: {0}", server.FriendlyName())); + Logging.Debug($"failure: {server.FriendlyName()}"); } public void UpdateLastRead(Server server) @@ -171,6 +171,5 @@ namespace Shadowsocks.Controller.Strategy { //TODO: combine this part of data with ICMP statics } - } } diff --git a/shadowsocks-csharp/Encryption/PolarSSL.cs b/shadowsocks-csharp/Encryption/PolarSSL.cs index 42ce5bf7..3c1d8831 100755 --- a/shadowsocks-csharp/Encryption/PolarSSL.cs +++ b/shadowsocks-csharp/Encryption/PolarSSL.cs @@ -30,7 +30,7 @@ namespace Shadowsocks.Encryption } catch (Exception e) { - Console.WriteLine(e.ToString()); + Logging.LogUsefulException(e); } LoadLibrary(dllPath); } diff --git a/shadowsocks-csharp/Encryption/Sodium.cs b/shadowsocks-csharp/Encryption/Sodium.cs index 564aaeda..9580dea5 100755 --- a/shadowsocks-csharp/Encryption/Sodium.cs +++ b/shadowsocks-csharp/Encryption/Sodium.cs @@ -27,7 +27,7 @@ namespace Shadowsocks.Encryption } catch (Exception e) { - Console.WriteLine(e.ToString()); + Logging.LogUsefulException(e); } LoadLibrary(dllPath); } diff --git a/shadowsocks-csharp/Model/Configuration.cs b/shadowsocks-csharp/Model/Configuration.cs index d9de24f5..74d40f0b 100755 --- a/shadowsocks-csharp/Model/Configuration.cs +++ b/shadowsocks-csharp/Model/Configuration.cs @@ -70,7 +70,7 @@ namespace Shadowsocks.Model { if (!(e is FileNotFoundException)) { - Console.WriteLine(e); + Logging.LogUsefulException(e); } return new Configuration { diff --git a/shadowsocks-csharp/Util/Util.cs b/shadowsocks-csharp/Util/Util.cs index 1858ee90..219978b2 100755 --- a/shadowsocks-csharp/Util/Util.cs +++ b/shadowsocks-csharp/Util/Util.cs @@ -21,7 +21,7 @@ namespace Shadowsocks.Util Directory.CreateDirectory(Application.StartupPath + "\\temp"); } catch (Exception e) { - Console.WriteLine(e); + Logging.LogUsefulException(e); } // don't use "/", it will fail when we call explorer /select xxx/temp\xxx.log return Application.StartupPath + "\\temp"; diff --git a/shadowsocks-csharp/View/LogForm.cs b/shadowsocks-csharp/View/LogForm.cs index 907783bb..fe12c5a1 100644 --- a/shadowsocks-csharp/View/LogForm.cs +++ b/shadowsocks-csharp/View/LogForm.cs @@ -156,7 +156,7 @@ namespace Shadowsocks.View private void OpenLocationMenuItem_Click(object sender, EventArgs e) { string argument = "/select, \"" + filename + "\""; - Console.WriteLine(argument); + Logging.Debug(argument); System.Diagnostics.Process.Start("explorer.exe", argument); } diff --git a/shadowsocks-csharp/View/MenuViewController.cs b/shadowsocks-csharp/View/MenuViewController.cs index 62f0a88d..0c581c13 100755 --- a/shadowsocks-csharp/View/MenuViewController.cs +++ b/shadowsocks-csharp/View/MenuViewController.cs @@ -231,7 +231,7 @@ namespace Shadowsocks.View void controller_UpdatePACFromGFWListError(object sender, System.IO.ErrorEventArgs e) { 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)