Log.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Created by Tobias Hieta on 07/03/16.
  3. //
  4. #include "Log.h"
  5. #include <QtQml>
  6. #include <QGuiApplication>
  7. #include "QsLog.h"
  8. #include "shared/Names.h"
  9. #include "shared/Paths.h"
  10. #include "settings/SettingsComponent.h"
  11. #include "Version.h"
  12. using namespace QsLogging;
  13. /////////////////////////////////////////////////////////////////////////////////////////
  14. static void qtMessageOutput(QtMsgType type, const QMessageLogContext& context, const QString& msg)
  15. {
  16. QByteArray localMsg = msg.toLocal8Bit();
  17. QString prefix;
  18. if (context.line)
  19. prefix = QString("%1:%2:%3: ").arg(context.file).arg(context.line).arg(context.function);
  20. QString text = prefix + msg;
  21. switch (type)
  22. {
  23. case QtDebugMsg:
  24. QLOG_DEBUG() << text;
  25. break;
  26. case QtInfoMsg:
  27. QLOG_INFO() << text;
  28. break;
  29. case QtWarningMsg:
  30. QLOG_WARN() << text;
  31. break;
  32. case QtCriticalMsg:
  33. QLOG_ERROR() << text;
  34. break;
  35. case QtFatalMsg:
  36. QLOG_FATAL() << text;
  37. break;
  38. }
  39. }
  40. /////////////////////////////////////////////////////////////////////////////////////////
  41. static void elidePattern(QString& msg, const QString& substring, int chars)
  42. {
  43. int start = 0;
  44. while (true)
  45. {
  46. start = msg.indexOf(substring, start);
  47. if (start < 0 || start + substring.length() + chars > msg.length())
  48. break;
  49. start += substring.length();
  50. for (int n = 0; n < chars; n++)
  51. msg[start + n] = QChar('x');
  52. }
  53. }
  54. /////////////////////////////////////////////////////////////////////////////////////////
  55. void Log::CensorAuthTokens(QString& msg)
  56. {
  57. elidePattern(msg, "api_key=", 20);
  58. elidePattern(msg, "X-MediaBrowser-Token%3D", 20);
  59. elidePattern(msg, "X-MediaBrowser-Token=", 20);
  60. elidePattern(msg, "api_key=", 20);
  61. elidePattern(msg, "ApiKey=", 20);
  62. elidePattern(msg, "AccessToken=", 20);
  63. }
  64. /////////////////////////////////////////////////////////////////////////////////////////
  65. static QsLogging::Level logLevelFromString(const QString& str)
  66. {
  67. if (str == "trace") return QsLogging::Level::TraceLevel;
  68. if (str == "debug") return QsLogging::Level::DebugLevel;
  69. if (str == "info") return QsLogging::Level::InfoLevel;
  70. if (str == "warn") return QsLogging::Level::WarnLevel;
  71. if (str == "error") return QsLogging::Level::ErrorLevel;
  72. if (str == "fatal") return QsLogging::Level::FatalLevel;
  73. if (str == "disable") return QsLogging::Level::OffLevel;
  74. // if not valid, use default
  75. return QsLogging::Level::DebugLevel;
  76. }
  77. /////////////////////////////////////////////////////////////////////////////////////////
  78. void Log::UpdateLogLevel()
  79. {
  80. QString level = SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "logLevel").toString();
  81. if (level.size())
  82. {
  83. QLOG_INFO() << "Setting log level to:" << level;
  84. Logger::instance().setLoggingLevel(logLevelFromString(level));
  85. }
  86. }
  87. /////////////////////////////////////////////////////////////////////////////////////////
  88. void Log::Init()
  89. {
  90. // Note where the logfile is going to be
  91. qDebug("Logging to %s", qPrintable(Paths::logDir(Names::MainName() + ".log")));
  92. // init logging.
  93. DestinationPtr dest = DestinationFactory::MakeFileDestination(
  94. Paths::logDir(Names::MainName() + ".log"),
  95. EnableLogRotationOnOpen,
  96. MaxSizeBytes(1024 * 1024),
  97. MaxOldLogCount(9));
  98. Logger::instance().addDestination(dest);
  99. Logger::instance().setLoggingLevel(DebugLevel);
  100. Logger::instance().setProcessingCallback(Log::CensorAuthTokens);
  101. qInstallMessageHandler(qtMessageOutput);
  102. QLOG_INFO() << "Starting Jellyfin Media Player version:" << qPrintable(Version::GetVersionString()) << "build date:" << qPrintable(Version::GetBuildDate());
  103. QLOG_INFO() << qPrintable(QString(" Running on: %1 [%2] arch %3").arg(QSysInfo::prettyProductName()).arg(QSysInfo::kernelVersion()).arg(QSysInfo::currentCpuArchitecture()));
  104. QLOG_INFO() << " Qt Version:" << QT_VERSION_STR << qPrintable(QString("[%1]").arg(QSysInfo::buildAbi()));
  105. }
  106. /////////////////////////////////////////////////////////////////////////////////////////
  107. void Log::EnableTerminalOutput()
  108. {
  109. Logger::instance().addDestination(DestinationFactory::MakeDebugOutputDestination());
  110. }
  111. /////////////////////////////////////////////////////////////////////////////////////////
  112. void Log::Uninit()
  113. {
  114. qInstallMessageHandler(0);
  115. Logger::destroyInstance();
  116. }