Improve logger

FossilOrigin-Name: 110e8c9b333f60d8ad967a35431f06286e91ec0cfa605c62f868ca4b52d7dbb7
This commit is contained in:
nekobit 2022-12-30 23:07:28 +00:00
parent b30bc9530c
commit 5e422ee302
3 changed files with 57 additions and 133 deletions

View file

@ -3,33 +3,33 @@
* Licensed under BSD 3-Clause. See LICENSE
*/
#include <iostream>
#include <utility>
#include <ctime>
#include <optional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <sstream>
#include <utility>
#include <vector>
#include <ctime>
#include "logger.h"
namespace
namespace { std::vector<std::string> categories; }
// Category is made
Logger::category_id
Logger::make_category(std::string name)
{
Logger::Logger inst{};
bool inst_setup = false;
int id = ::categories.size();
::categories.push_back(name);
return id;
}
using Level = Logger::Level;
using Log = Logger::Log;
Logger::Logger::Logger(bool log_started)
: logs{},
hook{[](const Log&){}}
static std::string
pretty_time(const std::string& format)
{
if (log_started)
*this << "Logging started";
}
using clock_type = typename std::chrono::system_clock;
const std::chrono::time_point<clock_type>
time_point;
/// Returns the time as a std::string in a specified format
const std::string Log::pretty_time(const std::string& format) const
{
std::stringstream time_str;
const std::time_t c_time = clock_type::to_time_t(time_point);
// time_t doesn't specify it's output, so write into string stream and return that
@ -38,52 +38,24 @@ const std::string Log::pretty_time(const std::string& format) const
return time_str.str();
}
void Logger::Logger::set_log_hook(std::function<void(const Log&)> func)
{
this->hook = func;
}
/// Creates a log based on the current time
Log Logger::Logger::make_log(std::string message, Level level)
void
Logger::log(Logger::category_id id,
Logger::Level lvl,
std::string const& message)
{
auto time = clock_type::now();
return { level, std::move(time), std::move(message) };
}
std::stringstream s;
void Logger::Logger::log(std::string message, Level level)
{
const Log l = make_log(std::move(message), level);
logs.emplace_back(l);
hook(l);
}
Logger::Logger& Logger::instance()
{
if (!inst_setup)
s << "[" << pretty_time("") << " | " << categories.at(id) <<" ] " <<
"<" << Logger::level_string[static_cast<int>(lvl)] << "> " <<
message;
if (lvl == Logger::Level::WARN)
{
inst.set_log_hook([](const Log& log) {
std::cout << log << std::endl;
});
inst_setup = true;
std::cerr << s.rdbuf() << std::endl;
}
else {
std::cout << s.rdbuf() << std::endl;
}
return inst;
}
void Logger::Logger::operator<<(std::string message)
{
const Log l = make_log(std::move(message));
logs.emplace_back(l);
// Call hook
hook(l);
}
std::ostream& operator<<(std::ostream& os, const Logger::Log& log)
{
std::stringstream stream;
stream << "[" << log.pretty_time()<< "] " <<
"<" << Logger::level_string[static_cast<int>(log.level)] << "> " <<
log.message;
os << stream.str();
return os;
}

View file

@ -6,7 +6,9 @@
// Note: I copied this OOP garbage from another project
// Just your generic, average logger
#pragma once
#ifndef LOGGER_H
#define LOGGER_H
#include <functional>
#include <vector>
#include <chrono>
@ -26,8 +28,8 @@
namespace Logger
{
using clock_type = typename std::chrono::system_clock;
using log_time_point = std::chrono::time_point<clock_type>;
using category_id = int;
static const std::string level_string[] = {
"Debug",
@ -43,71 +45,18 @@ namespace Logger
WARN,
ERROR
};
/**
* Creates a category.
*
* @return ID of this category, remember it!
*/
category_id make_category(std::string name);
struct Log
{
const std::string pretty_time(const std::string& format = "%T") const;
// Members
Logger::Level level;
const log_time_point time_point;
std::string message;
};
class Logger
{
public:
// BEGIN ITERATOR
struct Iterator
{
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = Log;
Iterator(Log* log) : p{log} {}
// Common operators
Log& operator*() const { return *p; }
Log* operator->() { return p; }
// Prefix op
Iterator& operator++() { ++p; return *this; }
// Postfix op
Iterator operator++(int) { Iterator t = *this; ++(*this); return t; }
friend bool operator== (const Iterator& a, const Iterator& b)
{ return a.p == b.p; };
friend bool operator!= (const Iterator& a, const Iterator& b)
{ return a.p != b.p; };
private:
Log* p;
};
// Iterator methods
Iterator begin() { return Iterator{&logs[0]}; }
Iterator end() { return Iterator{&logs[logs.size()]}; }
// END ITERATOR
Logger(bool log_started = false);
~Logger() = default;
/// Function is called each time a new log is added
void set_log_hook(std::function<void(const Log&)> func);
static Log make_log(std::string message, Level level = Level::INFO);
void log(std::string message, Level level = Level::INFO);
void operator<<(std::string message);
private:
std::vector<Log> logs;
std::function<void(const Log&)> hook;
};
// This is retarded *shrugs*
Logger& instance();
void
log(int category,
Logger::Level lvl,
std::string const& message);
}
std::ostream& operator<<(std::ostream& os, const Logger::Log& log);
#endif // LOGGER_H

View file

@ -8,10 +8,13 @@
#include "http/httpserver.h"
#include "http/response.h"
HTTP::Response
Route::MastoAPI::create_app(std::any& args,
HTTP::Request& req,
const HTTP::RequestArgs_t& arg);
namespace Route
{
HTTP::Response
Route::MastoAPI::create_app(std::any &args, HTTP::Request &req,
const HTTP::RequestArgs_t &arg);
}
void
Protocol::MastoAPI::init_masto_apps(HTTP::Server* server);