[gnome-subtitles/gtk3] Improving logging throughout the app



commit 029ae1f177aa7c681f8448fe0290fb1544132d3a
Author: Pedro Castro <pedro gnomesubtitles org>
Date:   Mon Dec 4 22:04:50 2017 +0000

    Improving logging throughout the app

 gnome-subtitles.csproj                             |    3 +-
 src/GnomeSubtitles/Core/Backup.cs                  |    5 +-
 .../Core/Command/TranslatorCommand.cs              |    5 +-
 src/GnomeSubtitles/Core/Config.cs                  |   23 ++++---
 src/GnomeSubtitles/Core/Document.cs                |    3 -
 src/GnomeSubtitles/Core/SpellLanguages.cs          |    5 +-
 src/GnomeSubtitles/Core/Util.cs                    |    5 +-
 src/GnomeSubtitles/Dialog/FileOpenDialog.cs        |    9 +--
 .../Dialog/Unmanaged/FileOpenErrorDialog.cs        |    5 +-
 .../Dialog/Unmanaged/FileSaveErrorDialog.cs        |    5 +-
 .../Dialog/Unmanaged/VideoErrorDialog.cs           |    5 +-
 src/GnomeSubtitles/Execution/BugReporter.cs        |    5 +-
 src/GnomeSubtitles/Execution/Executable.cs         |   14 ++--
 src/GnomeSubtitles/Ui/MainUi.cs                    |   25 ++++----
 src/GnomeSubtitles/Ui/VideoPreview/Player.cs       |    5 +-
 src/SubLib/Core/SubtitleFactory.cs                 |   18 +++---
 src/SubLib/Core/SubtitleSaver.cs                   |    6 +-
 src/SubLib/IO/FileInputOutput.cs                   |   14 +---
 src/SubLib/IO/Input/SubtitleInput.cs               |   17 +++---
 src/SubLib/IO/Output/VerboseConsole.cs             |   44 -------------
 src/SubLib/Util/Logger.cs                          |   67 ++++++++++++++++++++
 21 files changed, 160 insertions(+), 128 deletions(-)
---
diff --git a/gnome-subtitles.csproj b/gnome-subtitles.csproj
index 6ce1224..7e1f3f0 100644
--- a/gnome-subtitles.csproj
+++ b/gnome-subtitles.csproj
@@ -279,7 +279,6 @@
     <Compile Include="src\SubLib\IO\Input\SubtitleInput.cs" />
     <Compile Include="src\SubLib\IO\Input\SubtitleParser.cs" />
     <Compile Include="src\SubLib\IO\Output\SubtitleOutput.cs" />
-    <Compile Include="src\SubLib\IO\Output\VerboseConsole.cs" />
     <Compile Include="src\SubLib\IO\SubtitleFormats\BuiltInSubtitleFormats.cs" />
     <Compile Include="src\SubLib\IO\SubtitleFormats\SubtitleFormat.cs" />
     <Compile Include="src\SubLib\IO\SubtitleFormats\SubtitleFormatAQTitle.cs" />
@@ -309,9 +308,11 @@
     <Compile Include="src\GnomeSubtitles\Dialog\VideoOpenDialog.cs" />
     <Compile Include="src\GnomeSubtitles\Dialog\FileSaveDialog.cs" />
     <Compile Include="src\GnomeSubtitles\Dialog\FileTranslationSaveDialog.cs" />
+    <Compile Include="src\SubLib\Util\Logger.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ItemGroup>
     <Folder Include="Properties\" />
+    <Folder Include="src\SubLib\Util\" />
   </ItemGroup>
 </Project>
diff --git a/src/GnomeSubtitles/Core/Backup.cs b/src/GnomeSubtitles/Core/Backup.cs
index 8bae72f..8aec429 100644
--- a/src/GnomeSubtitles/Core/Backup.cs
+++ b/src/GnomeSubtitles/Core/Backup.cs
@@ -24,6 +24,7 @@ using GnomeSubtitles.Ui;
 using Gtk;
 using SubLib.Core;
 using SubLib.Core.Domain;
+using SubLib.Util;
 
 namespace GnomeSubtitles.Core {
 
@@ -107,7 +108,7 @@ public class Backup {
                        }
                }
                catch (Exception e) {
-                       Console.Error.WriteLine("Caught exception creating backup files: " + e);
+                       Logger.Error(e, "Caught an exception while creating backup files");
                }
        }
 
