From b220361474fdf90f4b54e67f60ab2b33e027b510 Mon Sep 17 00:00:00 2001 From: adjoly Date: Fri, 11 Apr 2025 15:17:32 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20added=20log=20debug=20function=20with=20VERBOSE=20mode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exemples/test.toml | 1 + includes/log.hpp | 46 ++++++++++++++++++++++++++++++++----------- lib/tomlpp | 2 +- src/config/Server.cpp | 15 +++++++------- src/main.cpp | 5 ++--- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/exemples/test.toml b/exemples/test.toml index 777ed4b..f4b6ece 100644 --- a/exemples/test.toml +++ b/exemples/test.toml @@ -2,6 +2,7 @@ server_names = { "localhost", "2B5.local" } host = "localhost" port = 8080 +log_file = "test.log" [server.error_pages] 404 = "not_found.html" diff --git a/includes/log.hpp b/includes/log.hpp index b39558e..108f89e 100644 --- a/includes/log.hpp +++ b/includes/log.hpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */ -/* Updated: 2025/04/11 11:54:37 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 15:09:59 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,22 +19,44 @@ #include namespace webserv { + +/** + * @brief Used to log debug message + * + * @note Only work if VERBOSE mode is active + */ +static inline void log(std::string emoji, std::string who, + std::string str) { +#ifdef VERBOSE + if (who.empty()) + std::cout << "「" << emoji << "」debug: " << str << std::endl; + else + std::cout << "「" << emoji << "」debug(" << who << "): " << str + << std::endl; +#else + (void)emoji, (void)what, (void)who, (void)str; +#endif +} + class Logger { public: Logger(const std::string &fileName) : _fileName(fileName) { - if (fileName.empty()) + log("➕", "Logger", "filename constructor called"); + if (!fileName.empty()) { _ttyOnly = true; - else { + } else { + std::cout << "wtfff: " << _fileName << std::endl; _file.open(fileName.c_str(), std::ios::app); - _ttyOnly = false; - } - if (!_file.is_open() && !_ttyOnly) { - _ttyOnly = true; - warn("could not open log file, going tty only"); + if (!_file.is_open() && !_ttyOnly) { + _ttyOnly = true; + warn("could not open log file, going tty only"); + } else + _ttyOnly = false; } } Logger(const Logger &other) : _ttyOnly(other._ttyOnly) { + log("➕", "Logger", "copy constructor called"); if (!other._ttyOnly) { _file.open(other._fileName.c_str(), std::ios::app); if (!_file.is_open()) { @@ -46,6 +68,7 @@ class Logger { // Copy assignment operator Logger &operator=(const Logger &other) { + log("➕", "Logger", "copy assignment constructor called"); if (this != &other) { if (_file.is_open()) { _file.close(); @@ -62,6 +85,7 @@ class Logger { return *this; } ~Logger(void) { + log("➖", "Logger", "destructor called"); if (_file.is_open()) _file.close(); } @@ -90,10 +114,8 @@ class Logger { protected: private: - std::string printPogitMsg(const std::string &emoji, - const std::string &type, - const std::string &what, - const std::string &msg) { + std::string printPogitMsg(const std::string &emoji, const std::string &type, + const std::string &what, const std::string &msg) { std::stringstream os; #ifdef TTY (void)emoji; diff --git a/lib/tomlpp b/lib/tomlpp index d9e5070..9926aa1 160000 --- a/lib/tomlpp +++ b/lib/tomlpp @@ -1 +1 @@ -Subproject commit d9e507093a3b37d35d5115d766e6d81044cacb00 +Subproject commit 9926aa1530c08923d092df755fd5c876e56a409c diff --git a/src/config/Server.cpp b/src/config/Server.cpp index 1624708..a510406 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */ -/* Updated: 2025/04/11 12:12:56 by adjoly ### ########.fr */ +/* Updated: 2025/04/11 14:56:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,12 +52,6 @@ Server::Server(std::string file_name) { std::map *map; _table = tomlFile->getParsedFile(); - void *val = _table->access("log_file", toml::STRING, found); - std::string log_file = ""; - if (found == true && val != not_nullptr) { - std::string log_file = *static_cast(val); - } - _log = new Logger(log_file); try { _table = _getServerTable(); } catch(std::runtime_error &e) { @@ -66,6 +60,13 @@ Server::Server(std::string file_name) { delete _log; throw e; } + void *val = _table->access("log_file", toml::STRING, found); + std::string log_file = ""; + if (found == true && val != not_nullptr) { + std::string log_file = *static_cast(val); + std::cout << log_file << std::endl; + } + _log = new Logger(log_file); // host and port parsing void *host = accessValue("host", toml::STRING, _table, _log); diff --git a/src/main.cpp b/src/main.cpp index 13b1232..6595318 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,8 +6,7 @@ /* By: mmoussou getLogger()->info("testtt"); delete conf; }