Browse Source

Add setting to disable logging

Disabling logging was often requested by users, so here it is.

This still creates a log file at start and writes some stuff to it, even
if logging is completely disabled. The log level is set at a later stage
because I don't want to do all of the initialization (including config
file loading) without logging.
Vincent Lang 9 years ago
parent
commit
8eca83f254
2 changed files with 39 additions and 0 deletions
  1. 14 0
      resources/settings/settings_description.json
  2. 25 0
      src/main.cpp

+ 14 - 0
resources/settings/settings_description.json

@@ -59,6 +59,20 @@
       {
         "value": "disablemouse",
         "default": false
+      },
+      {
+        "value": "logLevel",
+        "default": "debug",
+        "hidden": true,
+        "possible_values": [
+          [ "trace", "trace" ],
+          [ "debug", "debug" ],
+          [ "info", "info" ],
+          [ "warn", "warn" ],
+          [ "error", "error" ],
+          [ "fatal", "fatal" ],
+          [ "disable", "disable" ]
+        ]
       }
     ]
   },

+ 25 - 0
src/main.cpp

@@ -106,6 +106,29 @@ void initLogger()
   qInstallMessageHandler(qtMessageOutput);
 }
 
+static QsLogging::Level logLevelFromString(const QString& str)
+{
+  if (str == "trace")     return QsLogging::Level::TraceLevel;
+  if (str == "debug")     return QsLogging::Level::DebugLevel;
+  if (str == "info")      return QsLogging::Level::InfoLevel;
+  if (str == "warn")      return QsLogging::Level::WarnLevel;
+  if (str == "error")     return QsLogging::Level::ErrorLevel;
+  if (str == "fatal")     return QsLogging::Level::FatalLevel;
+  if (str == "disable")   return QsLogging::Level::OffLevel;
+  // if not valid, use default
+  return QsLogging::Level::DebugLevel;
+}
+
+static void updateLogLevel()
+{
+  QString level = SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "logLevel").toString();
+  if (level.size())
+  {
+    QLOG_INFO() << "Setting log level to:" << level;
+    Logger::instance().setLoggingLevel(logLevelFromString(level));
+  }
+}
+
 /////////////////////////////////////////////////////////////////////////////////////////
 char** appendCommandLineArguments(int *argc, char **argv)
 {
@@ -273,6 +296,8 @@ int main(int argc, char *argv[])
     });
     engine->load(QUrl(QStringLiteral("qrc:/ui/webview.qml")));
 
+    updateLogLevel();
+
     // run our application
     int ret = app.exec();