From 0c3905e58a3d41c74af026a0785f8353daa67e42 Mon Sep 17 00:00:00 2001 From: Hylke Bons Date: Thu, 22 Nov 2012 12:31:48 +0000 Subject: [PATCH] fetcher: log exceptions more cleanly --- SparkleLib/Git/SparkleFetcherGit.cs | 2 +- SparkleLib/Git/SparkleRepoGit.cs | 14 +++----------- SparkleLib/SparkleExtensions.cs | 2 +- SparkleLib/SparkleFetcherBase.cs | 29 +++++++++++------------------ SparkleLib/SparkleLogger.cs | 9 +++++++++ SparkleShare/Mac/SparkleUI.cs | 2 +- 6 files changed, 26 insertions(+), 32 deletions(-) diff --git a/SparkleLib/Git/SparkleFetcherGit.cs b/SparkleLib/Git/SparkleFetcherGit.cs index 4cf2ddeb..4ab3b6ea 100755 --- a/SparkleLib/Git/SparkleFetcherGit.cs +++ b/SparkleLib/Git/SparkleFetcherGit.cs @@ -294,7 +294,7 @@ namespace SparkleLib.Git { } } catch (Exception e) { - SparkleLogger.LogInfo ("Fetcher", "Failed to dispose properly: " + e.Message); + SparkleLogger.LogInfo ("Fetcher", "Failed to dispose properly", e); } } diff --git a/SparkleLib/Git/SparkleRepoGit.cs b/SparkleLib/Git/SparkleRepoGit.cs index bcb17fa2..75c84366 100644 --- a/SparkleLib/Git/SparkleRepoGit.cs +++ b/SparkleLib/Git/SparkleRepoGit.cs @@ -411,8 +411,6 @@ namespace SparkleLib.Git { { SparkleGit git = new SparkleGit (LocalPath, "add --all"); git.StartAndWaitForExit (); - - SparkleLogger.LogInfo ("Git", Name + " | Changes staged"); } @@ -533,9 +531,7 @@ namespace SparkleLib.Git { SparkleLogger.LogInfo ("Git", Name + " | Conflict type: " + line); // Ignore conflicts in the .sparkleshare file and use the local version - if (conflicting_path.EndsWith (".sparkleshare") || - conflicting_path.EndsWith (".empty")) { - + if (conflicting_path.EndsWith (".sparkleshare") || conflicting_path.EndsWith (".empty")) { // Recover local version SparkleGit git_theirs = new SparkleGit (LocalPath, "checkout --theirs \"" + conflicting_path + "\""); git_theirs.StartAndWaitForExit (); @@ -574,12 +570,8 @@ namespace SparkleLib.Git { // The local version has been modified, but the server version was removed } else if (line.StartsWith ("DU")) { - - // The modified local version is already in the - // checkout, so it just needs to be added. - // - // We need to specifically mention the file, so - // we can't reuse the Add () method + // The modified local version is already in the checkout, so it just needs to be added. + // We need to specifically mention the file, so we can't reuse the Add () method SparkleGit git_add = new SparkleGit (LocalPath, "add \"" + conflicting_path + "\""); git_add.StartAndWaitForExit (); diff --git a/SparkleLib/SparkleExtensions.cs b/SparkleLib/SparkleExtensions.cs index a5164288..fb39aa75 100755 --- a/SparkleLib/SparkleExtensions.cs +++ b/SparkleLib/SparkleExtensions.cs @@ -68,7 +68,7 @@ namespace SparkleLib { else if (byte_count >= 1024) return String.Format ("{0:##.##} ᴋʙ", Math.Round (byte_count / 1024, 0)); else - return byte_count.ToString () + " bytes"; + return byte_count.ToString () + " ʙ"; } } } diff --git a/SparkleLib/SparkleFetcherBase.cs b/SparkleLib/SparkleFetcherBase.cs index 5df9b736..173b8b85 100755 --- a/SparkleLib/SparkleFetcherBase.cs +++ b/SparkleLib/SparkleFetcherBase.cs @@ -162,8 +162,8 @@ namespace SparkleLib { IsActive = false; - bool repo_is_encrypted = - (RemoteUrl.AbsolutePath.Contains ("-crypto") || RemoteUrl.Host.Equals ("sparkleshare.net")); + bool repo_is_encrypted = (RemoteUrl.AbsolutePath.Contains ("-crypto") || + RemoteUrl.Host.Equals ("sparkleshare.net")); Finished (repo_is_encrypted, IsFetchedRepoEmpty, Warnings); @@ -257,29 +257,22 @@ namespace SparkleLib { private string FetchHostKey () { - string host = RemoteUrl.Host; - int port = RemoteUrl.Port; - - if (port < 1) - port = 22; - - SparkleLogger.LogInfo ("Auth", "Fetching host key for " + host); - - Process process = new Process () { - EnableRaisingEvents = true - }; + SparkleLogger.LogInfo ("Auth", "Fetching host key for " + RemoteUrl.Host); + Process process = new Process (); process.StartInfo.FileName = "ssh-keyscan"; - process.StartInfo.Arguments = "-t rsa -p " + port + " " + host; process.StartInfo.WorkingDirectory = SparkleConfig.DefaultConfig.TmpPath; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.CreateNoWindow = true; + process.EnableRaisingEvents = true; + + if (RemoteUrl.Port < 1) + process.StartInfo.Arguments = "-t rsa -p 22 " + RemoteUrl.Host; + else + process.StartInfo.Arguments = "-t rsa -p " + RemoteUrl.Port + " " + RemoteUrl.Host; process.Start (); - - // Reading the standard output HAS to go before - // WaitForExit, or it will hang forever on output > 4096 bytes string host_key = process.StandardOutput.ReadToEnd ().Trim (); process.WaitForExit (); @@ -302,7 +295,7 @@ namespace SparkleLib { return fingerprint.ToLower ().Replace ("-", ":"); } catch (Exception e) { - SparkleLogger.LogInfo ("Fetcher", "Failed creating fingerprint: " + e.Message + e.StackTrace); + SparkleLogger.LogInfo ("Fetcher", "Failed creating fingerprint: " + e.Message + " " + e.StackTrace); return null; } } diff --git a/SparkleLib/SparkleLogger.cs b/SparkleLib/SparkleLogger.cs index 6e3bff18..68889ede 100755 --- a/SparkleLib/SparkleLogger.cs +++ b/SparkleLib/SparkleLogger.cs @@ -26,10 +26,19 @@ namespace SparkleLib { private static int log_size = 0; public static void LogInfo (string type, string message) + { + LogInfo (type, message, null); + } + + + public static void LogInfo (string type, string message, Exception exception) { string timestamp = DateTime.Now.ToString ("HH:mm:ss"); string line = timestamp + " | " + type + " | " + message; + if (exception != null) + line += ": " + exception.Message + " " + exception.StackTrace; + if (SparkleConfig.DebugMode) Console.WriteLine (line); diff --git a/SparkleShare/Mac/SparkleUI.cs b/SparkleShare/Mac/SparkleUI.cs index d8739f30..97d75be6 100755 --- a/SparkleShare/Mac/SparkleUI.cs +++ b/SparkleShare/Mac/SparkleUI.cs @@ -77,7 +77,7 @@ namespace SparkleShare { private void HideDockIcon () { - // Currently not supported, here for completeness sake (see Apple's docs) + // Currently not supported by Apple's API // NSApplication.SharedApplication.ActivationPolicy = NSApplicationActivationPolicy.None; }