🏗️」 wip: added log debug function with VERBOSE mode

This commit is contained in:
2025-04-11 15:17:32 +02:00
parent bb841f90c8
commit b220361474
5 changed files with 46 additions and 23 deletions

View File

@ -2,6 +2,7 @@
server_names = { "localhost", "2B5.local" } server_names = { "localhost", "2B5.local" }
host = "localhost" host = "localhost"
port = 8080 port = 8080
log_file = "test.log"
[server.error_pages] [server.error_pages]
404 = "not_found.html" 404 = "not_found.html"

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 09:28:27 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 <string> #include <string>
namespace webserv { 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 { class Logger {
public: public:
Logger(const std::string &fileName) : _fileName(fileName) { Logger(const std::string &fileName) : _fileName(fileName) {
if (fileName.empty()) log("", "Logger", "filename constructor called");
if (!fileName.empty()) {
_ttyOnly = true; _ttyOnly = true;
else { } else {
std::cout << "wtfff: " << _fileName << std::endl;
_file.open(fileName.c_str(), std::ios::app); _file.open(fileName.c_str(), std::ios::app);
_ttyOnly = false;
}
if (!_file.is_open() && !_ttyOnly) { if (!_file.is_open() && !_ttyOnly) {
_ttyOnly = true; _ttyOnly = true;
warn("could not open log file, going tty only"); warn("could not open log file, going tty only");
} else
_ttyOnly = false;
} }
} }
Logger(const Logger &other) : _ttyOnly(other._ttyOnly) { Logger(const Logger &other) : _ttyOnly(other._ttyOnly) {
log("", "Logger", "copy constructor called");
if (!other._ttyOnly) { if (!other._ttyOnly) {
_file.open(other._fileName.c_str(), std::ios::app); _file.open(other._fileName.c_str(), std::ios::app);
if (!_file.is_open()) { if (!_file.is_open()) {
@ -46,6 +68,7 @@ class Logger {
// Copy assignment operator // Copy assignment operator
Logger &operator=(const Logger &other) { Logger &operator=(const Logger &other) {
log("", "Logger", "copy assignment constructor called");
if (this != &other) { if (this != &other) {
if (_file.is_open()) { if (_file.is_open()) {
_file.close(); _file.close();
@ -62,6 +85,7 @@ class Logger {
return *this; return *this;
} }
~Logger(void) { ~Logger(void) {
log("", "Logger", "destructor called");
if (_file.is_open()) if (_file.is_open())
_file.close(); _file.close();
} }
@ -90,10 +114,8 @@ class Logger {
protected: protected:
private: private:
std::string printPogitMsg(const std::string &emoji, std::string printPogitMsg(const std::string &emoji, const std::string &type,
const std::string &type, const std::string &what, const std::string &msg) {
const std::string &what,
const std::string &msg) {
std::stringstream os; std::stringstream os;
#ifdef TTY #ifdef TTY
(void)emoji; (void)emoji;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 15:10:07 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<std::string, toml::ANode *> *map; std::map<std::string, toml::ANode *> *map;
_table = tomlFile->getParsedFile(); _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<std::string *>(val);
}
_log = new Logger(log_file);
try { try {
_table = _getServerTable(); _table = _getServerTable();
} catch(std::runtime_error &e) { } catch(std::runtime_error &e) {
@ -66,6 +60,13 @@ Server::Server(std::string file_name) {
delete _log; delete _log;
throw e; 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<std::string *>(val);
std::cout << log_file << std::endl;
}
_log = new Logger(log_file);
// host and port parsing // host and port parsing
void *host = accessValue("host", toml::STRING, _table, _log); void *host = accessValue("host", toml::STRING, _table, _log);

View File

@ -6,8 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */ /* Created: 2025/02/03 15:45:07 by mmoussou #+# #+# */
/* Updated: 2025/04/10 12:18:39 by mmoussou ### ########.fr */ /* Updated: 2025/04/11 14:52:21 by adjoly ### ########.fr */
/* Updated: 2025/03/25 17:10:29 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,6 +36,6 @@ int main(int ac, char **av) {
std::cout << e.what() << std::endl; std::cout << e.what() << std::endl;
return 1; return 1;
} }
(void)conf; conf->getLogger()->info("testtt");
delete conf; delete conf;
} }