BetaTrader
A HFT Eco-System
Loading...
Searching...
No Matches
Logger.h
Go to the documentation of this file.
1//
2// Created by sujal on 28-10-2025.
3//
4
5#pragma once
6
7#include "spdlog/async.h"
8#include "spdlog/sinks/rotating_file_sink.h"
9#include "spdlog/sinks/stdout_color_sinks.h"
10#include "spdlog/spdlog.h"
11#include <chrono>
12#include <iomanip>
13#include <sstream>
14#include <string>
15#include <vector> // Include for std::vector
16
17namespace logging {
18 class Logger {
19 private:
25 static std::string
26 GenerateTimestampedFilename(const std::string& baseLogPath)
27 {
28 auto now = std::chrono::system_clock::now();
29 auto time = std::chrono::system_clock::to_time_t(now);
30
31 std::stringstream ss;
32 ss << std::put_time(std::localtime(&time), "%Y-%m-%d_%H-%M-%S");
33
34 // Split the path into directory, name, and extension
35 size_t lastSlash = baseLogPath.find_last_of("/\\");
36 size_t lastDot = baseLogPath.find_last_of('.');
37
38 std::string directory
39 = (lastSlash != std::string::npos)
40 ? baseLogPath.substr(0, lastSlash + 1)
41 : "";
42 std::string filename = (lastSlash != std::string::npos)
43 ? baseLogPath.substr(lastSlash + 1)
44 : baseLogPath;
45 std::string extension
46 = (lastDot != std::string::npos && lastDot > lastSlash)
47 ? baseLogPath.substr(lastDot)
48 : "";
49 std::string basename
50 = (lastDot != std::string::npos && lastDot > lastSlash)
51 ? filename.substr(0, lastDot - (lastSlash + 1))
52 : filename;
53
54 return directory + basename + "_" + ss.str() + extension;
55 }
56
57 public:
74 static void
75 Init(const std::string& loggerName = "async_logger",
76 const std::string& logFilePath = "logs/app.log",
77 const bool enableConsole = true, const bool enableFile = true,
78 const spdlog::level::level_enum globalLevel = spdlog::level::trace,
79 const size_t queueSize = 8192, const size_t numThreads = 1,
80 size_t maxFileSize = 1024 * 1024 * 300, size_t maxFiles = 5,
81 spdlog::sink_ptr customSink = nullptr) // Added customSink parameter
82 {
83 spdlog::drop_all();
84 spdlog::shutdown();
85 spdlog::init_thread_pool(queueSize, numThreads);
86 std::vector<spdlog::sink_ptr> sinks;
87
88 if (enableConsole) {
89 auto consoleSink = std::make_shared<
90 spdlog::sinks::stdout_color_sink_mt>();
91 consoleSink->set_level(globalLevel);
92 sinks.push_back(consoleSink);
93 }
94
95 if (enableFile) {
96 std::string timestampedPath
97 = GenerateTimestampedFilename(logFilePath);
98 auto fileSink = std::make_shared<
99 spdlog::sinks::rotating_file_sink_mt>(
100 timestampedPath, maxFileSize, maxFiles);
101 fileSink->set_level(globalLevel);
102 sinks.push_back(fileSink);
103 }
104
105 if (customSink) {
106 sinks.push_back(customSink);
107 }
108
109 if (sinks.empty()) {
110 throw std::runtime_error(
111 "Logger requires at least one sink (file or console).");
112 }
113
114 auto asyncLogger = std::make_shared<spdlog::async_logger>(
115 loggerName, sinks.begin(), sinks.end(),
116 spdlog::thread_pool(),
117 spdlog::async_overflow_policy::block);
118
119 asyncLogger->set_level(globalLevel);
120 asyncLogger->set_pattern(
121 "[%Y-%m-%d %H:%M:%S.%e] [t %t] [%^%l%$] [%s:%#] %v");
122
123 spdlog::register_logger(asyncLogger);
124 spdlog::set_default_logger(asyncLogger);
125 spdlog::set_level(globalLevel);
126 spdlog::set_error_handler([](const std::string& msg) {
127 fprintf(stderr, "SPDLOG INTERNAL ERROR: %s\n", msg.c_str());
128 });
129 }
130
134 static void Shutdown() { spdlog::shutdown(); }
135 };
136} // namespace logging
137
138#define LOG_TRACE(...) SPDLOG_TRACE(__VA_ARGS__)
139#define LOG_DEBUG(...) SPDLOG_DEBUG(__VA_ARGS__)
140#define LOG_INFO(...) SPDLOG_INFO(__VA_ARGS__)
141#define LOG_WARN(...) SPDLOG_WARN(__VA_ARGS__)
142#define LOG_ERROR(...) SPDLOG_ERROR(__VA_ARGS__)
143#define LOG_CRITICAL(...) SPDLOG_CRITICAL(__VA_ARGS__)
Definition Logger.h:18
static void Init(const std::string &loggerName="async_logger", const std::string &logFilePath="logs/app.log", const bool enableConsole=true, const bool enableFile=true, const spdlog::level::level_enum globalLevel=spdlog::level::trace, const size_t queueSize=8192, const size_t numThreads=1, size_t maxFileSize=1024 *1024 *300, size_t maxFiles=5, spdlog::sink_ptr customSink=nullptr)
Initializes the spdlog and sets up the sources and sinks NOTE: Use Shutdown to clean up and dump all ...
Definition Logger.h:75
static void Shutdown()
Definition Logger.h:134
static std::string GenerateTimestampedFilename(const std::string &baseLogPath)
Generates a timestamped filename.
Definition Logger.h:26
Definition Logger.h:17