diff --git a/src/config/config_loader.cpp b/src/config/config_loader.cpp index 498f48e..35302d7 100644 --- a/src/config/config_loader.cpp +++ b/src/config/config_loader.cpp @@ -21,6 +21,7 @@ ConfigLoader& Config::instance() } ConfigLoader::ConfigLoader() + : cat_id{ Logger::make_category("Config") } {} void ConfigLoader::load_config(const std::filesystem::path& path) @@ -34,9 +35,12 @@ void ConfigLoader::load_config(const std::filesystem::path& path) Config::load_instance(config.instance, node); Config::load_frontend(config.frontend, node); } - catch (const YAML::BadFile& err) - { - Logger::instance().log("Couldn't load config: \""s + path.string() + "\"", Logger::Level::ERROR); + catch (const YAML::BadFile& err) { + Logger::log( + cat_id, + Logger::Level::ERROR, + "Couldn't load config: \""s + path.string() + "\""); + throw err; } catch (const YAML::BadConversion& err) diff --git a/src/config/config_loader.h b/src/config/config_loader.h index 8fbee49..628fd72 100644 --- a/src/config/config_loader.h +++ b/src/config/config_loader.h @@ -5,6 +5,7 @@ #pragma once +#include #include #include #include @@ -33,6 +34,9 @@ namespace Config Config::Instance instance; Config::Frontend frontend; } config; + + private: + Logger::category_id cat_id; }; // Global instance of config diff --git a/src/database/database_config.cpp b/src/database/database_config.cpp index 32b0c64..e1d0c81 100644 --- a/src/database/database_config.cpp +++ b/src/database/database_config.cpp @@ -12,22 +12,34 @@ std::shared_ptr DB::load_db_from_cfg() { + Logger::category_id log_id = + Logger::make_category("Database config"); std::shared_ptr database = nullptr; // Select Database switch (Config::instance().config.database.type) { case DB::Database::Type::Unset: - Logger::instance().log("Database not set in config.yaml, using SQLite3!", Logger::Level::WARN); + Logger::log(log_id, + Logger::Level::WARN, + "Database not set in config.yaml, using SQLite3!"); // Fall + case DB::Database::Type::SQLite: - database = std::make_shared(Config::instance().config.database.file); + database = std::make_shared( + Config::instance().config.database.file); break; + case DB::Database::Type::PostgreSQL: - Logger::instance() << "Not implemented!"; + Logger::log(log_id, + Logger::Level::WARN, + "Not implemented!"); return nullptr; + default: - Logger::instance() << "Unsupported Database!"; + Logger::log(log_id, + Logger::Level::WARN, + "Unsupported Database!"); return nullptr; } diff --git a/src/database/sqlite/sqlite.cpp b/src/database/sqlite/sqlite.cpp index 3577f8f..951dd9e 100644 --- a/src/database/sqlite/sqlite.cpp +++ b/src/database/sqlite/sqlite.cpp @@ -7,7 +7,6 @@ #include #include #include "sqlite.h" -#include #include #include #include @@ -18,19 +17,22 @@ using namespace std::string_literals; SQLite::SQLite(std::filesystem::path path) : DB::Database(), - path{std::move(path)}, - db{nullptr}, - sql_cache() + path{ std::move(path) }, + db{ nullptr }, + sql_cache(), + logger_cat{ Logger::make_category("SQLite") } { if (sqlite3_open(this->path.c_str(), &db)) { const std::string err_msg = "Can't open database: "s + - std::string(sqlite3_errmsg(db)); - sqlite3_close(db); - throw std::runtime_error(err_msg); + std::string( sqlite3_errmsg(db) ); + sqlite3_close( db ); + throw std::runtime_error( err_msg ); } - Logger::instance().log("SQLite3 database opened at "s + this->path.string(), Logger::Level::DEBUG); + Logger::log(logger_cat, + Logger::Level::DEBUG, + "SQLite3 database opened at "s + this->path.string()); } SQLite::~SQLite() @@ -51,7 +53,9 @@ int SQLite::sqlite_exec(SQLiteCacheIndex idx, sqlite3_stmt* stmt = sql_cache[idx]; assert(stmt != nullptr); - Logger::instance().log("Executing query: "s + sqlite3_sql(stmt), Logger::Level::DEBUG); + Logger::log(logger_cat, + Logger::Level::DEBUG, + "Executing query: "s + sqlite3_sql(stmt)); int query_count = 0; @@ -89,7 +93,9 @@ int SQLite::sqlite_exec(SQLiteCacheIndex idx, switch (code) { case SQLITE_BUSY: - Logger::instance().log("SQLite database is busy!", Logger::Level::DEBUG); + Logger::log(logger_cat, + Logger::Level::DEBUG, + "SQLite database is busy!"); break; case SQLITE_ERROR: default: @@ -435,8 +441,14 @@ CREATE TABLE IF NOT EXISTS config ( { if (sqlite3_exec(db, sql, nullptr, nullptr, &err)) { - Logger::instance().log("Statement: "s + sql, Logger::Level::ERROR); - Logger::instance().log(err, Logger::Level::ERROR); + Logger::log(logger_cat, + Logger::Level::ERROR, + "Statement: "s + sql); + + Logger::log(logger_cat, + Logger::Level::ERROR, + err); + throw std::runtime_error(err); } } diff --git a/src/database/sqlite/sqlite.h b/src/database/sqlite/sqlite.h index 356deb8..3a3afc8 100644 --- a/src/database/sqlite/sqlite.h +++ b/src/database/sqlite/sqlite.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include "../database.h" @@ -78,5 +79,6 @@ namespace DB std::filesystem::path path; struct SQLiteDeleter { void operator()(sqlite3* db) { sqlite3_close(db); } }; sqlite3* db; + Logger::category_id logger_cat; }; } diff --git a/src/frontends/fcgi/fcgi.cpp b/src/frontends/fcgi/fcgi.cpp index 73e3ba6..6551b36 100644 --- a/src/frontends/fcgi/fcgi.cpp +++ b/src/frontends/fcgi/fcgi.cpp @@ -118,6 +118,7 @@ struct FCGI_UnknownTypeRecord { namespace { static std::array fcgi_fd{}; + static int logger_cat = Logger::make_category("FCGI"); enum fcgi_fd_pipes { @@ -128,7 +129,7 @@ namespace { STDERR_RD, STDERR_WR, }; - + // Helper function for reading a pipe int selector(const std::vector& fds) { @@ -147,7 +148,10 @@ namespace { int rc = select(max_fd+1, &rfds, nullptr, nullptr, nullptr); if (rc == -1) { - Logger::instance().log("select(): "s + std::strerror(errno), Logger::Level::ERROR); + Logger::log( + logger_cat, + Logger::Level::ERROR, + "select(): "s + std::strerror(errno)); } return rc; @@ -230,7 +234,10 @@ void Frontend::fork_fcgi_process() bool is_parent = pid != 0; if (is_parent) { - Logger::instance() << "Executing frontend \"" + exec + "\" in process " + std::to_string(pid) + "..."; + Logger::log( + logger_cat, + Logger::Level::INFO, + "Executing frontend \"" + exec + "\" in process " + std::to_string(pid) + "..."); return; } diff --git a/src/http/httpserver.cpp b/src/http/httpserver.cpp index f7671b3..4e135fa 100644 --- a/src/http/httpserver.cpp +++ b/src/http/httpserver.cpp @@ -7,14 +7,14 @@ #include "http/request.h" #include #include -#include "logger.h" using namespace HTTP; Server::Server(uint16_t port, std::any callback_args) : port{port}, req_map(), - cb_args(callback_args) + cb_args(callback_args), + logger_cat( Logger::make_category("HTTP Logger") ) {} void Server::start() @@ -55,5 +55,8 @@ std::optional Server::handle_request(HTTP::Request &reques void Server::log_request(const HTTP::Request& request) { - Logger::instance() << std::string(request.get_type_string()) + " " + request.get_url(); + Logger::log( + this->logger_cat, + Logger::Level::INFO, + std::string(request.get_type_string()) + " " + request.get_url()); } diff --git a/src/http/httpserver.h b/src/http/httpserver.h index 6989f1d..7e448cc 100644 --- a/src/http/httpserver.h +++ b/src/http/httpserver.h @@ -12,6 +12,7 @@ #include #include #include "request.h" +#include "logger.h" #include "response.h" namespace HTTP @@ -70,11 +71,11 @@ namespace HTTP inline uint16_t get_port() const noexcept { return port; } protected: std::vector req_map; + Logger::category_id logger_cat; private: std::any cb_args; /** Port number, should be set once */ uint16_t port; - }; } diff --git a/src/http/microhttpd_server.cpp b/src/http/microhttpd_server.cpp index 59f38e7..66ba5cd 100644 --- a/src/http/microhttpd_server.cpp +++ b/src/http/microhttpd_server.cpp @@ -29,7 +29,8 @@ struct MHDConnectionState namespace { - HTTP::Request::Type str_to_method(std::string method) + HTTP::Request::Type + str_to_method(std::string method) { // i.e. if (method == "GET") return HTTP::Request::Type::GET; #define IF_METHOD(val) if (method == #val) return HTTP::Request::Type::val @@ -43,9 +44,10 @@ namespace // Iterator function for POST, GET, etc. // - enum MHD_Result gcv_iterator(void* cls, MHD_ValueKind kind, - const char* key, size_t key_len, - const char* value, size_t value_len) + enum MHD_Result + gcv_iterator(void* cls, MHD_ValueKind kind, + const char* key, size_t key_len, + const char* value, size_t value_len) { HTTP::Request& request = *reinterpret_cast(cls); @@ -70,10 +72,11 @@ namespace } - void request_completed(void* cls, - struct MHD_Connection* connection, - void** con_cls, - enum MHD_RequestTerminationCode toe) + void + request_completed(void* cls, + struct MHD_Connection* connection, + void** con_cls, + enum MHD_RequestTerminationCode toe) { MHDConnectionState* con_info = reinterpret_cast(*con_cls); @@ -85,14 +88,15 @@ namespace } // See libmicrohttpd docs, this function will call itself recursively - enum MHD_Result new_connection(void* cls, - MHD_Connection* conn, - const char* url, - const char* _method, - const char* version, - const char* post_data, - size_t* post_data_size, - void** con_cls) + enum MHD_Result + new_connection(void* cls, + MHD_Connection* conn, + const char* url, + const char* _method, + const char* version, + const char* post_data, + size_t* post_data_size, + void** con_cls) { MicroHttpdServer* that = reinterpret_cast(cls); MHD_Response* response; @@ -105,8 +109,7 @@ namespace *con_cls = new MHDConnectionState{}; return MHD_YES; } - else if (*post_data_size) - { + else if (*post_data_size) { state->data += std::string(post_data, post_data + *post_data_size); *post_data_size = 0; return MHD_YES; @@ -177,9 +180,10 @@ void MicroHttpdServer::start() if (!dm) { // MicroHTTPD seems to keep the errors in errno, but doesn't really have a good error system - Logger::instance().log("Couldn't start MicroHTTPD daemon at port "s + std::to_string(get_port()) + - ": " + std::strerror(errno), - Logger::Level::ERROR); + Logger::log(logger_cat, + Logger::Level::ERROR, + "Couldn't start MicroHTTPD daemon at port "s + std::to_string(get_port()) + + ": " + std::strerror(errno)); exit(1); } diff --git a/src/logger.h b/src/logger.h index 00216a3..01f2325 100644 --- a/src/logger.h +++ b/src/logger.h @@ -57,6 +57,10 @@ namespace Logger log(int category, Logger::Level lvl, std::string const& message); + + void + log(Logger::Level lvl, + std::string const& message); } #endif // LOGGER_H