| 1 | // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-only |
| 4 | |
| 5 | #include "LogManager.h" |
| 6 | #include <dtkcore_global.h> |
| 7 | |
| 8 | // 均使用以兼容不同 DTK Log 模块版本 |
| 9 | DCORE_USE_NAMESPACE |
| 10 | #ifdef DLOG_CORE_USE_NAMESPACE |
| 11 | DLOG_CORE_USE_NAMESPACE |
| 12 | #endif |
| 13 | |
| 14 | DBMLogManager::DBMLogManager() |
| 15 | { |
| 16 | m_format = "%{time}{yyyy-MM-dd, HH:mm:ss.zzz} [%{type:-7}] [%{file:-20} %{function:-35} %{line}] %{message}\n"; |
| 17 | } |
| 18 | |
| 19 | void DBMLogManager::initConsoleAppender() |
| 20 | { |
| 21 | m_consoleAppender = new ConsoleAppender; |
| 22 | m_consoleAppender->setFormat(m_format); |
| 23 | loggerInstance()->registerAppender(m_consoleAppender); |
| 24 | } |
| 25 | |
| 26 | void DBMLogManager::initRollingFileAppender() |
| 27 | { |
| 28 | QString cachePath = QStandardPaths::standardLocations(QStandardPaths::CacheLocation).at(0); |
| 29 | if (systemLog) { |
| 30 | cachePath = QString("/var/log/%1").arg(qApp->organizationName()); |
| 31 | } |
| 32 | if (!QDir(cachePath).exists()) { |
| 33 | if (!QDir(cachePath).mkpath(cachePath)) { |
| 34 | return; |
| 35 | } |
| 36 | } |
| 37 | m_logPath = joinPath(cachePath, QString("%1.log").arg(qApp->applicationName())); |
| 38 | |
| 39 | m_rollingFileAppender = new RollingFileAppender(m_logPath); |
| 40 | m_rollingFileAppender->setFormat(m_format); |
| 41 | m_rollingFileAppender->setLogFilesLimit(5); |
| 42 | m_rollingFileAppender->setDatePattern(RollingFileAppender::DailyRollover); |
| 43 | loggerInstance()->registerAppender(m_rollingFileAppender); |
| 44 | } |
| 45 | |
| 46 | //! Registers the appender to write the log records to the Console |
| 47 | /** |
| 48 | * \sa registerFileAppender |
| 49 | */ |
| 50 | void DBMLogManager::setSystemLog(bool sys) |
| 51 | { |
| 52 | DBMLogManager::instance()->systemLog = sys; |
| 53 | } |
| 54 | |
| 55 | void DBMLogManager::registerConsoleAppender() |
| 56 | { |
| 57 | DBMLogManager::instance()->initConsoleAppender(); |
| 58 | } |
| 59 | |
| 60 | //! Registers the appender to write the log records to the file |
| 61 | /** |
| 62 | * \sa getlogFilePath |
| 63 | * \sa registerConsoleAppender |
| 64 | */ |
| 65 | void DBMLogManager::registerFileAppender() |
| 66 | { |
| 67 | DBMLogManager::instance()->initRollingFileAppender(); |
| 68 | } |
| 69 | |
| 70 | //! Return the path file log storage |
| 71 | /** |
| 72 | * \sa registerFileAppender |
| 73 | */ |
| 74 | QString DBMLogManager::getlogFilePath() |
| 75 | { |
| 76 | return QDir::toNativeSeparators(DBMLogManager::instance()->m_logPath); |
| 77 | } |
| 78 | |
| 79 | void DBMLogManager::setLogFormat(const QString &format) |
| 80 | { |
| 81 | //m_format = "%{time}{yyyy-MM-dd, HH:mm:ss.zzz} [%{type:-7}] [%{file:-20} %{function:-35} %{line}] %{message}\n"; |
| 82 | DBMLogManager::instance()->m_format = format; |
| 83 | } |
| 84 | |
| 85 | QString DBMLogManager::joinPath(const QString &path, const QString &fileName) |
| 86 | { |
| 87 | QString separator(QDir::separator()); |
| 88 | return QString("%1%2%3").arg(path, separator, fileName); |
| 89 | } |
| 90 | |
| 91 | DBMLogManager::~DBMLogManager() |
| 92 | { |
| 93 | |
| 94 | } |
| 95 |