@@ -118,7 +119,7 @@ public class Backup {
                        return true;
                }
                catch (Exception e) {
-                       Console.Error.WriteLine("Caught exception saving backup file: " + e);
+                       Logger.Error(e, "Caught an exception while saving backup file");
                        return false;
                }
        }
diff --git a/src/GnomeSubtitles/Core/Command/TranslatorCommand.cs 
b/src/GnomeSubtitles/Core/Command/TranslatorCommand.cs
index 8aedbe7..49bc91d 100644
--- a/src/GnomeSubtitles/Core/Command/TranslatorCommand.cs
+++ b/src/GnomeSubtitles/Core/Command/TranslatorCommand.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2011 Pedro Castro
+ * Copyright (C) 2011-2017 Pedro Castro
  *
  * Gnome Subtitles is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -26,6 +26,7 @@ using SubLib;
 using SubLib.Core;
 using SubLib.Core.Domain;
 using SubLib.Exceptions;
+using SubLib.Util;
 using System;
 
 namespace GnomeSubtitles.Core.Command {
@@ -96,7 +97,7 @@ public class TranslatorCommand : FixedSingleSelectionCommand {
                        return true;
                }
                catch (TranslatorException e) { //TODO know which exceptions are originally thrown. Check if 
it's possible to have the second error message in the application language.
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e);
                        BasicErrorDialog errorDialog = new BasicErrorDialog(Catalog.GetString("Could not 
translate the chosen subtitle."), e.Message);
                        errorDialog.Show();
                        return false;
diff --git a/src/GnomeSubtitles/Core/Config.cs b/src/GnomeSubtitles/Core/Config.cs
index e975d13..1fa5d39 100644
--- a/src/GnomeSubtitles/Core/Config.cs
+++ b/src/GnomeSubtitles/Core/Config.cs
@@ -23,6 +23,9 @@ using System;
 
 //TODO clean commented code
 //TODO implement a runtime variable that allows to run the app without a config schema installed
+using SubLib.Util;
+
+
 namespace GnomeSubtitles.Core {
 
 /* Enumerations */
@@ -104,7 +107,7 @@ public class Config {
                try {
                        settings = new Settings(Schema);
                } catch(Exception e) {
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e, "Config initialization error");
                }
        }
 
@@ -360,7 +363,7 @@ public class Config {
                        return settings.GetString(key);
                }
                catch (Exception e) {
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e);
                        return defaultValue;
                }
        }
@@ -370,7 +373,7 @@ public class Config {
                        return settings.GetBoolean(key);
                }
                catch (Exception e) {
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e);
                        return defaultValue;
                }
        }
@@ -387,7 +390,7 @@ public class Config {
                        return number;
                }
                catch (Exception e) {
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e);
                        return defaultValue;
                }
        }
@@ -401,7 +404,7 @@ public class Config {
                                return strings;
                }
                catch (Exception e) {
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e);
                        return defaultValue;
                }
        }
@@ -423,7 +426,7 @@ public class Config {
                        return (Enum)Enum.Parse(defaultValue.GetType(), stringValue);
                }
                catch (Exception e) {
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e);
                        return defaultValue;
                }
        }
@@ -433,7 +436,7 @@ public class Config {
                        settings.SetStrv(key, values);
                }
                catch (Exception e) {
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e);
                }
 //             if (values.Length == 0) {
 //                     string[] newValues = { String.Empty };
@@ -448,7 +451,7 @@ public class Config {
                        settings.SetString(key, value);
                }
                catch (Exception e) {
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e);
                }
        }
 
@@ -457,7 +460,7 @@ public class Config {
                        settings.SetBoolean(key, value);
                }
                catch (Exception e) {
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e);
                }
        }
 
@@ -466,7 +469,7 @@ public class Config {
                        settings.SetInt(key, value);
                }
                catch (Exception e) {
-                       Console.Error.WriteLine(e);
+                       Logger.Error(e);
                }
        }
 
