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,6 +1,5 @@
|
|||
set(SOURCES
|
||||
config.cpp
|
||||
logger.cpp
|
||||
actor.cpp
|
||||
router.cpp
|
||||
core.cpp
|
||||
|
@ -8,7 +7,6 @@ set(SOURCES
|
|||
|
||||
set(HEADERS
|
||||
config.h
|
||||
logger.h
|
||||
actor.h
|
||||
router.h
|
||||
core.h
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
|
||||
#include "config.h"
|
||||
|
||||
Config::Config(const std::string& path):
|
||||
Config::Config(const std::filesystem::path& path):
|
||||
path(path),
|
||||
root(YAML::LoadFile(path))
|
||||
{}
|
||||
|
||||
|
@ -23,21 +24,21 @@ std::string Config::getFullJID() const {
|
|||
return getBareJID() + "/" + getResource();
|
||||
}
|
||||
|
||||
Logger::Level Config::getLogLevel() const {
|
||||
Shared::Logger::Level Config::getLogLevel() const {
|
||||
std::string level = root["logLevel"].as<std::string>("info");
|
||||
|
||||
if (level == "trace")
|
||||
return Logger::trace;
|
||||
return Shared::Logger::trace;
|
||||
else if (level == "debug")
|
||||
return Logger::debug;
|
||||
return Shared::Logger::debug;
|
||||
else if (level == "info")
|
||||
return Logger::info;
|
||||
return Shared::Logger::info;
|
||||
else if (level == "warn" || level == "warning")
|
||||
return Logger::warning;
|
||||
return Shared::Logger::warning;
|
||||
else if (level == "error")
|
||||
return Logger::error;
|
||||
return Shared::Logger::error;
|
||||
else
|
||||
return Logger::info;
|
||||
return Shared::Logger::info;
|
||||
}
|
||||
|
||||
gloox::TLSPolicy Config::getTLSPolicy() const {
|
||||
|
@ -115,3 +116,14 @@ Shared::Responses Config::getResponses() const {
|
|||
|
||||
return responses;
|
||||
}
|
||||
|
||||
void Config::setActors(const std::map<std::string, std::string>& actors) {
|
||||
YAML::Node cfg = root["actors"] = YAML::Node(YAML::NodeType::Map);
|
||||
|
||||
for (const std::pair<const std::string, std::string>& pair : actors)
|
||||
cfg[pair.first] = pair.second;
|
||||
|
||||
std::ofstream out(path);
|
||||
out << YAML::Dump(root);
|
||||
out.close();
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@
|
|||
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
|
||||
#include "gloox/gloox.h"
|
||||
|
||||
|
@ -12,7 +14,7 @@
|
|||
|
||||
#include "shared/definitions.h"
|
||||
#include "shared/result.h"
|
||||
#include "logger.h"
|
||||
#include "shared/logger.h"
|
||||
|
||||
class Config {
|
||||
public:
|
||||
|
@ -23,7 +25,7 @@ public:
|
|||
};
|
||||
|
||||
public:
|
||||
Config(const std::string& path);
|
||||
Config(const std::filesystem::path& path);
|
||||
|
||||
bool isValid() const;
|
||||
|
||||
|
@ -32,11 +34,14 @@ public:
|
|||
std::string getPassword() const;
|
||||
std::string getResource() const;
|
||||
std::map<std::string, std::string> getActors() const;
|
||||
Logger::Level getLogLevel() const;
|
||||
Shared::Logger::Level getLogLevel() const;
|
||||
gloox::TLSPolicy getTLSPolicy() const;
|
||||
Module getModuleConfig(const std::string& name) const;
|
||||
Shared::Responses getResponses() const;
|
||||
|
||||
void setActors(const std::map<std::string, std::string>& actors);
|
||||
|
||||
private:
|
||||
std::filesystem::path path;
|
||||
YAML::Node root;
|
||||
};
|
||||
|
|
|
@ -15,7 +15,7 @@ Core::Core(const std::string& configPath):
|
|||
void Core::send(const std::string& jid, const std::string& body) {
|
||||
std::shared_ptr<Connection> cn = connection.lock();
|
||||
if (!cn) {
|
||||
logger.log(Logger::warning, "Couldn't send a message to " + jid + ", connection is not available", {"Core"});
|
||||
logger.log(Shared::Logger::warning, "Couldn't send a message to " + jid + ", connection is not available", {"Core"});
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -37,12 +37,15 @@ void Core::initialize(const std::shared_ptr<Connection>& cn) {
|
|||
|
||||
void Core::setGroup(const std::string& jid, const std::string& group) {
|
||||
router.registerActor(jid, group);
|
||||
// todo save config
|
||||
|
||||
std::map<std::string, std::string> actors = router.getActors();
|
||||
actors["default"] = router.getDefaultGroup();
|
||||
config.setActors(actors);
|
||||
}
|
||||
|
||||
void Core::initializeActors() {
|
||||
for (const std::pair<const std::string, std::string>& pair : config.getActors()) {
|
||||
logger.log(Logger::info, "registering actor " + pair.first + " as " + pair.second, {"Core"});
|
||||
logger.log(Shared::Logger::info, "registering actor " + pair.first + " as " + pair.second, {"Core"});
|
||||
router.registerActor(pair.first, pair.second);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#include "shared/logger.h"
|
||||
#include "config.h"
|
||||
#include "router.h"
|
||||
#include "logger.h"
|
||||
|
||||
class Connection;
|
||||
|
||||
|
@ -22,7 +22,7 @@ public:
|
|||
|
||||
public:
|
||||
Config config;
|
||||
Logger logger;
|
||||
Shared::Logger logger;
|
||||
Router router;
|
||||
|
||||
private:
|
||||
|
|
|
@ -1,156 +0,0 @@
|
|||
// 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[Logger::convert(level)] : "UNKNOWN";
|
||||
}
|
||||
|
||||
constexpr std::string_view getColor(gloox::LogLevel level) {
|
||||
return (level >= 0 && level < 3) ? colorMap[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';
|
||||
}
|
||||
|
||||
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() {}
|
||||
|
||||
void 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 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;
|
||||
}
|
||||
|
|
@ -1,35 +0,0 @@
|
|||
// 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>
|
||||
|
||||
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;
|
||||
};
|
|
@ -6,8 +6,8 @@
|
|||
#include "module/module.h"
|
||||
#include "connection/connection.h"
|
||||
|
||||
Router::Router(const Logger& logger):
|
||||
logger(logger),
|
||||
Router::Router(const Shared::Logger& logger):
|
||||
Shared::Loggable(logger, {"Router"}),
|
||||
modules(),
|
||||
actors(),
|
||||
defaultGroup("Stranger"),
|
||||
|
@ -44,13 +44,13 @@ void Router::routeMessage(const std::string& sender, const std::string& body) {
|
|||
std::vector<std::string> args = Module::Module::split(body);
|
||||
Modules::iterator mItr = modules.find(args[0]);
|
||||
if (mItr == modules.end()) {
|
||||
logger.log(Logger::debug, "could not find module \"" + args[0] + "\" to handle message from " + sender, {"Router"});
|
||||
debug("could not find module \"" + args[0] + "\" to handle message from " + sender);
|
||||
return onMessageResult(Shared::unhandled, sender);
|
||||
}
|
||||
|
||||
std::shared_ptr<Module::Module> module = mItr->second.lock();
|
||||
if (!module) {
|
||||
logger.log(Logger::error, "could not lock module \"" + mItr->first + "\" to handle message from " + sender, {"Router"});
|
||||
error("could not lock module \"" + mItr->first + "\" to handle message from " + sender);
|
||||
return onMessageResult(Shared::error, sender);
|
||||
}
|
||||
|
||||
|
@ -59,9 +59,9 @@ void Router::routeMessage(const std::string& sender, const std::string& body) {
|
|||
try {
|
||||
result = module->message(aItr->second, args);
|
||||
if (result == Shared::success)
|
||||
logger.log(Logger::debug, "module \"" + mItr->first + "\" successfully handled message from " + sender, {"Router"});
|
||||
debug("module \"" + mItr->first + "\" successfully handled message from " + sender);
|
||||
} catch (...) {
|
||||
logger.log(Logger::error, "module \"" + mItr->first + "\" thew an unhandled exception handling message from " + sender, {"Router"});
|
||||
error("module \"" + mItr->first + "\" thew an unhandled exception handling message from " + sender);
|
||||
result = Shared::error;
|
||||
}
|
||||
|
||||
|
@ -77,6 +77,10 @@ std::map<std::string, std::string> Router::getActors() const {
|
|||
return result;
|
||||
}
|
||||
|
||||
std::string Router::getDefaultGroup() const {
|
||||
return defaultGroup;
|
||||
}
|
||||
|
||||
void Router::setConnection(const std::shared_ptr<Connection>& cn) {
|
||||
connection = cn;
|
||||
}
|
||||
|
@ -100,7 +104,7 @@ void Router::onMessageResult(Shared::Result result, const std::string& sender) {
|
|||
|
||||
std::shared_ptr<Connection> cn = connection.lock();
|
||||
if (!cn) {
|
||||
logger.log(Logger::warning, "Couldn't send a message to " + sender + ", connection is not available", {"Router"});
|
||||
warn("Couldn't send a message to " + sender + ", connection is not available");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
#include <random>
|
||||
|
||||
#include "shared/result.h"
|
||||
#include "shared/loggable.h"
|
||||
#include "actor.h"
|
||||
#include "logger.h"
|
||||
|
||||
namespace Module {
|
||||
class Module;
|
||||
|
@ -19,15 +19,16 @@ namespace Module {
|
|||
|
||||
class Connection;
|
||||
|
||||
class Router {
|
||||
class Router : private Shared::Loggable {
|
||||
public:
|
||||
Router(const Logger& logger);
|
||||
Router(const Shared::Logger& logger);
|
||||
|
||||
void registerModule(const std::string& key, const std::shared_ptr<Module::Module>& module);
|
||||
void registerActor(const std::string& key, const std::string& group);
|
||||
void routeMessage(const std::string& sender, const std::string& body);
|
||||
|
||||
std::map<std::string, std::string> getActors() const;
|
||||
std::string getDefaultGroup() const;
|
||||
|
||||
void setConnection(const std::shared_ptr<Connection>& connection);
|
||||
void setResponses(Shared::Result result, const Shared::Strings& options);
|
||||
|
@ -43,7 +44,6 @@ private:
|
|||
typedef std::map<Shared::Result, List> Responses;
|
||||
|
||||
std::weak_ptr<Connection> connection;
|
||||
const Logger& logger;
|
||||
Modules modules;
|
||||
Actors actors;
|
||||
std::string defaultGroup;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue