Update logger uses

FossilOrigin-Name: f02d3338fcde9da604abf0aa87e59fd1a5a5e8ce4eb5f97ae1e545b775c751e3
This commit is contained in:
nekobit 2022-12-31 03:01:37 +00:00
parent 5e422ee302
commit c301bdaf8e
10 changed files with 100 additions and 47 deletions

View file

@ -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)

View file

@ -5,6 +5,7 @@
#pragma once
#include <logger.h>
#include <filesystem>
#include <yaml-cpp/yaml.h>
#include <optional>
@ -33,6 +34,9 @@ namespace Config
Config::Instance instance;
Config::Frontend frontend;
} config;
private:
Logger::category_id cat_id;
};
// Global instance of config

View file

@ -12,22 +12,34 @@
std::shared_ptr<DB::Database> DB::load_db_from_cfg()
{
Logger::category_id log_id =
Logger::make_category("Database config");
std::shared_ptr<DB::Database> 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<DB::SQLite>(Config::instance().config.database.file);
database = std::make_shared<DB::SQLite>(
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;
}

View file

@ -7,7 +7,6 @@
#include <stdexcept>
#include <utility>
#include "sqlite.h"
#include <logger.h>
#include <variant>
#include <type_traits>
#include <cassert>
@ -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);
}
}

View file

@ -10,6 +10,7 @@
#include <utility>
#include <variant>
#include <filesystem>
#include <logger.h>
#include <memory>
#include <sqlite3.h>
#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;
};
}

View file

@ -118,6 +118,7 @@ struct FCGI_UnknownTypeRecord {
namespace {
static std::array<int, 6> 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<int>& 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;
}

View file

@ -7,14 +7,14 @@
#include "http/request.h"
#include <cstdint>
#include <iostream>
#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<HTTP::Response const> 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());
}

View file

@ -12,6 +12,7 @@
#include <utility>
#include <string_view>
#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<RequestCallbackPair_t> req_map;
Logger::category_id logger_cat;
private:
std::any cb_args;
/** Port number, should be set once */
uint16_t port;
};
}

View file

@ -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<HTTP::Request*>(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<MHDConnectionState*>(*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<MicroHttpdServer*>(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);
}

View file

@ -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