utility 2026.1.9
A comprehensive C++ utilities library tailored for the development of modern desktop and extended reality (XR) applications.
Loading...
Searching...
No Matches
standard_logger.cpp
1/*
2 Copyright (c) 2026 ETIB Corporation
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23#include <cstdlib>
24#include <iostream>
25
27
28namespace utility::logging
29{
30
31 StandardLogger::StandardLogger(const std::string &name)
32 : Logger(name)
33 {
34 }
35
37 {
38 std::cout.flush();
39 std::cerr.flush();
40 }
41
42 static bool useColor()
43 {
44 const char *noColor = std::getenv("NO_COLOR");
45 return noColor == nullptr || noColor[0] == '\0';
46 }
47
48 static const char *levelColor(LogLevel level)
49 {
50 switch (level) {
51 case LogLevel::DEBUG_LEVEL:
52 return "\033[36m";
53 case LogLevel::INFO_LEVEL:
54 return "\033[32m";
55 case LogLevel::WARNING_LEVEL:
56 return "\033[33m";
57 case LogLevel::ERROR_LEVEL:
58 return "\033[31m";
59 default:
60 return "\033[0m";
61 }
62 }
63
64 static const char *resetColor()
65 {
66 return "\033[0m";
67 }
68
70 {
71 // Write straight to the destination stream instead of composing the
72 // line in an intermediate `std::stringstream` and copying it with
73 // `str()`. The emitted bytes are identical.
74 const bool isError = record.level == LogLevel::WARNING_LEVEL
75 || record.level == LogLevel::ERROR_LEVEL;
76 std::ostream &stream = isError ? std::cerr : std::cout;
77
78 stream << "[" << record.timestamp << "] ";
79 stream << "[" << record.loggerName << "] ";
80
81 bool color = useColor();
82 if (color) {
83 stream << levelColor(record.level);
84 }
85 stream << "[" << levelToString(record.level) << "] ";
86 if (color) {
87 stream << resetColor();
88 }
89
90 if (record.level == LogLevel::DEBUG_LEVEL) {
91 stream << "[" << record.file << ":" << record.line << " "
92 << record.function << "] ";
93 }
94
95 stream << record.message << '\n';
96
97 const FlushPolicy policy = getFlushPolicy();
98 if (policy == FlushPolicy::ALWAYS
99 || (policy == FlushPolicy::BUFFERED && isError)) {
100 stream.flush();
101 }
102 }
103
104} // namespace utility::logging
Abstract logger interface defining stream-style logging operations.
Definition logger.hpp:96
FlushPolicy getFlushPolicy(void) const noexcept
Get the current output flush policy.
Definition logger.cpp:144
static std::string levelToString(LogLevel level)
Get string representation of log level.
Definition logger.cpp:71
StandardLogger(const std::string &name)
Constructor with logger name.
~StandardLogger(void) override
Destructor ensuring output streams are flushed.
void output(const LogRecord &record) override
Output a formatted log record to the appropriate stream.
FlushPolicy
Controls when a stream-backed logger flushes its output.
Definition logger.hpp:68
Standard output/error logger declaration.
Structured log record carrying metadata for a single log entry.
Definition logger.hpp:77
LogLevel level
Severity level.
Definition logger.hpp:78
int line
Source line number.
Definition logger.hpp:84
std::string file
Source file path.
Definition logger.hpp:83
std::string loggerName
Name of the logger that emitted the record.
Definition logger.hpp:82
std::string timestamp
Formatted timestamp string.
Definition logger.hpp:80
std::string function
Function name.
Definition logger.hpp:85
std::string message
Log message content.
Definition logger.hpp:79