Skip to content

Commit

Permalink
fix: console logger
Browse files Browse the repository at this point in the history
  • Loading branch information
araujo88 committed Mar 25, 2024
1 parent 7ea470e commit acdfc8d
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 93 deletions.
14 changes: 2 additions & 12 deletions include/console_logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@
#define CONSOLE_LOGGER_H_

#include "logger.hpp"
#include <iostream>
#include <string>
#include <chrono>
#include <ctime>
#include <iomanip>

namespace tpt
{
class ConsoleLogger : public ILogger
class ConsoleLogger : public Logger
{
public:
virtual void log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0) override;
virtual void debug(const std::string &message) override;
virtual void info(const std::string &message) override;
virtual void warning(const std::string &message) override;
virtual void error(const std::string &message) override;
virtual void critical(const std::string &message) override;
virtual void log(const std::string &message, tpt::LogLevel level, const char *file, int line) override;
};
}

Expand Down
33 changes: 19 additions & 14 deletions include/logger.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#ifndef LOGGER_H_
#define LOGGER_H_

#include <iostream>
#include <string>
#include <chrono>
#include <ctime>
#include <iomanip>
#include <fstream>

namespace tpt
{
Expand All @@ -16,23 +21,23 @@ namespace tpt
CRITICAL
};

// ILogger interface with virtual methods for logging messages at different severity levels.
class ILogger
// Logger base class with virtual methods for logging messages at different severity levels.
class Logger
{
public:
virtual ~ILogger() = default; // Virtual destructor for proper cleanup of derived classes

// Pure virtual functions for logging messages at different severity levels
virtual void log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0) = 0;
virtual void debug(const std::string &message) = 0;
virtual void info(const std::string &message) = 0;
virtual void warning(const std::string &message) = 0;
virtual void error(const std::string &message) = 0;
virtual void critical(const std::string &message) = 0;
protected:
std::string logLevelToString(LogLevel level);

// Additional virtual methods can be added here as needed
public:
virtual ~Logger() {}
virtual void log(const std::string &message, LogLevel level, const char *file, int line) = 0;
#define LOG(logger, level, message) (logger).log(message, level, __FILE__, __LINE__)
#define LOG_DEBUG(logger, message) LOG(logger, tpt::LogLevel::DEBUG, message)
#define LOG_INFO(logger, message) LOG(logger, tpt::LogLevel::INFO, message)
#define LOG_WARNING(logger, message) LOG(logger, tpt::LogLevel::WARNING, message)
#define LOG_ERROR(logger, message) LOG(logger, tpt::LogLevel::ERROR, message)
#define LOG_CRITICAL(logger, message) LOG(logger, tpt::LogLevel::CRITICAL, message)
};

} // namespace tpt

#endif // LOGGER_H_
#endif // LOGGER_H_
2 changes: 2 additions & 0 deletions include/teapot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "response.hpp"
#include "context.hpp"
#include "base_exceptions.hpp"
#include "console_logger.hpp"

#ifdef __linux__
#include "unix_socket.hpp"
Expand Down Expand Up @@ -57,6 +58,7 @@ namespace tpt
CORSMiddleware cors_middleware;
SanitizerMiddleware sanitizer_middleware;
SecurityMiddleware security_middleware;
ConsoleLogger logger;
#ifdef __linux__
tpt::UnixSocket socket;
#endif
Expand Down
9 changes: 6 additions & 3 deletions include/unix_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "socket.hpp"
#include "utils.hpp"
#include "base_exceptions.hpp"
#include "console_logger.hpp"

namespace tpt
{
Expand All @@ -28,12 +29,14 @@ namespace tpt
std::string ip_address;
std::string client_ip;
std::vector<std::string> ip_blacklist;
ConsoleLogger logger;

public:
UnixSocket();
UnixSocket(unsigned int port);
UnixSocket(std::string ip_address, unsigned int port);
UnixSocket(std::string ip_address, unsigned int port, unsigned int max_connections);
UnixSocket(ConsoleLogger logger);
UnixSocket(ConsoleLogger logger, unsigned int port);
UnixSocket(ConsoleLogger logger, std::string ip_address, unsigned int port);
UnixSocket(ConsoleLogger logger, std::string ip_address, unsigned int port, unsigned int max_connections);
~UnixSocket();
std::string getClientIp();
virtual void bindSocket() override;
Expand Down
89 changes: 41 additions & 48 deletions src/console_logger.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,47 @@
#include "../include/console_logger.hpp"

using namespace tpt;

void ConsoleLogger::log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0)
namespace tpt
{
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);

// You might want to adjust the time format according to your needs
std::tm now_tm = *std::localtime(&now_time);

std::cout << "[" << std::put_time(&now_tm, "%Y-%m-%d %H:%M:%S") << "] ";
std::cout << "[" << static_cast<int>(level) << "] ";

