2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
2025-02-24 19:21:32 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
|
2025-01-24 00:11:17 +00:00
|
|
|
* SPDX-FileCopyrightText: (C) 2012 - 2016 Symless Ltd.
|
|
|
|
|
* SPDX-FileCopyrightText: (C) 2002 Chris Schoeneman
|
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-only WITH LicenseRef-OpenSSL-Exception
|
2012-06-10 16:50:54 +00:00
|
|
|
*/
|
|
|
|
|
|
2025-03-19 21:14:47 +00:00
|
|
|
#include "base/LogOutputters.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
#include "arch/Arch.h"
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-05-17 02:11:30 +00:00
|
|
|
#include <iostream>
|
2014-02-28 12:36:45 +00:00
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
#include <QFile>
|
|
|
|
|
#include <QString>
|
|
|
|
|
#include <QTextStream>
|
|
|
|
|
|
2025-07-08 12:32:29 +00:00
|
|
|
constexpr auto s_logFileSizeLimit = 1024 * 1024; //!< Max Log size before rotating (1Mb)
|
2015-05-19 13:04:02 +00:00
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// StopLogOutputter
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
void StopLogOutputter::open(const QString &)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void StopLogOutputter::close()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void StopLogOutputter::show(bool)
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
bool StopLogOutputter::write(LogLevel, const QString &)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// ConsoleLogOutputter
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
void ConsoleLogOutputter::open(const QString &title)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-05-17 02:11:30 +00:00
|
|
|
// do nothing
|
2024-10-17 18:04:35 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ConsoleLogOutputter::close()
|
|
|
|
|
{
|
2025-05-17 02:11:30 +00:00
|
|
|
// do nothing
|
2024-10-17 18:04:35 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void ConsoleLogOutputter::show(bool showIfEmpty)
|
|
|
|
|
{
|
2025-05-17 02:11:30 +00:00
|
|
|
// do nothing
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
bool ConsoleLogOutputter::write(LogLevel level, const QString &msg)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-07-07 21:44:21 +00:00
|
|
|
if ((level >= LogLevel::Fatal) && (level <= LogLevel::Warning))
|
2025-05-30 22:26:02 +00:00
|
|
|
std::cerr << qPrintable(msg) << std::endl;
|
2025-05-17 02:11:30 +00:00
|
|
|
else
|
2025-05-30 22:26:02 +00:00
|
|
|
std::cout << qPrintable(msg) << std::endl;
|
2025-05-17 02:11:30 +00:00
|
|
|
std::cout.flush();
|
2024-07-02 19:07:06 +00:00
|
|
|
return true;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-20 02:19:19 +00:00
|
|
|
void ConsoleLogOutputter::flush() const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-05-20 02:19:19 +00:00
|
|
|
// do nothing
|
2024-10-17 18:04:35 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// SystemLogOutputter
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
void SystemLogOutputter::open(const QString &title)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
|
|
|
|
ARCH->openLog(title);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void SystemLogOutputter::close()
|
|
|
|
|
{
|
|
|
|
|
ARCH->closeLog();
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void SystemLogOutputter::show(bool showIfEmpty)
|
|
|
|
|
{
|
|
|
|
|
ARCH->showLog(showIfEmpty);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
bool SystemLogOutputter::write(LogLevel level, const QString &msg)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
ARCH->writeLog(level, msg);
|
|
|
|
|
return true;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// SystemLogger
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
SystemLogger::SystemLogger(const QString &title, bool blockConsole)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// redirect log messages
|
|
|
|
|
if (blockConsole) {
|
2025-02-24 14:25:52 +00:00
|
|
|
m_stop = new StopLogOutputter; // NOSONAR - Adopted by `Log`
|
2024-07-02 19:07:06 +00:00
|
|
|
CLOG->insert(m_stop);
|
|
|
|
|
}
|
2025-02-24 14:25:52 +00:00
|
|
|
m_syslog = new SystemLogOutputter; // NOSONAR - Adopted by `Log`
|
2024-07-02 19:07:06 +00:00
|
|
|
m_syslog->open(title);
|
|
|
|
|
CLOG->insert(m_syslog);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
SystemLogger::~SystemLogger()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
CLOG->remove(m_syslog);
|
|
|
|
|
delete m_syslog;
|
2025-04-27 19:39:23 +00:00
|
|
|
if (m_stop != nullptr) {
|
2024-07-02 19:07:06 +00:00
|
|
|
CLOG->remove(m_stop);
|
|
|
|
|
delete m_stop;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// FileLogOutputter
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
FileLogOutputter::FileLogOutputter(const QString &logFile)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
setLogFilename(logFile);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
void FileLogOutputter::setLogFilename(const QString &logFile)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
assert(logFile != nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
m_fileName = logFile;
|
2014-10-27 14:11:43 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
bool FileLogOutputter::write(LogLevel level, const QString &message)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-05-30 22:26:02 +00:00
|
|
|
QFile file(m_fileName);
|
|
|
|
|
if (!file.open(QFile::WriteOnly | QFile::Append))
|
|
|
|
|
return false;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
QTextStream(&file) << message << Qt::endl;
|
|
|
|
|
file.close();
|
2015-05-19 13:04:02 +00:00
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
if (file.size() > s_logFileSizeLimit) {
|
|
|
|
|
const auto oldFile = QStringLiteral("%1.1").arg(m_fileName);
|
|
|
|
|
QFile::remove(m_fileName);
|
|
|
|
|
file.rename(m_fileName, oldFile);
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-30 22:26:02 +00:00
|
|
|
void FileLogOutputter::open(const QString &title)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-05-19 11:16:06 +00:00
|
|
|
// do nothing
|
2024-10-17 18:04:35 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void FileLogOutputter::close()
|
|
|
|
|
{
|
2025-05-19 11:16:06 +00:00
|
|
|
// do nothing
|
2024-10-17 18:04:35 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void FileLogOutputter::show(bool showIfEmpty)
|
|
|
|
|
{
|
2025-05-19 11:16:06 +00:00
|
|
|
// do nothing
|
2024-10-17 18:04:35 +00:00
|
|
|
}
|