NoTeX 1.0.0
A modern noteworthy LaTeX template
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1
16
17#pragma once
18
19// plog only records __FILE__ into each Record when PLOG_CAPTURE_FILE is
20// defined before <plog/Log.h> is first included in a translation unit;
21// otherwise Record::getFile() always returns "" and CustomFormatter's file
22// field silently goes blank. Must stay defined ahead of the plog includes
23// below, and every translation unit that uses PLOG_* must reach plog only
24// through this header so the definition is always seen first.
25#define PLOG_CAPTURE_FILE
26
27#include <plog/Appenders/RollingFileAppender.h>
28#include <plog/Init.h>
29#include <plog/Log.h>
30#include <plog/Record.h>
31#include <plog/Severity.h>
32#include <plog/Util.h>
33
34#include <ctime>
35#include <filesystem>
36#include <iomanip>
37#include <iostream>
38
39#include "constants.hpp"
40
41namespace fs = std::filesystem;
42
52public:
58 static plog::util::nstring header() { return plog::util::nstring(); }
59
67 static plog::util::nstring format(const plog::Record& record) {
68 std::tm t{};
69 plog::util::localtime_s(&t, &record.getTime().time);
70
71 plog::util::nostringstream ss;
72 ss << "[" << (t.tm_year + 1900) << "-" << std::setfill('0')
73 << std::setw(2) << (t.tm_mon + 1) << "-" << std::setfill('0')
74 << std::setw(2) << t.tm_mday << " " << std::setfill('0')
75 << std::setw(2) << t.tm_hour << ":" << std::setfill('0')
76 << std::setw(2) << t.tm_min << ":" << std::setfill('0')
77 << std::setw(2) << t.tm_sec << "." << std::setfill('0')
78 << std::setw(3) << static_cast<int>(record.getTime().millitm) << "]";
79
80 ss << " [" << plog::severityToString(record.getSeverity()) << "]";
81
82 ss << " [" << fs::path(record.getFile()).filename().string() << ":"
83 << record.getFunc() << ":" << record.getLine() << "]";
84
85 ss << " " << record.getMessage() << "\n";
86
87 return ss.str();
88 }
89};
90
118inline void init_logging(const std::string& app_name,
119 plog::Severity severity = plog::debug) {
120 try {
121 // Create logs directory if it doesn't exist
122 fs::path logs_dir = fs::current_path() / "logs";
123 if (!fs::exists(logs_dir)) {
124 if (!fs::create_directories(logs_dir)) {
125 throw std::runtime_error("Failed to create logs directory: " +
126 logs_dir.string());
127 }
128 }
129
130 // Construct log file path
131 fs::path log_file = logs_dir / (app_name + ".log");
132
133 // Initialize plog with rolling file appender and CustomFormatter
134 // Parameters: severity, appender, formatter, max_size, max_files
135 static plog::RollingFileAppender<CustomFormatter> file_appender(
136 log_file.string().c_str(), 5000000, 3); // 5MB per file, 3 files
137
138 plog::init(severity, &file_appender);
139
140#ifdef DEBUG
141 std::cout << ansi::text::dim << "Logging initialized to: " << log_file
142 << ansi::reset << std::endl;
143#endif
144 } catch (const std::exception& e) {
145 std::cerr << ansi::color::red
146 << "Logging initialization error: " << e.what() << ansi::reset
147 << std::endl;
148 throw;
149 }
150}
Single-line plog formatter: "[time] [LEVEL] [file:func:line] msg".
Definition logging.hpp:51
static plog::util::nstring header()
Header line prepended once when a log file is created.
Definition logging.hpp:58
static plog::util::nstring format(const plog::Record &record)
Format a single log record as one newline-terminated line.
Definition logging.hpp:67
Compile-time constants.
void init_logging(const std::string &app_name, plog::Severity severity=plog::debug)
Initialize logging to file in logs/ directory.
Definition logging.hpp:118
constexpr std::string_view red
Text foreground color: red.
Definition constants.hpp:69
constexpr std::string_view dim
Reduce text intensity (dim/faint).
Definition constants.hpp:42
constexpr std::string_view reset
Reset all text formatting and colors to terminal default.
Definition constants.hpp:30