Skip to content

Commit 7ea470e

Browse files
committed
feat: console logger
1 parent dfc8495 commit 7ea470e

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

include/console_logger.hpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
#ifndef CONSOLE_LOGGER_H_
3+
#define CONSOLE_LOGGER_H_
4+
5+
#include "logger.hpp"
6+
#include <iostream>
7+
#include <string>
8+
#include <chrono>
9+
#include <ctime>
10+
#include <iomanip>
11+
12+
namespace tpt
13+
{
14+
class ConsoleLogger : public ILogger
15+
{
16+
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;
23+
};
24+
}
25+
26+
#endif

include/logger.hpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef LOGGER_H_
2+
#define LOGGER_H_
3+
4+
#include <string>
5+
6+
namespace tpt
7+
{
8+
9+
// Define the LogLevel enum to specify the severity of the log messages.
10+
enum class LogLevel
11+
{
12+
DEBUG,
13+
INFO,
14+
WARNING,
15+
ERROR,
16+
CRITICAL
17+
};
18+
19+
// ILogger interface with virtual methods for logging messages at different severity levels.
20+
class ILogger
21+
{
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;
32+
33+
// Additional virtual methods can be added here as needed
34+
};
35+
36+
} // namespace tpt
37+
38+
#endif // LOGGER_H_

src/console_logger.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "../include/console_logger.hpp"
2+
3+
using namespace tpt;
4+
5+
void ConsoleLogger::log(const std::string &message, LogLevel level, const char *file = nullptr, int line = 0)
6+
{
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)
17+
{
18+
std::cout << "[" << file << ":" << line << "] ";
19+
}
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);
31+
}
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)

0 commit comments

Comments
 (0)