Logging
This commit is contained in:
parent
89c04254b8
commit
60c8782bdd
@ -23,15 +23,21 @@ std::string Config::getFullJID() const {
|
|||||||
return getBareJID() + "/" + getResource();
|
return getBareJID() + "/" + getResource();
|
||||||
}
|
}
|
||||||
|
|
||||||
gloox::LogLevel Config::getLogLevel() const {
|
Logger::Level Config::getLogLevel() const {
|
||||||
std::string level = root["logLevel"].as<std::string>("warning");
|
std::string level = root["logLevel"].as<std::string>("info");
|
||||||
|
|
||||||
if (level == "debug")
|
if (level == "trace")
|
||||||
return gloox::LogLevelDebug;
|
return Logger::trace;
|
||||||
|
else if (level == "debug")
|
||||||
|
return Logger::debug;
|
||||||
|
else if (level == "info")
|
||||||
|
return Logger::info;
|
||||||
|
else if (level == "warn" || level == "warning")
|
||||||
|
return Logger::warning;
|
||||||
else if (level == "error")
|
else if (level == "error")
|
||||||
return gloox::LogLevelError;
|
return Logger::error;
|
||||||
else
|
else
|
||||||
return gloox::LogLevelWarning;
|
return Logger::info;
|
||||||
}
|
}
|
||||||
|
|
||||||
gloox::TLSPolicy Config::getTLSPolicy() const {
|
gloox::TLSPolicy Config::getTLSPolicy() const {
|
||||||
|
@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
#include "yaml-cpp/yaml.h"
|
#include "yaml-cpp/yaml.h"
|
||||||
|
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
public:
|
public:
|
||||||
struct Module {
|
struct Module {
|
||||||
@ -31,7 +33,7 @@ public:
|
|||||||
std::string getPassword() const;
|
std::string getPassword() const;
|
||||||
std::string getResource() const;
|
std::string getResource() const;
|
||||||
std::map<std::string, std::string> getActors() const;
|
std::map<std::string, std::string> getActors() const;
|
||||||
gloox::LogLevel getLogLevel() const;
|
Logger::Level getLogLevel() const;
|
||||||
gloox::TLSPolicy getTLSPolicy() const;
|
gloox::TLSPolicy getTLSPolicy() const;
|
||||||
Module getModuleConfig(const std::string& name) const;
|
Module getModuleConfig(const std::string& name) const;
|
||||||
|
|
||||||
|
@ -6,5 +6,5 @@
|
|||||||
Core::Core(const std::string& configPath):
|
Core::Core(const std::string& configPath):
|
||||||
config(configPath),
|
config(configPath),
|
||||||
router(),
|
router(),
|
||||||
logger()
|
logger(config.getLogLevel())
|
||||||
{}
|
{}
|
||||||
|
@ -7,15 +7,21 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
static constexpr std::string_view logLevelMap[] = {
|
static constexpr std::string_view logLevelMap[] = {
|
||||||
"DEBUG", // 0
|
"TRACE",
|
||||||
"WARNING", // 1
|
"DEBUG",
|
||||||
"ERROR", // 2
|
"INFO",
|
||||||
|
"WARNING",
|
||||||
|
"ERROR",
|
||||||
|
"FATAL",
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr std::string_view colorMap[] = {
|
static constexpr std::string_view colorMap[] = {
|
||||||
"\033[32m", // DEBUG (Green)
|
"\033[90m", // TRACE (Gray)
|
||||||
|
"\033[34m", // DEBUG (Blue)
|
||||||
|
"\033[32m", // INFO (Green)
|
||||||
"\033[33m", // WARNING (Yellow)
|
"\033[33m", // WARNING (Yellow)
|
||||||
"\033[31m", // ERROR (Red)
|
"\033[31m", // ERROR (Red)
|
||||||
|
"\033[35m", // FATAL (Magenta)
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr std::string_view clear = "\033[0m";
|
static constexpr std::string_view clear = "\033[0m";
|
||||||
@ -33,11 +39,11 @@ void writeTimestamp() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constexpr std::string_view getLogLevel(gloox::LogLevel level) {
|
constexpr std::string_view getLogLevel(gloox::LogLevel level) {
|
||||||
return (level >= 0 && level < 3) ? logLevelMap[level] : "UNKNOWN";
|
return (level >= 0 && level < 3) ? logLevelMap[Logger::convert(level)] : "UNKNOWN";
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr std::string_view getColor(gloox::LogLevel level) {
|
constexpr std::string_view getColor(gloox::LogLevel level) {
|
||||||
return (level >= 0 && level < 3) ? colorMap[level] : "";
|
return (level >= 0 && level < 3) ? colorMap[Logger::convert(level)] : "";
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeTags(gloox::LogArea area) {
|
void writeTags(gloox::LogArea area) {
|
||||||
@ -78,14 +84,73 @@ void writeTags(gloox::LogArea area) {
|
|||||||
std::cout << '\t';
|
std::cout << '\t';
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger::Logger()
|
Logger::Level 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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
Logger::Logger(Level level):
|
||||||
|
level(level)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Logger::~Logger() {}
|
Logger::~Logger() {}
|
||||||
|
|
||||||
void Logger::handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message) {
|
void Logger::handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message) {
|
||||||
writeTimestamp();
|
writeTimestamp();
|
||||||
std::cout << getColor(level) << bold << "[" << getLogLevel(level) << "]" << clear << bold;
|
std::cout << getColor(level) << bold << '[' << getLogLevel(level) << ']' << clear << bold;
|
||||||
writeTags(area);
|
writeTags(area);
|
||||||
std::cout << clear << '\t' << message << clear << std::endl;
|
std::cout << clear << '\t' << message << clear << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Logger::log(Level lvl, const std::string& message, const std::vector<std::string>& domain) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -3,13 +3,33 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include <gloox/loghandler.h>
|
#include <gloox/loghandler.h>
|
||||||
#include <gloox/logsink.h>
|
#include <gloox/logsink.h>
|
||||||
|
|
||||||
class Logger: public gloox::LogHandler {
|
class Logger: public gloox::LogHandler {
|
||||||
public:
|
public:
|
||||||
Logger();
|
enum Level {
|
||||||
|
trace,
|
||||||
|
debug,
|
||||||
|
info,
|
||||||
|
warning,
|
||||||
|
error,
|
||||||
|
fatal
|
||||||
|
};
|
||||||
|
|
||||||
|
public:
|
||||||
|
Logger(Level level = info);
|
||||||
~Logger();
|
~Logger();
|
||||||
|
|
||||||
void handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message) override;
|
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 = {});
|
||||||
|
|
||||||
|
static gloox::LogLevel convert(Level level);
|
||||||
|
static Level convert(gloox::LogLevel level);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Level level;
|
||||||
};
|
};
|
||||||
|
@ -21,7 +21,8 @@ void Connection::initiialize() {
|
|||||||
gloox->registerConnectionListener(this);
|
gloox->registerConnectionListener(this);
|
||||||
gloox->registerMessageHandler(this);
|
gloox->registerMessageHandler(this);
|
||||||
|
|
||||||
gloox->logInstance().registerLogHandler(core->config.getLogLevel(), gloox::LogAreaAll, &core->logger);
|
::gloox::LogLevel level = Logger::convert(core->config.getLogLevel());
|
||||||
|
gloox->logInstance().registerLogHandler(level, gloox::LogAreaAll, &core->logger);
|
||||||
|
|
||||||
gloox::Disco* disco = gloox->disco();
|
gloox::Disco* disco = gloox->disco();
|
||||||
disco->setVersion("Jay", "0.0.1");
|
disco->setVersion("Jay", "0.0.1");
|
||||||
@ -38,6 +39,7 @@ void Connection::deinitialize() {
|
|||||||
if (state == initial)
|
if (state == initial)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
core->logger.log(Logger::debug, "deinitializing", {"Connection"});
|
||||||
gloox->logInstance().removeLogHandler(&core->logger);
|
gloox->logInstance().removeLogHandler(&core->logger);
|
||||||
|
|
||||||
gloox->removeMessageHandler(this);
|
gloox->removeMessageHandler(this);
|
||||||
@ -51,21 +53,96 @@ void Connection::connect() {
|
|||||||
if (state != disconnected)
|
if (state != disconnected)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
core->logger.log(Logger::debug, "connecting", {"Connection"});
|
||||||
state = connected;
|
state = connected;
|
||||||
gloox->connect(true);
|
gloox->connect(true);
|
||||||
state = disconnected;
|
state = disconnected;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::send(const std::string& jid, const std::string& body) {
|
void Connection::send(const std::string& jid, const std::string& body) {
|
||||||
|
core->logger.log(Logger::debug, "sending message \"" + body + "\" to " + jid, {"Connection"});
|
||||||
gloox->send(gloox::Message(gloox::Message::Chat, jid, body));
|
gloox->send(gloox::Message(gloox::Message::Chat, jid, body));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::handleMessage(const gloox::Message& message, gloox::MessageSession* session) {
|
void Connection::handleMessage(const gloox::Message& message, gloox::MessageSession* session) {
|
||||||
core->router.routeMessage(message.from().bare(), message.body());
|
std::string jid = message.from().bare();
|
||||||
|
core->logger.log(Logger::debug, "received message \"" + message.body() + "\" from " + jid, {"Connection"});
|
||||||
|
core->router.routeMessage(jid, message.body());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Connection::onConnect() {}
|
void Connection::onConnect() {
|
||||||
void Connection::onDisconnect(gloox::ConnectionError e) {}
|
core->logger.log(Logger::info, "connection established", {"Connection"});
|
||||||
bool Connection::onTLSConnect(const gloox::CertInfo&) { return true; }
|
}
|
||||||
|
void Connection::onDisconnect(gloox::ConnectionError e) {
|
||||||
|
std::string error;
|
||||||
|
|
||||||
|
switch (e) {
|
||||||
|
case gloox::ConnNoError:
|
||||||
|
break;
|
||||||
|
case gloox::ConnStreamError: //TODO Use ClientBase::streamError() to find the reason. */
|
||||||
|
error = "stream error occured";
|
||||||
|
break;
|
||||||
|
case gloox::ConnStreamVersionError:
|
||||||
|
error = "incoming stream's version is not supported";
|
||||||
|
break;
|
||||||
|
case gloox::ConnStreamClosed:
|
||||||
|
error = "stream has been closed (by the server)";
|
||||||
|
break;
|
||||||
|
case gloox::ConnProxyAuthRequired:
|
||||||
|
error = "HTTP/SOCKS5 proxy requires authentication";
|
||||||
|
break;
|
||||||
|
case gloox::ConnProxyAuthFailed:
|
||||||
|
error = "HTTP/SOCKS5 proxy authentication failed";
|
||||||
|
break;
|
||||||
|
case gloox::ConnProxyNoSupportedAuth:
|
||||||
|
error = "HTTP/SOCKS5 proxy requires an unsupported auth mechanism";
|
||||||
|
break;
|
||||||
|
case gloox::ConnIoError:
|
||||||
|
error = "I/O error occured";
|
||||||
|
break;
|
||||||
|
case gloox::ConnParseError:
|
||||||
|
error = "XML parse error occurred";
|
||||||
|
break;
|
||||||
|
case gloox::ConnConnectionRefused:
|
||||||
|
error = "connection was refused by the server (on the socket level)";
|
||||||
|
break;
|
||||||
|
case gloox::ConnDnsError:
|
||||||
|
error = "resolving the server's hostname failed";
|
||||||
|
break;
|
||||||
|
case gloox::ConnOutOfMemory:
|
||||||
|
error = "connection out of memory";
|
||||||
|
break;
|
||||||
|
case gloox::ConnNoSupportedAuth:
|
||||||
|
error = "auth mechanisms the server offers are not supported";
|
||||||
|
break;
|
||||||
|
case gloox::ConnTlsFailed:
|
||||||
|
error = "server's certificate could not be verified or the TLS";
|
||||||
|
break;
|
||||||
|
case gloox::ConnTlsNotAvailable:
|
||||||
|
error = "server didn't offer TLS while it was set to be required";
|
||||||
|
break;
|
||||||
|
case gloox::ConnCompressionFailed:
|
||||||
|
error = "negotiating/initializing compression failed";
|
||||||
|
break;
|
||||||
|
case gloox::ConnAuthenticationFailed: //TODO use ClientBase::authError() to find the reason. */
|
||||||
|
error = "authentication failed";
|
||||||
|
break;
|
||||||
|
|
||||||
|
case gloox::ConnUserDisconnected:
|
||||||
|
error = "user (or higher-level protocol) requested a disconnect";
|
||||||
|
break;
|
||||||
|
case gloox::ConnNotConnected:
|
||||||
|
error = "there is no active connection";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error.empty())
|
||||||
|
core->logger.log(Logger::info, "disconnected" , {"Connection"});
|
||||||
|
else
|
||||||
|
core->logger.log(Logger::error, "disconnected: " + error , {"Connection"});
|
||||||
|
}
|
||||||
|
bool Connection::onTLSConnect(const gloox::CertInfo&) {
|
||||||
|
core->logger.log(Logger::info, "TLS established", {"Connection"});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
5
jay.cpp
5
jay.cpp
@ -43,8 +43,10 @@ void Jay::initialize() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Jay::createActors() {
|
void Jay::createActors() {
|
||||||
for (const std::pair<const std::string, std::string>& pair : core->config.getActors())
|
for (const std::pair<const std::string, std::string>& pair : core->config.getActors()) {
|
||||||
|
core->logger.log(Logger::info, "registering actor " + pair.first + " as " + pair.second, {"Jay"});
|
||||||
core->router.registerActor(pair.first, pair.second);
|
core->router.registerActor(pair.first, pair.second);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Jay::createModules() {
|
void Jay::createModules() {
|
||||||
@ -53,6 +55,7 @@ void Jay::createModules() {
|
|||||||
if (!conf.enabled)
|
if (!conf.enabled)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
core->logger.log(Logger::info, "enabling module " + pair.first, {"Jay"});
|
||||||
modules.emplace_back(pair.second(core, connection));
|
modules.emplace_back(pair.second(core, connection));
|
||||||
core->router.registerModule(pair.first, modules.back());
|
core->router.registerModule(pair.first, modules.back());
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user