if (file)
void ConsoleLogger::log(const std::string &message, tpt::LogLevel level, const char *file, int line)
{
std::cout << "[" << file << ":" << line << "] ";
auto now = std::chrono::system_clock::now();
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
std::tm now_tm = *std::localtime(&now_time);

std::string colorCode;
switch (level)
{
case LogLevel::DEBUG:
colorCode = "\033[36m"; // Cyan
break;
case LogLevel::INFO:
colorCode = "\033[0m";
break;
case LogLevel::WARNING:
colorCode = "\033[33m"; // Yellow
break;
case LogLevel::ERROR:
colorCode = "\033[31m"; // Red
break;
case LogLevel::CRITICAL:
colorCode = "\033[35m"; // Magenta
break;
default:
colorCode = "\033[0m"; // Reset
break;
}

// Use logLevelToString to convert the LogLevel to a string
std::string levelStr = logLevelToString(level);

std::cout << colorCode << "[" << std::put_time(&now_tm, "%Y-%m-%d %H:%M:%S") << "] "
<< "[" << levelStr << "]";

if (file)
{
std::cout << " [" << file << ":" << line << "]";
}

std::cout << " " << message << "\033[0m" << std::endl;
}

std::cout << message << std::endl;
}
void ConsoleLogger::debug(const std::string &message)
{
log(message, LogLevel::DEBUG);
}

void ConsoleLogger::info(const std::string &message)
{
log(message, LogLevel::INFO);
}

void ConsoleLogger::warning(const std::string &message)
{
log(message, LogLevel::WARNING);
}

void ConsoleLogger::error(const std::string &message)
{
log(message, LogLevel::ERROR);
}

void ConsoleLogger::critical(const std::string &message)
{
log(message, LogLevel::CRITICAL);
}

// Helper Macros
#define LOG(logger, level, message) (logger).log(message, level, __FILE__, __LINE__)
#define LOG_DEBUG(logger, message) LOG(logger, tpt::LogLevel::DEBUG, message)
#define LOG_INFO(logger, message) LOG(logger, tpt::LogLevel::INFO, message)
#define LOG_WARNING(logger, message) LOG(logger, tpt::LogLevel::WARNING, message)
#define LOG_ERROR(logger, message) LOG(logger, tpt::LogLevel::ERROR, message)
#define LOG_CRITICAL(logger, message) LOG(logger, tpt::LogLevel::CRITICAL, message)
23 changes: 23 additions & 0 deletions src/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "../include/logger.hpp"

namespace tpt
{
std::string Logger::logLevelToString(LogLevel level)
{
switch (level)
{
case LogLevel::DEBUG:
return "DEBUG";
case LogLevel::INFO:
return "INFO";
case LogLevel::WARNING:
return "WARNING";
case LogLevel::ERROR:
return "ERROR";
case LogLevel::CRITICAL:
return "CRITICAL";
default:
return "UNKNOWN";
}
}
}
11 changes: 7 additions & 4 deletions src/teapot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ void Teapot::mainEventLoop(SOCKET client_socket)
content_type = "text/html";
}
}
catch (...)
catch (std::exception &e)
{
body = Utils::readFileToBuffer(this->static_files_dir + "/500.html");
content_type = "text/html";
Expand Down Expand Up @@ -169,8 +169,9 @@ Teapot::Teapot()
this->sanitizer_middleware = SanitizerMiddleware();
this->cors_middleware = CORSMiddleware();
this->security_middleware = SecurityMiddleware();
this->logger = ConsoleLogger();
#ifdef __linux__
this->socket = tpt::UnixSocket(this->port);
this->socket = tpt::UnixSocket(this->logger, this->port);
#endif
#ifdef _WIN32
this->socket = tpt::WinSocket(this->port);
Expand All @@ -184,8 +185,9 @@ Teapot::Teapot(unsigned int port)
this->max_connections = 10;
this->logging_type = DEFAULT;
this->static_files_dir = "static";
this->logger = ConsoleLogger();
#ifdef __linux__
this->socket = tpt::UnixSocket(this->port);
this->socket = tpt::UnixSocket(this->logger, this->port);
#endif
#ifdef _WIN32
this->socket = tpt::WinSocket(this->port);
Expand All @@ -199,8 +201,9 @@ Teapot::Teapot(std::string ip_address, unsigned int port, unsigned int max_conne
this->max_connections = max_connections;
this->logging_type = logging_type;
this->static_files_dir = static_files_dir;
this->logger = ConsoleLogger();
#ifdef __linux__
this->socket = tpt::UnixSocket(ip_address, port, max_connections);
this->socket = tpt::UnixSocket(logger, ip_address, port, max_connections);
#endif
#ifdef _WIN32
this->socket = tpt::WinSocket(ip_address, port, max_connections);
Expand Down
Loading

0 comments on commit acdfc8d

Please sign in to comment.