2012-06-10 16:50:54 +00:00
|
|
|
/*
|
2024-09-17 19:00:25 +00:00
|
|
|
* Deskflow -- mouse and keyboard sharing utility
|
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
|
|
|
*/
|
|
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
#include "base/Log.h"
|
2025-07-07 21:44:21 +00:00
|
|
|
#include "base/LogLevel.h"
|
2025-03-19 21:14:47 +00:00
|
|
|
#include "base/LogOutputters.h"
|
2025-03-19 22:14:27 +00:00
|
|
|
#include "common/Constants.h"
|
2014-02-28 12:36:45 +00:00
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
#include <cstdarg>
|
2022-12-08 09:16:44 +00:00
|
|
|
#include <cstdint>
|
2012-06-10 16:50:54 +00:00
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstring>
|
2025-07-31 13:39:10 +00:00
|
|
|
|
2025-10-31 01:53:38 +00:00
|
|
|
#if HAVE_FORMAT
|
2025-07-31 13:39:10 +00:00
|
|
|
#include <format>
|
|
|
|
|
#endif
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-11-04 02:06:52 +00:00
|
|
|
#include <QDateTime>
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
const int kPriorityPrefixLength = 3;
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
// names of priorities
|
2026-05-09 01:10:59 +00:00
|
|
|
static const char *g_priority[] = {"FATAL", "ERROR", "WARNING", "INFO", "DEBUG", "VERBOSE"};
|
2012-06-10 16:50:54 +00:00
|
|
|
|
|
|
|
|
// number of priorities
|
2026-05-09 01:10:59 +00:00
|
|
|
static const int g_numPriority = 6;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2018-08-02 13:11:05 +00:00
|
|
|
// if NDEBUG (not debug) is not specified, i.e. you're building in debug,
|
|
|
|
|
// then set default log level to DEBUG, otherwise the max level is INFO.
|
|
|
|
|
//
|
|
|
|
|
// GOTCHA: if `-DCMAKE_BUILD_TYPE=Debug` isn't set when configuring cmake
|
|
|
|
|
// for visual studio, then NDEBUG will be set (even if your VS solution
|
|
|
|
|
// config is Debug).
|
2012-07-29 02:10:25 +00:00
|
|
|
#ifndef NDEBUG
|
2025-07-07 21:44:21 +00:00
|
|
|
static const LogLevel g_defaultMaxPriority = LogLevel::Debug;
|
2012-06-10 16:50:54 +00:00
|
|
|
#else
|
2025-07-07 21:44:21 +00:00
|
|
|
static const LogLevel g_defaultMaxPriority = LogLevel::Info;
|
2012-06-10 16:50:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
namespace {
|
|
|
|
|
|
2025-07-07 21:44:21 +00:00
|
|
|
LogLevel getPriority(const char *&fmt)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-16 08:30:35 +00:00
|
|
|
if (strnlen(fmt, SIZE_MAX) < kPriorityPrefixLength) {
|
|
|
|
|
throw std::invalid_argument("invalid format string, too short");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (fmt[0] != '%' || fmt[1] != 'z') {
|
|
|
|
|
throw std::invalid_argument("invalid format string, missing priority");
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 21:44:21 +00:00
|
|
|
return static_cast<LogLevel>(fmt[2] - '0');
|
2024-07-16 08:30:35 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-07 21:44:21 +00:00
|
|
|
std::vector<char> makeMessage(const char *filename, int lineNumber, const char *message, LogLevel priority)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2024-07-16 08:30:35 +00:00
|
|
|
|
|
|
|
|
// base size includes null terminator, colon, space, etc.
|
|
|
|
|
const int baseSize = 10;
|
|
|
|
|
|
|
|
|
|
const int priorityMaxSize = 10;
|
2025-07-07 21:44:21 +00:00
|
|
|
const auto currentPriority = static_cast<int>(priority);
|
2024-07-16 08:30:35 +00:00
|
|
|
|
2025-11-04 02:14:11 +00:00
|
|
|
auto timeStr = QDateTime::currentDateTime().toString(Qt::ISODateWithMs).toStdString();
|
2025-08-23 17:38:30 +00:00
|
|
|
|
2026-05-09 00:13:17 +00:00
|
|
|
auto sectionName = g_priority[currentPriority];
|
2025-08-23 17:38:30 +00:00
|
|
|
|
|
|
|
|
size_t priorityLength = strnlen(sectionName, priorityMaxSize);
|
2024-07-16 08:30:35 +00:00
|
|
|
size_t messageLength = strnlen(message, SIZE_MAX);
|
2025-11-04 02:06:52 +00:00
|
|
|
size_t bufferSize = baseSize + timeStr.length() + priorityLength + messageLength;
|
2024-07-16 08:30:35 +00:00
|
|
|
|
|
|
|
|
const auto filenameSet = filename != nullptr && filename[0] != '\0';
|
|
|
|
|
if (filenameSet) {
|
|
|
|
|
size_t filenameLength = strnlen(filename, SIZE_MAX);
|
|
|
|
|
size_t lineNumberLength = snprintf(nullptr, 0, "%d", lineNumber);
|
|
|
|
|
bufferSize += filenameLength + lineNumberLength;
|
|
|
|
|
|
|
|
|
|
std::vector<char> buffer(bufferSize);
|
2025-10-31 01:53:38 +00:00
|
|
|
#if HAVE_FORMAT
|
2025-07-31 13:39:10 +00:00
|
|
|
std::format_to_n(
|
2025-11-04 02:06:52 +00:00
|
|
|
buffer.data(), bufferSize, "[{}] {}: {}\n\t{}:{}", timeStr.c_str(), sectionName, message, filename, lineNumber
|
2025-07-31 13:39:10 +00:00
|
|
|
);
|
|
|
|
|
#else
|
2024-07-16 13:36:44 +00:00
|
|
|
snprintf(
|
2025-11-04 02:06:52 +00:00
|
|
|
buffer.data(), bufferSize, "[%s] %s: %s\n\t%s:%d", timeStr.c_str(), sectionName, message, filename, lineNumber
|
2024-10-17 18:04:35 +00:00
|
|
|
);
|
2025-07-31 13:39:10 +00:00
|
|
|
#endif
|
2024-07-16 08:30:35 +00:00
|
|
|
return buffer;
|
|
|
|
|
} else {
|
|
|
|
|
std::vector<char> buffer(bufferSize);
|
2025-10-31 01:53:38 +00:00
|
|
|
#if HAVE_FORMAT
|
2025-11-04 02:06:52 +00:00
|
|
|
std::format_to_n(buffer.data(), bufferSize, "[{}] {}: {}", timeStr.c_str(), sectionName, message);
|
2025-07-31 13:39:10 +00:00
|
|
|
#else
|
2025-11-04 02:06:52 +00:00
|
|
|
snprintf(buffer.data(), bufferSize, "[%s] %s: %s", timeStr.c_str(), sectionName, message);
|
2025-07-31 13:39:10 +00:00
|
|
|
#endif
|
2024-07-16 08:30:35 +00:00
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
2014-11-11 13:51:47 +00:00
|
|
|
// Log
|
2012-06-10 16:50:54 +00:00
|
|
|
//
|
|
|
|
|
|
2025-04-27 19:39:23 +00:00
|
|
|
Log *Log::s_log = nullptr;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
Log::Log(bool singleton)
|
|
|
|
|
{
|
2024-07-16 08:30:35 +00:00
|
|
|
if (singleton) {
|
2025-04-27 19:39:23 +00:00
|
|
|
assert(s_log == nullptr);
|
2024-07-16 08:30:35 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// other initalization
|
|
|
|
|
m_maxPriority = g_defaultMaxPriority;
|
2025-02-24 14:25:52 +00:00
|
|
|
insert(new ConsoleLogOutputter); // NOSONAR - Adopted by `Log`
|
2012-07-10 01:51:51 +00:00
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
if (singleton) {
|
|
|
|
|
s_log = this;
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
Log::Log(Log *src)
|
|
|
|
|
{
|
|
|
|
|
s_log = src;
|
|
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
Log::~Log()
|
|
|
|
|
{
|
2024-07-02 19:07:06 +00:00
|
|
|
// clean up
|
2025-04-27 20:17:32 +00:00
|
|
|
for (auto index = m_outputters.begin(); index != m_outputters.end(); ++index) {
|
2024-07-02 19:07:06 +00:00
|
|
|
delete *index;
|
|
|
|
|
}
|
2025-04-27 20:17:32 +00:00
|
|
|
for (auto index = m_alwaysOutputters.begin(); index != m_alwaysOutputters.end(); ++index) {
|
2024-07-02 19:07:06 +00:00
|
|
|
delete *index;
|
|
|
|
|
}
|
2015-01-14 17:24:45 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
Log *Log::getInstance()
|
|
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
assert(s_log != nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
return s_log;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
const char *Log::getFilterName() const
|
|
|
|
|
{
|
|
|
|
|
return getFilterName(getFilter());
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-07-07 21:44:21 +00:00
|
|
|
const char *Log::getFilterName(LogLevel level) const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-07-07 21:44:21 +00:00
|
|
|
const auto levelIndex = static_cast<int>(level);
|
|
|
|
|
if (levelIndex < 0) {
|
2024-07-02 19:07:06 +00:00
|
|
|
return "Message";
|
|
|
|
|
}
|
2025-07-07 21:44:21 +00:00
|
|
|
return g_priority[levelIndex];
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void Log::print(const char *file, int line, const char *fmt, ...)
|
|
|
|
|
{
|
2024-07-16 08:30:35 +00:00
|
|
|
const int initBufferSize = 1024;
|
|
|
|
|
const int bufferResizeScale = 2;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2025-07-07 21:44:21 +00:00
|
|
|
LogLevel priority = getPriority(fmt);
|
2024-07-16 08:30:35 +00:00
|
|
|
fmt += kPriorityPrefixLength;
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
if (priority > getFilter()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
std::vector<char> buffer(initBufferSize);
|
|
|
|
|
auto length = static_cast<int>(buffer.size());
|
2024-07-02 19:07:06 +00:00
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
va_list args;
|
|
|
|
|
va_start(args, fmt);
|
2024-07-16 08:30:35 +00:00
|
|
|
int n = vsnprintf(buffer.data(), length, fmt, args);
|
2024-07-02 19:07:06 +00:00
|
|
|
va_end(args);
|
|
|
|
|
|
2024-07-16 08:30:35 +00:00
|
|
|
if (n < 0 || n > length) {
|
|
|
|
|
length *= bufferResizeScale;
|
|
|
|
|
buffer.resize(length);
|
|
|
|
|
} else {
|
2024-07-02 19:07:06 +00:00
|
|
|
break;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2016-12-28 11:50:32 +00:00
|
|
|
|
2025-07-07 21:44:21 +00:00
|
|
|
if (priority == LogLevel::Print) {
|
2024-07-16 08:30:35 +00:00
|
|
|
output(priority, buffer.data());
|
2024-07-02 19:07:06 +00:00
|
|
|
} else {
|
2024-07-16 08:30:35 +00:00
|
|
|
auto message = makeMessage(file, line, buffer.data(), priority);
|
|
|
|
|
output(priority, message.data());
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-28 12:26:27 +00:00
|
|
|
void Log::insert(ILogOutputter *adoptedOutputter, bool alwaysAtHead)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-04-27 19:39:23 +00:00
|
|
|
assert(adoptedOutputter != nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
|
2025-06-16 00:15:07 +00:00
|
|
|
std::scoped_lock lock{m_mutex};
|
2024-07-02 19:07:06 +00:00
|
|
|
if (alwaysAtHead) {
|
2025-02-28 12:26:27 +00:00
|
|
|
m_alwaysOutputters.push_front(adoptedOutputter);
|
2024-07-02 19:07:06 +00:00
|
|
|
} else {
|
2025-02-28 12:26:27 +00:00
|
|
|
m_outputters.push_front(adoptedOutputter);
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-28 12:26:27 +00:00
|
|
|
adoptedOutputter->open(kAppName);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void Log::remove(ILogOutputter *outputter)
|
|
|
|
|
{
|
2025-06-16 00:15:07 +00:00
|
|
|
std::scoped_lock lock{m_mutex};
|
2024-07-02 19:07:06 +00:00
|
|
|
m_outputters.remove(outputter);
|
|
|
|
|
m_alwaysOutputters.remove(outputter);
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-17 18:04:35 +00:00
|
|
|
void Log::pop_front(bool alwaysAtHead)
|
|
|
|
|
{
|
2025-06-16 00:15:07 +00:00
|
|
|
std::scoped_lock lock{m_mutex};
|
2024-07-02 19:07:06 +00:00
|
|
|
OutputterList *list = alwaysAtHead ? &m_alwaysOutputters : &m_outputters;
|
|
|
|
|
if (!list->empty()) {
|
|
|
|
|
delete list->front();
|
|
|
|
|
list->pop_front();
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-02 12:25:30 +00:00
|
|
|
bool Log::setFilter(const QString &maxPriority)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-10-02 12:25:30 +00:00
|
|
|
if (maxPriority.isEmpty()) {
|
2024-07-02 19:07:06 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2025-10-02 12:25:30 +00:00
|
|
|
|
|
|
|
|
for (int i = 0; i < g_numPriority; ++i) {
|
|
|
|
|
if (maxPriority == QString(g_priority[i])) {
|
|
|
|
|
setFilter(static_cast<LogLevel>(i));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-07 21:44:21 +00:00
|
|
|
void Log::setFilter(LogLevel maxPriority)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-06-16 00:15:07 +00:00
|
|
|
std::scoped_lock lock{m_mutex};
|
2024-07-02 19:07:06 +00:00
|
|
|
m_maxPriority = maxPriority;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-07 21:44:21 +00:00
|
|
|
LogLevel Log::getFilter() const
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-06-16 00:15:07 +00:00
|
|
|
std::scoped_lock lock{m_mutex};
|
2024-07-02 19:07:06 +00:00
|
|
|
return m_maxPriority;
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|
|
|
|
|
|
2025-07-07 21:44:21 +00:00
|
|
|
void Log::output(LogLevel priority, const char *msg)
|
2024-10-17 18:04:35 +00:00
|
|
|
{
|
2025-08-23 17:38:30 +00:00
|
|
|
assert(static_cast<int>(priority) >= -2 && static_cast<int>(priority) < g_numPriority);
|
2025-04-27 19:39:23 +00:00
|
|
|
assert(msg != nullptr);
|
2024-07-02 19:07:06 +00:00
|
|
|
if (!msg)
|
|
|
|
|
return;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2025-06-16 00:15:07 +00:00
|
|
|
std::scoped_lock lock{m_mutex};
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
OutputterList::const_iterator i;
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
for (i = m_alwaysOutputters.begin(); i != m_alwaysOutputters.end(); ++i) {
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// write to outputter
|
|
|
|
|
(*i)->write(priority, msg);
|
|
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
for (i = m_outputters.begin(); i != m_outputters.end(); ++i) {
|
2012-06-10 16:50:54 +00:00
|
|
|
|
2024-07-02 19:07:06 +00:00
|
|
|
// write to outputter and break out of loop if it returns false
|
|
|
|
|
if (!(*i)->write(priority, msg)) {
|
|
|
|
|
break;
|
2016-12-28 11:50:32 +00:00
|
|
|
}
|
2024-07-02 19:07:06 +00:00
|
|
|
}
|
2012-06-10 16:50:54 +00:00
|
|
|
}
|