diff --git a/src/GnomeSubtitles/Core/Document.cs b/src/GnomeSubtitles/Core/Document.cs
index f001e5a..2819e1c 100644
--- a/src/GnomeSubtitles/Core/Document.cs
+++ b/src/GnomeSubtitles/Core/Document.cs
@@ -151,7 +151,6 @@ public class Document {
                        CloseTranslation();
 
                SubtitleFactory factory = new SubtitleFactory();
-               factory.Verbose = true;
                factory.Encoding = encoding;
                factory.FallbackEncoding = GetFallbackEncoding();
 
@@ -201,7 +200,6 @@ public class Document {
        /* Used in the object construction */
        private void New () {
                SubtitleFactory factory = new SubtitleFactory();
-               factory.Verbose = true;
 
                subtitles = new Ui.View.Subtitles(factory.New());
        }
@@ -209,7 +207,6 @@ public class Document {
        /* Used in the object construction */
        private void Open (string path, Encoding encoding) {
                SubtitleFactory factory = new SubtitleFactory();
-               factory.Verbose = true;
                factory.Encoding = encoding;
                factory.FallbackEncoding = GetFallbackEncoding();
                factory.InputFrameRate = Base.Ui.Menus.TimingsInputFrameRateActive;
diff --git a/src/GnomeSubtitles/Core/SpellLanguages.cs b/src/GnomeSubtitles/Core/SpellLanguages.cs
index 417b400..f968577 100644
--- a/src/GnomeSubtitles/Core/SpellLanguages.cs
+++ b/src/GnomeSubtitles/Core/SpellLanguages.cs
@@ -22,6 +22,7 @@ using System;
 using System.Collections;
 using System.Runtime.InteropServices;
 using SubLib.Core.Domain;
+using SubLib.Util;
 
 namespace GnomeSubtitles.Core {
 
@@ -127,7 +128,7 @@ public class SpellLanguages {
                else
                        activeLanguage = languages[index] as SpellLanguage;
 
-               System.Console.WriteLine("[Spellcheck] Setting active language: {0}", (activeLanguage == null 
? "none." : activeLanguage.ID));
+               Logger.Info("[Spellcheck] Setting active language: {0}", (activeLanguage == null ? "none." : 
activeLanguage.ID));
 
                /* Set index variable */
                if (textType == SubtitleTextType.Text)
@@ -173,7 +174,7 @@ public class SpellLanguages {
                enchant_broker_free(broker);
 
                languages.Sort();
-               System.Console.WriteLine("[Spellcheck] Found {0} languages", languages.Count);
+               Logger.Info("[Spellcheck] Found {0} languages", languages.Count);
        }
 
        private void GetActiveLanguagesFromConfig () {
diff --git a/src/GnomeSubtitles/Core/Util.cs b/src/GnomeSubtitles/Core/Util.cs
index 04aed08..1d28e76 100644
--- a/src/GnomeSubtitles/Core/Util.cs
+++ b/src/GnomeSubtitles/Core/Util.cs
@@ -18,11 +18,12 @@
  */
 
 using Gtk;
+using SubLib.Core.Domain;
+using SubLib.Util;
 using System;
 using System.Diagnostics;
 using System.Globalization;
 using System.Threading;
-using SubLib.Core.Domain;
 
 namespace GnomeSubtitles.Core {
 
@@ -141,7 +142,7 @@ public class Util {
                        Process.Start(url);
                }
                catch (Exception e) {
-                       Console.Error.WriteLine("Caught exception when trying to open url [{0}]: {1}", url, 
e);
+                       Logger.Error(e, "Caught exception when trying to open url \"{0}\"", url);
                }
        }
 
diff --git a/src/GnomeSubtitles/Dialog/FileOpenDialog.cs b/src/GnomeSubtitles/Dialog/FileOpenDialog.cs
index d1b864e..6a1525a 100644
--- a/src/GnomeSubtitles/Dialog/FileOpenDialog.cs
+++ b/src/GnomeSubtitles/Dialog/FileOpenDialog.cs
@@ -28,6 +28,7 @@ using System.Collections;
 using System.IO;
 using System.Text;
 using System.Text.RegularExpressions;
+using SubLib.Util;
 
 namespace GnomeSubtitles.Dialog {
 
@@ -166,14 +167,13 @@ public class FileOpenDialog : BaseDialog {
                        folder = dialog.CurrentFolder;
                }
                catch (Exception e) {
-                       System.Console.Error.WriteLine("Caught exception when trying to get the current 
folder:");
-                       System.Console.Error.WriteLine(e);
+                       Logger.Error(e, "Caught exception when trying to get the current folder");
                        SetVideoSelectionSensitivity(false);
                        return;
                }
 
                if ((folder == null) || (folder == String.Empty)) {
-                       System.Console.Error.WriteLine("Error when trying to get the current folder.");
+                       Logger.Error("Error when trying to get the current folder.");
                        SetVideoSelectionSensitivity(false);
                        return;
                }
@@ -210,8 +210,7 @@ public class FileOpenDialog : BaseDialog {
                        filePath = dialog.Filename;
                }
                catch (Exception e) {
-                       System.Console.Error.WriteLine("Caught exception when trying to get the current 
filename:");
-                       System.Console.Error.WriteLine(e);
+                       Logger.Error(e, "Caught exception when trying to get the current filename");
                        SetActiveComboBoxItem(0);
                        return;
                }
diff --git a/src/GnomeSubtitles/Dialog/Unmanaged/FileOpenErrorDialog.cs 
b/src/GnomeSubtitles/Dialog/Unmanaged/FileOpenErrorDialog.cs
index ba45bdf..9b74cd4 100644
--- a/src/GnomeSubtitles/Dialog/Unmanaged/FileOpenErrorDialog.cs
+++ b/src/GnomeSubtitles/Dialog/Unmanaged/FileOpenErrorDialog.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2009 Pedro Castro
+ * Copyright (C) 2006-2017 Pedro Castro
  *
  * Gnome Subtitles is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@
 
 using Gtk;
 using Mono.Unix;
+using SubLib.Util;
 using System;
 
 namespace GnomeSubtitles.Dialog.Unmanaged {
@@ -33,7 +34,7 @@ public abstract class FileOpenErrorDialog : ErrorDialog {
        }
 
        public FileOpenErrorDialog (string filename, Exception exception) {
-               Console.Error.WriteLine("File open error:\n" + exception);
+               Logger.Error(exception, "File open error");
 
                string primaryText = GetPrimaryText(filename);
                string secondaryText = GetSecondaryText(exception);
diff --git a/src/GnomeSubtitles/Dialog/Unmanaged/FileSaveErrorDialog.cs 
b/src/GnomeSubtitles/Dialog/Unmanaged/FileSaveErrorDialog.cs
index 5c11ae3..4d4919a 100644
--- a/src/GnomeSubtitles/Dialog/Unmanaged/FileSaveErrorDialog.cs
+++ b/src/GnomeSubtitles/Dialog/Unmanaged/FileSaveErrorDialog.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2007-2009 Pedro Castro
+ * Copyright (C) 2007-2017 Pedro Castro
  *
  * Gnome Subtitles is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -23,6 +23,7 @@ using SubLib;
 using System;
 using System.IO;
 using System.Security;
+using SubLib.Util;
 
 namespace GnomeSubtitles.Dialog.Unmanaged {
 
@@ -33,7 +34,7 @@ public class FileSaveErrorDialog : ErrorDialog {
        private string actionLabel = Catalog.GetString("Save to another file");
 
        public FileSaveErrorDialog (string filename, Exception exception) {
-               Console.Error.WriteLine("File save error:\n" + exception);
+               Logger.Error(exception, "File save error");
 
                string primaryText = GetPrimaryText(filename);
                string secondaryText = GetSecondaryText(exception);
diff --git a/src/GnomeSubtitles/Dialog/Unmanaged/VideoErrorDialog.cs 
b/src/GnomeSubtitles/Dialog/Unmanaged/VideoErrorDialog.cs
index 14764a0..f07ae5c 100644
--- a/src/GnomeSubtitles/Dialog/Unmanaged/VideoErrorDialog.cs
+++ b/src/GnomeSubtitles/Dialog/Unmanaged/VideoErrorDialog.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2007-2009 Pedro Castro
+ * Copyright (C) 2007-2017 Pedro Castro
  *
  * Gnome Subtitles is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -20,6 +20,7 @@
 using GnomeSubtitles.Ui.VideoPreview.Exceptions;
 using Mono.Unix;
 using System;
+using SubLib.Util;
 
 namespace GnomeSubtitles.Dialog.Unmanaged {
 
@@ -29,7 +30,7 @@ public class VideoErrorDialog : FileOpenErrorDialog {
        private string primaryTextStart = Catalog.GetString("Could not play the file");
 
        public VideoErrorDialog (Uri uri, Exception exception) : base(uri, exception) {
-               Console.Error.WriteLine("Video error: " + exception.ToString());
+               Logger.Error(exception, "Video error");
        }
 
        /* Overriden members */
diff --git a/src/GnomeSubtitles/Execution/BugReporter.cs b/src/GnomeSubtitles/Execution/BugReporter.cs
index 7e1ec72..cbf3e8c 100644
--- a/src/GnomeSubtitles/Execution/BugReporter.cs
+++ b/src/GnomeSubtitles/Execution/BugReporter.cs
@@ -26,6 +26,9 @@ using System.Diagnostics;
 using System.IO;
 
 //FIXME still needed?
+using SubLib.Util;
+
+
 namespace GnomeSubtitles.Execution {
 
 public class BugReporter {
@@ -38,7 +41,7 @@ public class BugReporter {
 
        public static void Report (string message) {
                string bugInfo = GetBugInfo(message);
-               Console.Error.WriteLine(bugInfo);
+               Logger.Error(bugInfo);
 
                try {
                        RunBugBuddy(bugInfo);
diff --git a/src/GnomeSubtitles/Execution/Executable.cs b/src/GnomeSubtitles/Execution/Executable.cs
index 1e18add..a748951 100644
--- a/src/GnomeSubtitles/Execution/Executable.cs
+++ b/src/GnomeSubtitles/Execution/Executable.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of Gnome Subtitles.
- * Copyright (C) 2006-2009 Pedro Castro
+ * Copyright (C) 2006-2017 Pedro Castro
  *
  * Gnome Subtitles is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@
 
 using GLib;
 using GnomeSubtitles.Core;
+using SubLib.Util;
 using System;
 using System.Runtime.InteropServices;
 using System.Text;
@@ -38,12 +39,11 @@ public class Executable {
        public static bool SetProcessNamePrctl (string name) {
                try {
                        if (prctl(15, Encoding.ASCII.GetBytes(name + "\0"), 0, 0, 0) != 0) { // 15 = 
PR_SET_NAME
-                               Console.Error.WriteLine("Error setting process name with prctl: " + 
Mono.Unix.Native.Stdlib.GetLastError());
+                               Logger.Error("Error setting process name with prctl: {0}", 
Mono.Unix.Native.Stdlib.GetLastError());
                        }
                }
                catch (Exception e) {
-               Console.Error.WriteLine("Setting the process name using prctl has thrown an exception:");
-               Console.Error.WriteLine(e);
+                       Logger.Error(e, "Setting the process name using prctl has thrown an exception");
                return false;
         }
         return true;
@@ -54,8 +54,7 @@ public class Executable {
                        setproctitle(Encoding.ASCII.GetBytes("%s\0"), Encoding.ASCII.GetBytes(name + "\0"));
                }
                catch (Exception e) {
-               Console.Error.WriteLine("Setting the process name using setproctitle has thrown an 
exception:");
-               Console.Error.WriteLine(e);
+                       Logger.Error(e, "Setting the process name using setproctitle has thrown an 
exception");
                return false;
         }
         return true;
@@ -95,8 +94,9 @@ public class Executable {
        /* Event members */
 
        private static void OnUnhandledException (UnhandledExceptionArgs args) {
-               if (args.ExceptionObject is Exception)
+               if (args.ExceptionObject is Exception) {
                        BugReporter.Report(args.ExceptionObject as Exception);
+               }
 
                Kill();
        }
diff --git a/src/GnomeSubtitles/Ui/MainUi.cs b/src/GnomeSubtitles/Ui/MainUi.cs
index 81d6241..46983f6 100644
--- a/src/GnomeSubtitles/Ui/MainUi.cs
+++ b/src/GnomeSubtitles/Ui/MainUi.cs
@@ -26,8 +26,9 @@ using GnomeSubtitles.Ui.VideoPreview;
 using GnomeSubtitles.Ui.View;
 using Gtk;
 using Mono.Unix;
-using SubLib.Exceptions;
 using SubLib.Core.Domain;
+using SubLib.Exceptions;
+using SubLib.Util;
 using System;
 using System.IO;
 using System.Text;
@@ -426,8 +427,8 @@ public class MainUi {
                SaveConfirmationDialog dialog = new SaveTranslationOnNewTranslationConfirmationDialog();
                        return dialog.WaitForResponse();
                }
-               else
-               return true;
+
+               return true;
        }
 
        /// <summary>Whether a translation should be opened, after choosing the respective confirmation 
dialog.</summary>
@@ -436,8 +437,8 @@ public class MainUi {
                SaveConfirmationDialog dialog = new SaveTranslationOnOpenConfirmationDialog();
                        return dialog.WaitForResponse();
                }
-               else
-               return true;
+
+               return true;
        }
 
        /// <summary>Whether a translation should be closed, after choosing the respective confirmation 
dialog.</summary>
@@ -446,16 +447,17 @@ public class MainUi {
                SaveConfirmationDialog dialog = new SaveTranslationOnCloseConfirmationDialog();
                        return dialog.WaitForResponse();
                }
-               else
-               return true;
+
+               return true;
        }
 
+       //FIXME use a header bar to show the title and subtitle
        private void UpdateTitleModificationStatus (bool showFilename, bool modified) {
                if (showFilename) {
-                       string prefix = (modified ? "*" : String.Empty);
-                       window.Title = prefix + Base.Document.TextFile.Filename + " - " + 
Base.ExecutionContext.ApplicationName;
+                       window.Title = (modified ? "*" : "") + Base.Document.TextFile.Filename;
+               } else {
+                       window.Title = Base.ExecutionContext.ApplicationName;
                }
-               else window.Title = Base.ExecutionContext.ApplicationName;
        }
 
        private int GetFileOpenCodePageFromConfig () {
@@ -485,8 +487,7 @@ public class MainUi {
                                file = Path.GetFullPath(file);
                        }
                        catch (Exception e) {
-                               System.Console.Error.WriteLine("Unable to read subtitle file path from 
argument 0");
-                               System.Console.Error.WriteLine(e);
+                               Logger.Error(e, "Unable to read subtitle file path from argument 0");
                                return null;
                        }
                }
diff --git a/src/GnomeSubtitles/Ui/VideoPreview/Player.cs b/src/GnomeSubtitles/Ui/VideoPreview/Player.cs
index b65001e..a61d1a6 100644
--- a/src/GnomeSubtitles/Ui/VideoPreview/Player.cs
+++ b/src/GnomeSubtitles/Ui/VideoPreview/Player.cs
@@ -23,6 +23,7 @@ using GStreamer;
 using Gtk;
 using SubLib.Core.Domain;
 using System;
+using SubLib.Util;
 
 namespace GnomeSubtitles.Ui.VideoPreview {
 
@@ -252,7 +253,7 @@ public class Player {
        }
 
        private void OnPlaybinFoundVideoInfo (VideoInfoEventArgs args) {
-               Console.WriteLine("[Player] Media info: " + args.VideoInfo.ToString());
+               Logger.Info("[Player] Media info: {0}", args.VideoInfo.ToString());
                this.videoInfo = args.VideoInfo;
 
                /* Set defaults if there is no video */
@@ -270,7 +271,7 @@ public class Player {
        private void OnPlaybinFoundTag (TagEventArgs args) {
                if ((!hasFoundDuration) && (FoundDuration != null) && (playbin.Duration != TimeSpan.Zero)) {
                        TimeSpan duration = playbin.Duration;
-                       Console.WriteLine("[Player] Media duration: " + duration);
+                       Logger.Info("[Player] Media duration: {0}", duration);
 
                        hasFoundDuration = true;
                        FoundDuration(duration);
diff --git a/src/SubLib/Core/SubtitleFactory.cs b/src/SubLib/Core/SubtitleFactory.cs
index 95ee430..bc52d68 100644
--- a/src/SubLib/Core/SubtitleFactory.cs
+++ b/src/SubLib/Core/SubtitleFactory.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of SubLib.
- * Copyright (C) 2005-2008,2011 Pedro Castro
+ * Copyright (C) 2005-2017 Pedro Castro
  *
  * SubLib is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -21,7 +21,7 @@ using SubLib.Core.Domain;
 using SubLib.Exceptions;
 using SubLib.IO.SubtitleFormats;
 using SubLib.IO.Input;
-using SubLib.IO.Output;
+using SubLib.Util;
 using System;
 using System.IO;
 using System.Text;
@@ -60,10 +60,10 @@ public class SubtitleFactory {
 
        /// <summary>Whether to enable the library to print messages to the console.</summary>
        /// <remarks>Messages will be shown along with the main methods of <see cref="SubtitleFactory" />
-       /// and <see cref="Subtitles" />. The default value is false.</remarks>
-       public bool Verbose {
-               get { return VerboseConsole.Verbose; }
-               set { VerboseConsole.Verbose = value; }
+       /// and <see cref="Subtitles" />. The default value is true.</remarks>
+       public bool LoggingEnabled {
+               get { return Logger.Enabled; }
+               set { Logger.Enabled = value; }
        }
 
        /// <summary>Whether to detect and store incomplete subtitles found upon open.</summary>
@@ -191,7 +191,9 @@ public class SubtitleFactory {
 
                fileProperties = new FileProperties(path, fileEncoding, format.Type, 
parsingProperties.TimingMode);
 
-               VerboseConsole.WriteLine("[*] Opened \"" + path + "\" with encoding \"" + fileEncoding + "\", 
format \"" + format.Name + "\", timing mode \"" + parsingProperties.TimingMode + "\" and frame rate \"" + 
subtitleProperties.CurrentFrameRate + "\" (input frame rate was \"" + inputFrameRate + "\")");
+               Logger.Info("[SubtitleFactory] Opened \"{0}\" with encoding \"{1}\", format \"{2}\", timing 
mode \"{3}\" and frame rate \"{4}\" (input frame rate was \"{5}\")",
+                       path, fileEncoding, format.Name, parsingProperties.TimingMode, 
subtitleProperties.CurrentFrameRate, inputFrameRate);
+
                return subtitles;
        }
 
@@ -208,7 +210,7 @@ public class SubtitleFactory {
 
                fileProperties = new FileProperties(path, fileEncoding, parsingProperties.TimingMode);
 
-               VerboseConsole.WriteLine("[*] Opened " + path + " with encoding " + fileEncoding);
+               Logger.Info("[SubtitleFactory] Opened {0} with encoding {1}", path, fileEncoding);
                return subtitles;
        }
 
diff --git a/src/SubLib/Core/SubtitleSaver.cs b/src/SubLib/Core/SubtitleSaver.cs
index 0d833ca..c5bdb4d 100644
--- a/src/SubLib/Core/SubtitleSaver.cs
+++ b/src/SubLib/Core/SubtitleSaver.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of SubLib.
- * Copyright (C) 2005-2008 Pedro Castro
+ * Copyright (C) 2005-2017 Pedro Castro
  *
  * SubLib is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -21,6 +21,7 @@ using SubLib.Core.Domain;
 using SubLib.IO;
 using SubLib.IO.Output;
 using SubLib.IO.SubtitleFormats;
+using SubLib.Util;
 
 namespace SubLib.Core {
 
@@ -54,7 +55,8 @@ public class SubtitleSaver {
                FileInputOutput.WriteFile(properties.Path, text, properties.Encoding);
 
                fileProperties = GetUpdatedFileProperties(properties);
-               VerboseConsole.WriteLine("[*] Saved " + textType + " \"" + properties.Path + "\" with 
encoding \"" + properties.Encoding + "\", format \"" + format.Name + "\" and frame rate \"" + 
subtitles.Properties.CurrentFrameRate + "\"");
+               Logger.Info("[SubtitleSaver] Saved {0} \"{1}\" with encoding \"{2}\", format \"{3}\" and 
frame rate \"{4}\"",
+                       textType, properties.Path, properties.Encoding, format.Name, 
subtitles.Properties.CurrentFrameRate);
        }
 
        /* Private methods */
diff --git a/src/SubLib/IO/FileInputOutput.cs b/src/SubLib/IO/FileInputOutput.cs
index beacaa1..8fef4d6 100644
--- a/src/SubLib/IO/FileInputOutput.cs
+++ b/src/SubLib/IO/FileInputOutput.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of SubLib.
- * Copyright (C) 2005-2008 Pedro Castro
+ * Copyright (C) 2005-2017 Pedro Castro
  *
  * SubLib is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,7 +17,7 @@
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  */
 
-using SubLib.IO.Output;
+using SubLib.Util;
 using System;
 using System.Collections;
 using System.IO;
@@ -56,7 +56,7 @@ internal class FileInputOutput {
                stream.Seek(0, SeekOrigin.Begin);
 
                string[] detectedEncodings = detector.getProbableCharsets();
-               VerboseConsole.WriteLine(DetectedEncodingsToString(detectedEncodings));
+               Logger.Info("[FileInputOutput] Detected encodings: {0}", String.Join(", ", 
detectedEncodings));
 
                /* Check if no encoding was detected */
                if (detectedEncodings[0] == "nomatch")
@@ -144,14 +144,6 @@ internal class FileInputOutput {
                }
        }
 
-       private static string DetectedEncodingsToString (string[] detectedEncodings) {
-               string result = "Detected encodings:";
-               foreach (string encoding in detectedEncodings) {
-                       result += " " + encoding;
-               }
-               return result;
-       }
-
        /// <summary>Replaces the occurrences of Windows and Mac newline chars with unix newline.</summary>
        private static string ConvertNewLinesToUnix (string text) {
                text = text.Replace("\r\n", "\n"); //Replace Windows newline
diff --git a/src/SubLib/IO/Input/SubtitleInput.cs b/src/SubLib/IO/Input/SubtitleInput.cs
index 6461d4d..cc0c22d 100644
--- a/src/SubLib/IO/Input/SubtitleInput.cs
+++ b/src/SubLib/IO/Input/SubtitleInput.cs
@@ -1,6 +1,6 @@
 /*
  * This file is part of SubLib.
- * Copyright (C) 2005-2008 Pedro Castro
+ * Copyright (C) 2005-2017 Pedro Castro
  *
  * SubLib is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,8 +19,8 @@
 
 using SubLib.Core.Domain;
 using SubLib.Exceptions;
-using SubLib.IO.Output;
 using SubLib.IO.SubtitleFormats;
+using SubLib.Util;
 using System;
 using System.IO;
 using System.Text;
@@ -91,7 +91,7 @@ internal class SubtitleInput {
 
                /* Check if no codepage was detected */
                if (codePages.Length == 0) {
-                       VerboseConsole.WriteLine("No encoding was automatically detected. Using the fall-back 
encoding \"" + fallbackEncoding.WebName + "\"");
+                       Logger.Info("[SubtitleInput] No encoding was automatically detected. Using the 
fall-back encoding \"{0}\"", fallbackEncoding.WebName);
                        string text;
                        if (isSubtitleFile)
                                text = TestEncoding(fileStream, fallbackEncoding, out usedFormat);
@@ -183,7 +183,7 @@ internal class SubtitleInput {
        }
 
        private string TestEncoding (FileStream fileStream, Encoding encoding) {
-               VerboseConsole.WriteLine("Trying the encoding \"" + encoding.WebName + "\"");
+               Logger.Info("[SubtitleInput] Trying encoding \"{0}\"", encoding.WebName);
                /* Get the text */
                string text = FileInputOutput.ReadFile(fileStream, encoding, true);
 
@@ -192,10 +192,11 @@ internal class SubtitleInput {
 
        /// <exception cref="UnknownSubtitleFormatException">Thrown if the subtitle format could not be 
detected.</exception>
        private SubtitleFormat GetSubtitleFormat (string text) {
-               if (subtitleType == SubtitleType.Unknown)
-                       VerboseConsole.WriteLine("Trying to autodetect the subtitle format.");
-               else
-                       VerboseConsole.WriteLine("Trying the subtitle format \"" + subtitleType + "\"");
+               if (subtitleType == SubtitleType.Unknown) {
+                       Logger.Info("[SubtitleInput] Trying to autodetect the subtitle format.");
+               } else {
+                       Logger.Info("[SubtitleInput] Trying subtitle format \"{0}\"", subtitleType);
+               }
 
                SubtitleFormat subtitleFormat = null;
                if (subtitleType == SubtitleType.Unknown)
diff --git a/src/SubLib/Util/Logger.cs b/src/SubLib/Util/Logger.cs
new file mode 100644
index 0000000..63acc9c
--- /dev/null
+++ b/src/SubLib/Util/Logger.cs
@@ -0,0 +1,67 @@
+/*
+ * This file is part of SubLib.
+ * Copyright (C) 2006-2017 Pedro Castro
+ *
+ * SubLib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * SubLib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+using System;
+using System.IO;
+
+namespace SubLib.Util {
+
+public class Logger {
+       private enum LogType { Info, Error };
+
+       public static bool Enabled { get; set; } = true;
+
+       public static void Info (string message, params object[] args) {
+               if (Enabled) {
+                       WriteLine(LogType.Info, message, args);
+               }
+       }
+
+       public static void Error (string message, params object[] args) {
+               if (Enabled) {
+                       WriteLine(LogType.Error, message, args);
+               }
+       }
+
+       /// <summary>
+       /// Same as Error(message, args) but prints an exception after the error message.
+       /// </summary>
+       /// <param name="exception">Exception.</param>
+       /// <param name="message">Error message.</param>
+       /// <param name="args">Error message arguments.</param>
+       public static void Error (Exception exception, string message, params object[] args) {
+               Error(message + "\n" + exception.ToString(), args);
+       }
+
+       public static void Error (Exception exception) {
+               Error(exception.ToString());
+       }
+
+
+       private static void WriteLine (LogType type, string message, params object[] args) {
+               TextWriter writer = (type == LogType.Info ? Console.Out : Console.Error);
+               writer.WriteLine(DateTime.Now.ToString("HH:mm:ss.f") + " "
+                       + type.ToString().ToUpper() + " "
+                       + message,
+                       args);
+       }
+
+}
+
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]