Skip to content

Commit acdfc8d

Browse files
committed
fix: console logger
1 parent 7ea470e commit acdfc8d

8 files changed

+140
-93
lines changed

include/console_logger.hpp

+2-12
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,13 @@
33
#define CONSOLE_LOGGER_H_
44

55
#include "logger.hpp"
6-
#include <iostream>
7-
#include <string>
8-
#include <chrono>
9-
#include <ctime>
10-
#include <iomanip>
116

127
namespace tpt
138
{
14-
class ConsoleLogger : public ILogger
9+
class ConsoleLogger : public Logger
1510
{
1611
public:
17-
virtual void log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0) override;
18-
virtual void debug(const std::string &message) override;
19-
virtual void info(const std::string &message) override;
20-
virtual void warning(const std::string &message) override;
21-
virtual void error(const std::string &message) override;
22-
virtual void critical(const std::string &message) override;
12+
virtual void log(const std::string &message, tpt::LogLevel level, const char *file, int line) override;
2313
};
2414
}
2515

include/logger.hpp

+19-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#ifndef LOGGER_H_
22
#define LOGGER_H_
33

4+
#include <iostream>
45
#include <string>
6+
#include <chrono>
7+
#include <ctime>
8+
#include <iomanip>
9+
#include <fstream>
510

611
namespace tpt
712
{
@@ -16,23 +21,23 @@ namespace tpt
1621
CRITICAL
1722
};
1823

19-
// ILogger interface with virtual methods for logging messages at different severity levels.
20-
class ILogger
24+
// Logger base class with virtual methods for logging messages at different severity levels.
25+
class Logger
2126
{
22-
public:
23-
virtual ~ILogger() = default; // Virtual destructor for proper cleanup of derived classes
24-
25-
// Pure virtual functions for logging messages at different severity levels
26-
virtual void log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0) = 0;
27-
virtual void debug(const std::string &message) = 0;
28-
virtual void info(const std::string &message) = 0;
29-
virtual void warning(const std::string &message) = 0;
30-
virtual void error(const std::string &message) = 0;
31-
virtual void critical(const std::string &message) = 0;
27+
protected:
28+
std::string logLevelToString(LogLevel level);
3229

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

3641
} // namespace tpt
3742

38-
#endif // LOGGER_H_
43+
#endif // LOGGER_H_

include/teapot.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "response.hpp"
2424
#include "context.hpp"
2525
#include "base_exceptions.hpp"
26+
#include "console_logger.hpp"
2627

2728
#ifdef __linux__
2829
#include "unix_socket.hpp"
@@ -57,6 +58,7 @@ namespace tpt
5758
CORSMiddleware cors_middleware;
5859
SanitizerMiddleware sanitizer_middleware;
5960
SecurityMiddleware security_middleware;
61+
ConsoleLogger logger;
6062
#ifdef __linux__
6163
tpt::UnixSocket socket;
6264
#endif

include/unix_socket.hpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "socket.hpp"
1616
#include "utils.hpp"
1717
#include "base_exceptions.hpp"
18+
#include "console_logger.hpp"
1819

