소스 검색

Refactor logging init in main.

It was very cluttering, moved it all to Log.cpp/Log.h instead.
Tobias Hieta 9 년 전
부모
커밋
fa9e0f9ad9
4개의 변경된 파일133개의 추가작업 그리고 102개의 파일을 삭제
  1. 3 102
      src/main.cpp
  2. 1 0
      src/utils/CMakeLists.txt
  3. 115 0
      src/utils/Log.cpp
  4. 14 0
      src/utils/Log.h

+ 3 - 102
src/main.cpp

@@ -20,13 +20,12 @@
 #include "ui/KonvergoEngine.h"
 #include "UniqueApplication.h"
 #include "utils/HelperLauncher.h"
+#include "utils/Log.h"
 
 #if defined(Q_OS_MAC) || defined(Q_OS_LINUX)
 #include "SignalManager.h"
 #endif
 
-using namespace QsLogging;
-
 /////////////////////////////////////////////////////////////////////////////////////////
 static void preinitQt()
 {
@@ -49,101 +48,6 @@ static void preinitQt()
 #endif
 }
 
-/////////////////////////////////////////////////////////////////////////////////////////
-static void qtMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
-{
-    QByteArray localMsg = msg.toLocal8Bit();
-    QString prefix;
-    if (context.line)
-      prefix = QString("%1:%2:%3: ").arg(context.file).arg(context.line).arg(context.function);
-    QString text = prefix + msg;
-    switch (type)
-    {
-      case QtDebugMsg:
-        QLOG_DEBUG() << text;
-        break;
-      case QtInfoMsg:
-        QLOG_INFO() << text;
-        break;
-      case QtWarningMsg:
-        QLOG_WARN() << text;
-        break;
-      case QtCriticalMsg:
-        QLOG_ERROR() << text;
-        break;
-      case QtFatalMsg:
-        QLOG_FATAL() << text;
-        break;
-    }
-}
-
-/////////////////////////////////////////////////////////////////////////////////////////
-static void elidePattern(QString& msg, const QString& substring, int chars)
-{
-  int start = 0;
-  while (true)
-  {
-    start = msg.indexOf(substring, start);
-    if (start < 0 || start + substring.length() + chars > msg.length())
-      break;
-    start += substring.length();
-    for (int n = 0; n < chars; n++)
-      msg[start + n] = QChar('x');
-  }
-}
-
-/////////////////////////////////////////////////////////////////////////////////////////
-static void processLog(QString& msg)
-{
-  elidePattern(msg, "X-Plex-Token=", 20);
-  elidePattern(msg, "X-Plex-Token%3D", 20);
-}
-
-/////////////////////////////////////////////////////////////////////////////////////////
-void initLogger()
-{
-  // Note where the logfile is going to be
-  qDebug("Logging to %s", qPrintable(Paths::logDir(Names::MainName() + ".log")));
-
-  // init logging.
-  DestinationPtr dest = DestinationFactory::MakeFileDestination(
-    Paths::logDir(Names::MainName() + ".log"),
-    EnableLogRotationOnOpen,
-    MaxSizeBytes(1024 * 1024),
-    MaxOldLogCount(9));
-
-  Logger::instance().addDestination(dest);
-  Logger::instance().setLoggingLevel(DebugLevel);
-  Logger::instance().setProcessingCallback(processLog);
-
-  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, const QStringList& args)
 {
@@ -236,10 +140,7 @@ int main(int argc, char *argv[])
     Q_UNUSED(signalManager);
 #endif
 
-    initLogger();
-    QLOG_INFO() << "Starting Plex Media Player version:" << qPrintable(Version::GetVersionString()) << "build date:" << qPrintable(Version::GetBuildDate());
-    QLOG_INFO() << qPrintable(QString("  Running on: %1 [%2] arch %3").arg(QSysInfo::prettyProductName()).arg(QSysInfo::kernelVersion()).arg(QSysInfo::currentCpuArchitecture()));
-    QLOG_INFO() << "  Qt Version:" << QT_VERSION_STR << qPrintable(QString("[%1]").arg(QSysInfo::buildAbi()));
+    Log::Init();
 
     // Quit app and apply update if we find one.
     if (UpdateManager::CheckForUpdates())
@@ -317,7 +218,7 @@ int main(int argc, char *argv[])
     });
     engine->load(QUrl(QStringLiteral("qrc:/ui/webview.qml")));
 
