Logging is easier now, assigned in runtime group is now stored in config
This commit is contained in:
parent
7f57cd3bf6
commit
1bda854139
19 changed files with 153 additions and 53 deletions
|
@ -1,7 +1,11 @@
|
|||
set(SOURCES
|
||||
logger.cpp
|
||||
loggable.cpp
|
||||
)
|
||||
|
||||
set(HEADERS
|
||||
logger.h
|
||||
loggable.h
|
||||
definitions.h
|
||||
result.h
|
||||
)
|
||||
|
|
29
shared/loggable.cpp
Normal file
29
shared/loggable.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "loggable.h"
|
||||
|
||||
Shared::Loggable::Loggable(const Logger& logger, const std::vector<std::string>& domain) :
|
||||
logger(logger),
|
||||
domain(domain)
|
||||
{}
|
||||
|
||||
void Shared::Loggable::trace(const std::string& message) const {
|
||||
logger.log(Logger::trace, message, domain);
|
||||
}
|
||||
|
||||
void Shared::Loggable::debug(const std::string& message) const {
|
||||
logger.log(Logger::debug, message, domain);
|
||||
}
|
||||
|
||||
void Shared::Loggable::info(const std::string& message) const {
|
||||
logger.log(Logger::info, message, domain);
|
||||
}
|
||||
|
||||
void Shared::Loggable::warn(const std::string& message) const {
|
||||
logger.log(Logger::warning, message, domain);
|
||||
}
|
||||
|
||||
void Shared::Loggable::error(const std::string& message) const {
|
||||
logger.log(Logger::error, message, domain);
|
||||
}
|
29
shared/loggable.h
Normal file
29
shared/loggable.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
namespace Shared {
|
||||
|
||||
class Loggable {
|
||||
public:
|
||||
Loggable(const Logger& logger, const std::vector<std::string>& domain = {"Unknown"});
|
||||
|
||||
private:
|
||||
const Logger& logger;
|
||||
std::vector<std::string> domain;
|
||||
|
||||
protected:
|
||||
void trace(const std::string& message) const;
|
||||
void debug(const std::string& message) const;
|
||||
void info(const std::string& message) const;
|
||||
void warn(const std::string& message) const;
|
||||
void error(const std::string& message) const;
|
||||
};
|
||||
|
||||
}
|
156
shared/logger.cpp
Normal file
156
shared/logger.cpp
Normal file
|
@ -0,0 +1,156 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
|
||||
static constexpr std::string_view logLevelMap[] = {
|
||||
"TRACE",
|
||||
"DEBUG",
|
||||
"INFO",
|
||||
"WARNING",
|
||||
"ERROR",
|
||||
"FATAL",
|
||||
};
|
||||
|
||||
static constexpr std::string_view colorMap[] = {
|
||||
"\033[90m", // TRACE (Gray)
|
||||
"\033[34m", // DEBUG (Blue)
|
||||
"\033[32m", // INFO (Green)
|
||||
"\033[33m", // WARNING (Yellow)
|
||||
"\033[31m", // ERROR (Red)
|
||||
"\033[35m", // FATAL (Magenta)
|
||||
};
|
||||
|
||||
static constexpr std::string_view clear = "\033[0m";
|
||||
static constexpr std::string_view bold = "\033[1m";
|
||||
|
||||
void writeTimestamp() {
|
||||
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
||||
std::time_t time_now = std::chrono::system_clock::to_time_t(now);
|
||||
std::tm local_time = *std::localtime(&time_now);
|
||||
std::chrono::milliseconds millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
|
||||
|
||||
std::cout << "\033[90m" << std::put_time(&local_time, "%Y-%m-%d %H:%M:%S");
|
||||
std::cout << "." << std::setfill('0') << std::setw(3) << millis.count();
|
||||
std::cout << clear << ' ';
|
||||
}
|
||||
|
||||
constexpr std::string_view getLogLevel(gloox::LogLevel level) {
|
||||
return (level >= 0 && level < 3) ? logLevelMap[Shared::Logger::convert(level)] : "UNKNOWN";
|
||||
}
|
||||
|
||||
constexpr std::string_view getColor(gloox::LogLevel level) {
|
||||
return (level >= 0 && level < 3) ? colorMap[Shared::Logger::convert(level)] : "";
|
||||
}
|
||||
|
||||
void writeTags(gloox::LogArea area) {
|
||||
if (area & gloox::LogAreaClassParser)
|
||||
std::cout << " [Parser]";
|
||||
if (area & gloox::LogAreaClassConnectionTCPBase)
|
||||
std::cout << " [Connection]";
|
||||
if (area & gloox::LogAreaClassClient)
|
||||
std::cout << " [Client]";
|
||||
if (area & gloox::LogAreaClassClientbase)
|
||||
std::cout << " [Client Base]";
|
||||
if (area & gloox::LogAreaClassComponent)
|
||||
std::cout << " [Component]";
|
||||
if (area & gloox::LogAreaClassDns)
|
||||
std::cout << " [DNS]";
|
||||
if (area & gloox::LogAreaClassConnectionHTTPProxy)
|
||||
std::cout << " [HTTP Proxy]";
|
||||
if (area & gloox::LogAreaClassConnectionSOCKS5Proxy)
|
||||
std::cout << " [SOCKS5 Proxy]";
|
||||
if (area & gloox::LogAreaClassConnectionTCPClient)
|
||||
std::cout << " [TCP Client]";
|
||||
if (area & gloox::LogAreaClassConnectionTCPServer)
|
||||
std::cout << " [TCP Server]";
|
||||
if (area & gloox::LogAreaClassS5BManager)
|
||||
std::cout << " [SOCKS5 Bytestream Manager]";
|
||||
if (area & gloox::LogAreaClassSOCKS5Bytestream)
|
||||
std::cout << " [SOCKS5 Bytestream]";
|
||||
if (area & gloox::LogAreaLinkLocalManager)
|
||||
std::cout << " [Link Local Manager]";
|
||||
if (area & gloox::LogAreaXmlIncoming)
|
||||
std::cout << " \033[1;36m[XML IN]";
|
||||
if (area & gloox::LogAreaXmlOutgoing)
|
||||
std::cout << " \033[1;35m[XML OUT]";
|
||||
if (area & gloox::LogAreaUser)
|
||||
std::cout << " [USER]";
|
||||
|
||||
if (area == gloox::LogAreaClassDns)
|
||||
std::cout << '\t';
|
||||
}
|
||||
|
||||
Shared::Logger::Level Shared::Logger::convert(gloox::LogLevel level) {
|
||||
switch (level) {
|
||||
case gloox::LogLevelDebug:
|
||||
return trace;
|
||||
case gloox::LogLevelError:
|
||||
return error;
|
||||
case gloox::LogLevelWarning:
|
||||
return warning;
|
||||
};
|
||||
|
||||
return warning;
|
||||
}
|
||||
|
||||
gloox::LogLevel Shared::Logger::convert(Level level) {
|
||||
switch (level) {
|
||||
case trace:
|
||||
return gloox::LogLevelDebug;
|
||||
case debug:
|
||||
return gloox::LogLevelWarning;
|
||||
case info:
|
||||
return gloox::LogLevelWarning;
|
||||
case warning:
|
||||
return gloox::LogLevelWarning;
|
||||
case error:
|
||||
return gloox::LogLevelError;
|
||||
case fatal:
|
||||
return gloox::LogLevelError;
|
||||
}
|
||||
|
||||
return gloox::LogLevelWarning;
|
||||
}
|
||||
|
||||
Shared::Logger::Logger(Level level):
|
||||
level(level)
|
||||
{}
|
||||
|
||||
Shared::Logger::~Logger() {}
|
||||
|
||||
void Shared::Logger::handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message) {
|
||||
writeTimestamp();
|
||||
std::cout << getColor(level) << bold << '[' << getLogLevel(level) << ']' << clear << bold;
|
||||
writeTags(area);
|
||||
std::cout << clear << '\t' << message << clear << std::endl;
|
||||
}
|
||||
|
||||
void Shared::Logger::log(Level lvl, const std::string& message, const std::vector<std::string>& domain) const {
|
||||
if (lvl < level)
|
||||
return;
|
||||
|
||||
writeTimestamp();
|
||||
std::cout << colorMap[lvl] << bold << '[' << logLevelMap[lvl] << ']' << clear;
|
||||
|
||||
if (!domain.empty()) {
|
||||
std::cout << ' ' << bold << '[';
|
||||
|
||||
bool first = true;
|
||||
for (const std::string& tag : domain) {
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
std::cout << ' ';
|
||||
|
||||
std::cout << tag;
|
||||
}
|
||||
|
||||
std::cout << ']' << clear;
|
||||
}
|
||||
std::cout << '\t' << message << clear << std::endl;
|
||||
}
|
||||
|
39
shared/logger.h
Normal file
39
shared/logger.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include <gloox/loghandler.h>
|
||||
#include <gloox/logsink.h>
|
||||
|
||||
namespace Shared {
|
||||
|
||||
class Logger: public gloox::LogHandler {
|
||||
public:
|
||||
enum Level {
|
||||
trace,
|
||||
debug,
|
||||
info,
|
||||
warning,
|
||||
error,
|
||||
fatal
|
||||
};
|
||||
|
||||
public:
|
||||
Logger(Level level = info);
|
||||
~Logger();
|
||||
|
||||
void handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message) override;
|
||||
|
||||
void log(Level level, const std::string& message, const std::vector<std::string>& domain = {}) const;
|
||||
|
||||
static gloox::LogLevel convert(Level level);
|
||||
static Level convert(gloox::LogLevel level);
|
||||
|
||||
private:
|
||||
Level level;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue