Setup global logger
FossilOrigin-Name: 1e2199932bf6044bd7a41cf1d61e2ad2146352d3b714e30da2fe5c46d855fc44
This commit is contained in:
parent
d11810f245
commit
8bc17c9346
8 changed files with 40 additions and 7 deletions
|
@ -6,6 +6,7 @@ include(CTest)
|
|||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
find_package(Threads REQUIRED)
|
||||
find_package(SQLite3)
|
||||
pkg_check_modules(LIBMICROHTTPD REQUIRED libmicrohttpd)
|
||||
pkg_check_modules(YAML_CPP REQUIRED yaml-cpp)
|
||||
|
||||
|
@ -18,6 +19,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
|
|||
include_directories(wormhole
|
||||
${LIBMICROHTTPD_INCLUDE_DIRS}
|
||||
${YAML_CPP_INCLUDE_DIRS}
|
||||
${SQLite3_INCLUDE_DIRS}
|
||||
src/
|
||||
)
|
||||
|
||||
|
@ -42,4 +44,5 @@ add_executable(wormhole ${sources})
|
|||
target_link_libraries(wormhole ${CMAKE_DL_LIBS}
|
||||
${LIBMICROHTTPD_LIBRARIES}
|
||||
${YAML_CPP_LIBRARIES}
|
||||
${SQLite3_LIBRARIES}
|
||||
${CMAKE_THREAD_LIBS_INIT})
|
||||
|
|
|
@ -21,7 +21,8 @@
|
|||
using namespace DB;
|
||||
|
||||
SQLite::SQLite()
|
||||
: DB::Database()
|
||||
: DB::Database(),
|
||||
db(nullptr, SQLiteDeleter())
|
||||
{
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <sqlite3.h>
|
||||
#include "../database.h"
|
||||
|
||||
namespace DB
|
||||
|
@ -28,5 +30,7 @@ namespace DB
|
|||
explicit SQLite();
|
||||
virtual ~SQLite() = default;
|
||||
private:
|
||||
struct SQLiteDeleter { void operator()(sqlite3* db) { sqlite3_close(db); } };
|
||||
std::unique_ptr<sqlite3, SQLiteDeleter> db;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ void Server::stop()
|
|||
std::cerr << "Notice: Using base stop(), should be inherited" << std::endl;
|
||||
}
|
||||
|
||||
void Server::map_requests(std::initializer_list<RequestCallbackPair_t> requests)
|
||||
void Server::map_routes(std::initializer_list<RequestCallbackPair_t> requests)
|
||||
{
|
||||
for (auto& req: requests)
|
||||
{
|
||||
|
|
|
@ -48,14 +48,14 @@ namespace HTTP
|
|||
virtual void stop();
|
||||
|
||||
/**
|
||||
* @brief Maps a list of requests for the server to work with.
|
||||
* @brief Maps a list of routes for the server to work with.
|
||||
*
|
||||
* @see HTTP::Request
|
||||
*
|
||||
* These are mainly URLs and the request type that the HTTP server will
|
||||
* map to a function
|
||||
*/
|
||||
void map_requests(std::initializer_list<RequestCallbackPair_t> requests);
|
||||
void map_routes(std::initializer_list<RequestCallbackPair_t> requests);
|
||||
|
||||
/**
|
||||
* @brief Handles a crafted HTTP Request
|
||||
|
|
|
@ -16,13 +16,21 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <ctime>
|
||||
#include <optional>
|
||||
#include <iomanip>
|
||||
#include "logger.h"
|
||||
|
||||
using Logger::Level;
|
||||
using Logger::Log;
|
||||
namespace
|
||||
{
|
||||
Logger::Logger inst{};
|
||||
bool inst_setup = false;
|
||||
}
|
||||
|
||||
using Level = Logger::Level;
|
||||
using Log = Logger::Log;
|
||||
|
||||
Logger::Logger::Logger(bool log_started)
|
||||
: logs{},
|
||||
|
@ -62,6 +70,18 @@ void Logger::Logger::log(std::string message, Level level)
|
|||
hook(l);
|
||||
}
|
||||
|
||||
Logger::Logger& Logger::instance()
|
||||
{
|
||||
if (inst_setup)
|
||||
{
|
||||
inst.set_log_hook([](const Log& log) {
|
||||
std::cout << log << std::endl;
|
||||
});
|
||||
inst_setup = true;
|
||||
}
|
||||
return inst;
|
||||
}
|
||||
|
||||
void Logger::Logger::operator<<(std::string message)
|
||||
{
|
||||
const Log l = make_log(std::move(message));
|
||||
|
|
|
@ -95,6 +95,7 @@ namespace Logger
|
|||
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);
|
||||
|
||||
|
@ -106,6 +107,8 @@ namespace Logger
|
|||
std::vector<Log> logs;
|
||||
std::function<void(const Log&)> hook;
|
||||
};
|
||||
|
||||
Logger& instance();
|
||||
}
|
||||
|
||||
static std::ostream& operator<<(std::ostream& os, const Logger::Log& log)
|
||||
|
|
|
@ -31,7 +31,8 @@ void start_wormhole()
|
|||
|
||||
std::unique_ptr<HTTP::Server> server =
|
||||
std::make_unique<HTTP::MicroHttpdServer>(4000);
|
||||
server->map_requests({
|
||||
|
||||
server->map_routes({
|
||||
|
||||
{ {HTTP::Request::Type::GET, "/test"}, [](const HTTP::RequestArgs_t& arg){
|
||||
return HTTP::Response{"<html><body>Hello, browser!</body></html>"};
|
||||
|
@ -44,6 +45,7 @@ void start_wormhole()
|
|||
|
||||
server->start();
|
||||
|
||||
Logger::instance() << "Loading config";
|
||||
|
||||
getchar();
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue