deskflow/src/lib/base/LogOutputters.cpp

176 lines
3 KiB
C++
Raw Normal View History

2012-06-10 16:50:54 +00:00
/*
* Deskflow -- mouse and keyboard sharing utility
2025-02-24 19:21:32 +00:00
* SPDX-FileCopyrightText: (C) 2025 Deskflow Developers
* 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
*/
#include "base/LogOutputters.h"
#include "arch/Arch.h"
2012-06-10 16:50:54 +00:00
#include <iostream>
#include <QFile>
#include <QString>
#include <QTextStream>
constexpr auto s_logFileSizeLimit = 1024 * 1024; //!< Max Log size before rotating (1Mb)
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
//
void StopLogOutputter::open(const QString &)
{
// do nothing
2012-06-10 16:50:54 +00:00
}
void StopLogOutputter::close()
{
// do nothing
2012-06-10 16:50:54 +00:00
}
void StopLogOutputter::show(bool)
{
// do nothing
2012-06-10 16:50:54 +00:00
}
bool StopLogOutputter::write(LogLevel, const QString &)
{
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
//
void ConsoleLogOutputter::open(const QString &title)
{
// do nothing
}
2012-06-10 16:50:54 +00:00
void ConsoleLogOutputter::close()
{
// do nothing
}
2012-06-10 16:50:54 +00:00
void ConsoleLogOutputter::show(bool showIfEmpty)
{
// do nothing
2012-06-10 16:50:54 +00:00
}
bool ConsoleLogOutputter::write(LogLevel level, const QString &msg)
{
if ((level >= LogLevel::Fatal) && (level <= LogLevel::Warning))
std::cerr << qPrintable(msg) << std::endl;
else
std::cout << qPrintable(msg) << std::endl;
std::cout.flush();
return true;
2012-06-10 16:50:54 +00:00
}
void ConsoleLogOutputter::flush() const
{
// do nothing
}
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
//
void SystemLogOutputter::open(const QString &title)
{
ARCH->openLog(title);
}
2012-06-10 16:50:54 +00:00
void SystemLogOutputter::close()
{
ARCH->closeLog();
}
2012-06-10 16:50:54 +00:00
void SystemLogOutputter::show(bool showIfEmpty)
{
ARCH->showLog(showIfEmpty);
}
2012-06-10 16:50:54 +00:00
bool SystemLogOutputter::write(LogLevel level, const QString &msg)
{
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
//
SystemLogger::SystemLogger(const QString &title, bool blockConsole)
{
// redirect log messages
if (blockConsole) {
m_stop = new StopLogOutputter; // NOSONAR - Adopted by `Log`
CLOG->insert(m_stop);
}
m_syslog = new SystemLogOutputter; // NOSONAR - Adopted by `Log`
m_syslog->open(title);
CLOG->insert(m_syslog);
2012-06-10 16:50:54 +00:00
}
SystemLogger::~SystemLogger()
{
CLOG->remove(m_syslog);
delete m_syslog;
if (m_stop != nullptr) {
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
//
FileLogOutputter::FileLogOutputter(const QString &logFile)
{
setLogFilename(logFile);
2012-06-10 16:50:54 +00:00
}
void FileLogOutputter::setLogFilename(const QString &logFile)
{
assert(logFile != nullptr);
m_fileName = logFile;
}
bool FileLogOutputter::write(LogLevel, const QString &message)
{
QFile file(m_fileName);
if (!file.open(QFile::WriteOnly | QFile::Append))
return false;
QTextStream(&file) << message << Qt::endl;
file.close();
if (file.size() > s_logFileSizeLimit) {
const auto oldFile = QStringLiteral("%1.1").arg(m_fileName);
QFile::remove(m_fileName);
QFile::rename(m_fileName, oldFile);
}
return true;
2012-06-10 16:50:54 +00:00
}
void FileLogOutputter::open(const QString &title)
{
// do nothing
}
2012-06-10 16:50:54 +00:00
void FileLogOutputter::close()
{
// do nothing
}
2012-06-10 16:50:54 +00:00
void FileLogOutputter::show(bool showIfEmpty)
{
// do nothing
}