-    updateLogLevel();
+    Log::UpdateLogLevel();
 
     // run our application
     int ret = app.exec();

+ 1 - 0
src/utils/CMakeLists.txt

@@ -4,6 +4,7 @@ add_sources(
   PlatformUtils.cpp PlatformUtils.h
   HelperLauncher.h HelperLauncher.cpp
   Utils.cpp Utils.h
+  Log.cpp Log.h
 )
 
 if(APPLE)

+ 115 - 0
src/utils/Log.cpp

@@ -0,0 +1,115 @@
+//
+// Created by Tobias Hieta on 07/03/16.
+//
+
+#include "Log.h"
+
+#include <QtQml>
+#include <QGuiApplication>
+
+#include "QsLog.h"
+#include "shared/Names.h"
+#include "shared/Paths.h"
+#include "settings/SettingsComponent.h"
+#include "Version.h"
+
+using namespace QsLogging;
+
+/////////////////////////////////////////////////////////////////////////////////////////
+static void qtMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
+{
+    QByteArray localMsg = msg.toLocal8Bit();
+    QString prefix;
+    if (context.line)
+      prefix = QString("%1:%2:%3: ").arg(context.file).arg(context.line).arg(context.function);
+    QString text = prefix + msg;
+    switch (type)
+    {
+      case QtDebugMsg:
+        QLOG_DEBUG() << text;
+        break;
+      case QtInfoMsg:
+        QLOG_INFO() << text;
+        break;
+      case QtWarningMsg:
+        QLOG_WARN() << text;
+        break;
+      case QtCriticalMsg:
+        QLOG_ERROR() << text;
+        break;
+      case QtFatalMsg:
+        QLOG_FATAL() << text;
+        break;
+    }
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+static void elidePattern(QString& msg, const QString& substring, int chars)
+{
+  int start = 0;
+  while (true)
+  {
+    start = msg.indexOf(substring, start);
+    if (start < 0 || start + substring.length() + chars > msg.length())
+      break;
+    start += substring.length();
+    for (int n = 0; n < chars; n++)
+      msg[start + n] = QChar('x');
+  }
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+static void processLog(QString& msg)
+{
+  elidePattern(msg, "X-Plex-Token=", 20);
+  elidePattern(msg, "X-Plex-Token%3D", 20);
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+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;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+void Log::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));
+  }
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+void Log::Init()
+{
+  // Note where the logfile is going to be
+  qDebug("Logging to %s", qPrintable(Paths::logDir(Names::MainName() + ".log")));
+
+  // init logging.
+  DestinationPtr dest = DestinationFactory::MakeFileDestination(
+    Paths::logDir(Names::MainName() + ".log"),
+    EnableLogRotationOnOpen,
+    MaxSizeBytes(1024 * 1024),
+    MaxOldLogCount(9));
+
+  Logger::instance().addDestination(dest);
+  Logger::instance().setLoggingLevel(DebugLevel);
+  Logger::instance().setProcessingCallback(processLog);
+
+  qInstallMessageHandler(qtMessageOutput);
+
+  QLOG_INFO() << "Starting Plex Media Player version:" << qPrintable(Version::GetVersionString()) << "build date:" << qPrintable(Version::GetBuildDate());
+  QLOG_INFO() << qPrintable(QString("  Running on: %1 [%2] arch %3").arg(QSysInfo::prettyProductName()).arg(QSysInfo::kernelVersion()).arg(QSysInfo::currentCpuArchitecture()));
+  QLOG_INFO() << "  Qt Version:" << QT_VERSION_STR << qPrintable(QString("[%1]").arg(QSysInfo::buildAbi()));
+}

+ 14 - 0
src/utils/Log.h

@@ -0,0 +1,14 @@
+//
+// Created by Tobias Hieta on 07/03/16.
+//
+
+#ifndef PLEXMEDIAPLAYER_LOG_H
+#define PLEXMEDIAPLAYER_LOG_H
+
+namespace Log
+{
+  void Init();
+  void UpdateLogLevel();
+}
+
+#endif //PLEXMEDIAPLAYER_LOG_H