1920
namespace tpt
2021
{
@@ -28,12 +29,14 @@ namespace tpt
2829
std::string ip_address;
2930
std::string client_ip;
3031
std::vector<std::string> ip_blacklist;
32+
ConsoleLogger logger;
3133

3234
public:
3335
UnixSocket();
34-
UnixSocket(unsigned int port);
35-
UnixSocket(std::string ip_address, unsigned int port);
36-
UnixSocket(std::string ip_address, unsigned int port, unsigned int max_connections);
36+
UnixSocket(ConsoleLogger logger);
37+
UnixSocket(ConsoleLogger logger, unsigned int port);
38+
UnixSocket(ConsoleLogger logger, std::string ip_address, unsigned int port);
39+
UnixSocket(ConsoleLogger logger, std::string ip_address, unsigned int port, unsigned int max_connections);
3740
~UnixSocket();
3841
std::string getClientIp();
3942
virtual void bindSocket() override;

src/console_logger.cpp

+41-48
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,47 @@
11
#include "../include/console_logger.hpp"
22

3-
using namespace tpt;
4-
5-
void ConsoleLogger::log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0)
3+
namespace tpt
64
{
7-
auto now = std::chrono::system_clock::now();
8-
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
9-
10-
// You might want to adjust the time format according to your needs
11-
std::tm now_tm = *std::localtime(&now_time);
12-
13-
std::cout << "[" << std::put_time(&now_tm, "%Y-%m-%d %H:%M:%S") << "] ";
14-
std::cout << "[" << static_cast<int>(level) << "] ";
15-
16-
if (file)
5+
void ConsoleLogger::log(const std::string &message, tpt::LogLevel level, const char *file, int line)
176
{
18-
std::cout << "[" << file << ":" << line << "] ";
7+
auto now = std::chrono::system_clock::now();
8+
std::time_t now_time = std::chrono::system_clock::to_time_t(now);
9+
std::tm now_tm = *std::localtime(&now_time);
10+
11+
std::string colorCode;
12+
switch (level)
13+
{
14+
case LogLevel::DEBUG:
15+
colorCode = "\033[36m"; // Cyan
16+
break;
17+
case LogLevel::INFO:
18+
colorCode = "\033[0m";
19+
break;
20+
case LogLevel::WARNING:
21+
colorCode = "\033[33m"; // Yellow
22+
break;
23+
case LogLevel::ERROR:
24+
colorCode = "\033[31m"; // Red
25+
break;
26+
case LogLevel::CRITICAL:
27+
colorCode = "\033[35m"; // Magenta
28+
break;
29+
default:
30+
colorCode = "\033[0m"; // Reset
31+
break;
32+
}
33+
34+
// Use logLevelToString to convert the LogLevel to a string
35+
std::string levelStr = logLevelToString(level);
36+
37+
std::cout << colorCode << "[" << std::put_time(&now_tm, "%Y-%m-%d %H:%M:%S") << "] "
38+
<< "[" << levelStr << "]";
39+
40+
if (file)
41+
{
42+
std::cout << " [" << file << ":" << line << "]";
43+
}
44+
45+
std::cout << " " << message << "\033[0m" << std::endl;
1946
}
20-
21-
std::cout << message << std::endl;
22-
}
23-
void ConsoleLogger::debug(const std::string &message)
24-
{
25-
log(message, LogLevel::DEBUG);
26-
}
27-
28-
void ConsoleLogger::info(const std::string &message)
29-
{
30-
log(message, LogLevel::INFO);
3147
}
32-
33-
void ConsoleLogger::warning(const std::string &message)
34-
{
35-
log(message, LogLevel::WARNING);
36-
}
37-
38-
void ConsoleLogger::error(const std::string &message)
39-
{
40-
log(message, LogLevel::ERROR);
41-
}
42-
43-
void ConsoleLogger::critical(const std::string &message)
44-
{
45-
log(message, LogLevel::CRITICAL);
46-
}
47-
48-
// Helper Macros
49-
#define LOG(logger, level, message) (logger).log(message, level, __FILE__, __LINE__)
50-
#define LOG_DEBUG(logger, message) LOG(logger, tpt::LogLevel::DEBUG, message)
51-
#define LOG_INFO(logger, message) LOG(logger, tpt::LogLevel::INFO, message)
52-
#define LOG_WARNING(logger, message) LOG(logger, tpt::LogLevel::WARNING, message)
53-
#define LOG_ERROR(logger, message) LOG(logger, tpt::LogLevel::ERROR, message)
54-
#define LOG_CRITICAL(logger, message) LOG(logger, tpt::LogLevel::CRITICAL, message)

src/logger.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "../include/logger.hpp"
2+
3+
namespace tpt
4+
{
5+
std::string Logger::logLevelToString(LogLevel level)
6+
{
7+
switch (level)
8+
{
9+
case LogLevel::DEBUG:
10+
return "DEBUG";
11+
case LogLevel::INFO:
12+
return "INFO";
13+
case LogLevel::WARNING:
14+
return "WARNING";
15+
case LogLevel::ERROR:
16+
return "ERROR";
17+
case LogLevel::CRITICAL:
18+
return "CRITICAL";
19+
default:
20+
return "UNKNOWN";
21+
}
22+
}
23+
}

src/teapot.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void Teapot::mainEventLoop(SOCKET client_socket)
141141
content_type = "text/html";
142142
}
143143
}
144-
catch (...)
144+
catch (std::exception &e)
145145
{
146146
body = Utils::readFileToBuffer(this->static_files_dir + "/500.html");
147147
content_type = "text/html";
@@ -169,8 +169,9 @@ Teapot::Teapot()
169169
this->sanitizer_middleware = SanitizerMiddleware();
170170
this->cors_middleware = CORSMiddleware();
171171
this->security_middleware = SecurityMiddleware();
172+
this->logger = ConsoleLogger();
172173
#ifdef __linux__
173-
this->socket = tpt::UnixSocket(this->port);
174+
this->socket = tpt::UnixSocket(this->logger, this->port);
174175
#endif
175176
#ifdef _WIN32
176177
this->socket = tpt::WinSocket(this->port);
@@ -184,8 +185,9 @@ Teapot::Teapot(unsigned int port)
184185
this->max_connections = 10;
185186
this->logging_type = DEFAULT;
186187
this->static_files_dir = "static";
188+
this->logger = ConsoleLogger();
187189
#ifdef __linux__
188-
this->socket = tpt::UnixSocket(this->port);
190+
this->socket = tpt::UnixSocket(this->logger, this->port);
189191
#endif
190192
#ifdef _WIN32
191193
this->socket = tpt::WinSocket(this->port);
@@ -199,8 +201,9 @@ Teapot::Teapot(std::string ip_address, unsigned int port, unsigned int max_conne
199201
this->max_connections = max_connections;
200202
this->logging_type = logging_type;
201203
this->static_files_dir = static_files_dir;
204+
this->logger = ConsoleLogger();
202205
#ifdef __linux__
203-
this->socket = tpt::UnixSocket(ip_address, port, max_connections);
206+
this->socket = tpt::UnixSocket(logger, ip_address, port, max_connections);
204207
#endif
205208
#ifdef _WIN32
206209
this->socket = tpt::WinSocket(ip_address, port, max_connections);

0 commit comments

Comments
 (0)