Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::utils::string_utils Class Reference

String utility functions for log formatting and conversion. More...

#include <string_utils.h>

Collaboration diagram for kcenon::logger::utils::string_utils:
Collaboration graph

Static Public Member Functions

static std::string level_to_string (log_level level)
 Convert log level to human-readable string.
 
static std::string level_to_color (log_level level, bool use_colors=true)
 Convert log level to ANSI color code.
 
static const char * color_reset ()
 ANSI color reset sequence.
 
static std::string escape_json (const std::string &str)
 Escape special characters for JSON.
 
static std::string escape_xml (const std::string &str)
 Escape special characters for XML.
 
static std::string extract_filename (const std::string &file_path)
 Extract filename from full file path.
 
static std::string trim (const std::string &str)
 Trim whitespace from both ends of string.
 
static std::string to_lower (const std::string &str)
 Convert string to lowercase.
 
static std::string to_upper (const std::string &str)
 Convert string to uppercase.
 
static std::string replace_all (const std::string &str, const std::string &from, const std::string &to)
 Replace all occurrences of a substring.
 

Detailed Description

String utility functions for log formatting and conversion.

Provides common string manipulation functions used across formatters and writers, including escaping, conversion, and extraction utilities.

Definition at line 31 of file string_utils.h.

Member Function Documentation

◆ color_reset()

static const char * kcenon::logger::utils::string_utils::color_reset ( )
inlinestatic

ANSI color reset sequence.

Returns
ANSI escape sequence to reset colors
Note
Use after colored output to reset terminal colors.
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/formatters/template_formatter.h.

Definition at line 95 of file string_utils.h.

95 {
96 return "\033[0m";
97 }

Referenced by kcenon::logger::timestamp_formatter::format(), and kcenon::logger::template_formatter::resolve_placeholder().

Here is the caller graph for this function:

◆ escape_json()

static std::string kcenon::logger::utils::string_utils::escape_json ( const std::string & str)
inlinestatic

Escape special characters for JSON.

Parameters
strString to escape
Returns
JSON-escaped string safe for inclusion in JSON documents

Escaped characters:

  • " → \"
  • \ → \
  • / → \/
  • , \f,
    , \r, \t → respective escape sequences
  • Control characters (0x00-0x1F) → \uXXXX
Note
Thread-safe. Creates a new string with escaped content.
Compatible with JSON parsers and log aggregation systems.

Definition at line 114 of file string_utils.h.

114 {
115 std::ostringstream oss;
116 for (char c : str) {
117 switch (c) {
118 case '"': oss << "\\\""; break;
119 case '\\': oss << "\\\\"; break;
120 case '/': oss << "\\/"; break;
121 case '\b': oss << "\\b"; break;
122 case '\f': oss << "\\f"; break;
123 case '\n': oss << "\\n"; break;
124 case '\r': oss << "\\r"; break;
125 case '\t': oss << "\\t"; break;
126 default:
127 // Handle control characters
128 if (c >= 0 && c < 0x20) {
129 oss << "\\u"
130 << std::hex << std::setw(4) << std::setfill('0')
131 << static_cast<int>(c);
132 } else {
133 oss << c;
134 }
135 break;
136 }
137 }
138 return oss.str();
139 }

Referenced by kcenon::logger::json_formatter::format(), and kcenon::logger::json_formatter::format_value().

Here is the caller graph for this function:

◆ escape_xml()

static std::string kcenon::logger::utils::string_utils::escape_xml ( const std::string & str)
inlinestatic

Escape special characters for XML.

Parameters
strString to escape
Returns
XML-escaped string safe for inclusion in XML documents

Escaped characters:

  • & → &
  • < → <
  • > → >
  • " → "
  • ' → '
Note
Thread-safe. Creates a new string with escaped content.

Definition at line 155 of file string_utils.h.

155 {
156 std::ostringstream oss;
157 for (char c : str) {
158 switch (c) {
159 case '&': oss << "&amp;"; break;
160 case '<': oss << "&lt;"; break;
161 case '>': oss << "&gt;"; break;
162 case '"': oss << "&quot;"; break;
163 case '\'': oss << "&apos;"; break;
164 default: oss << c; break;
165 }
166 }
167 return oss.str();
168 }

◆ extract_filename()

static std::string kcenon::logger::utils::string_utils::extract_filename ( const std::string & file_path)
inlinestatic

Extract filename from full file path.

Parameters
file_pathFull path to file (may include directories)
Returns
Just the filename without directory components

Examples:

  • "/path/to/file.cpp" → "file.cpp"
  • "C:\\path\\to\\file.cpp" → "file.cpp"
  • "file.cpp" → "file.cpp"
Note
Works with both Unix (/) and Windows () path separators.
Thread-safe.
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/formatters/template_formatter.h.

Definition at line 183 of file string_utils.h.

183 {
184 if (file_path.empty()) {
185 return file_path;
186 }
187
188 size_t pos = file_path.find_last_of("/\\");
189 if (pos != std::string::npos) {
190 return file_path.substr(pos + 1);
191 }
192
193 return file_path;
194 }

Referenced by kcenon::logger::timestamp_formatter::format(), and kcenon::logger::template_formatter::resolve_placeholder().

Here is the caller graph for this function:

◆ level_to_color()

static std::string kcenon::logger::utils::string_utils::level_to_color ( log_level level,
bool use_colors = true )
inlinestatic

Convert log level to ANSI color code.

Parameters
levelLog level to convert
use_colorsWhether to return colors (if false, returns empty string)
Returns
ANSI escape sequence for the log level color

Color mapping:

  • CRITICAL/FATAL: Bright Magenta
  • ERROR: Bright Red
  • WARNING: Bright Yellow
  • INFO: Bright Green
  • DEBUG: Bright Cyan
  • TRACE: White
Note
Use with "\033[0m" to reset color after output.
Thread-safe and stateless.
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/formatters/template_formatter.h.

Definition at line 72 of file string_utils.h.

72 {
73 if (!use_colors) {
74 return "";
75 }
76
77 switch (level) {
78 case log_level::critical: return "\033[1;35m"; // Bright Magenta
79 case log_level::error: return "\033[1;31m"; // Bright Red
80 case log_level::warning: return "\033[1;33m"; // Bright Yellow
81 case log_level::info: return "\033[1;32m"; // Bright Green
82 case log_level::debug: return "\033[1;36m"; // Bright Cyan
83 case log_level::trace: return "\033[37m"; // White
84 case log_level::off: return "\033[90m"; // Dark Gray
85 }
86 return "";
87 }

Referenced by kcenon::logger::timestamp_formatter::format(), and kcenon::logger::template_formatter::resolve_placeholder().

Here is the caller graph for this function:

◆ level_to_string()

static std::string kcenon::logger::utils::string_utils::level_to_string ( log_level level)
inlinestatic

Convert log level to human-readable string.

Parameters
levelLog level to convert
Returns
String representation of log level

Output format: "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "TRACE", "OFF"

Note
Thread-safe and stateless.
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/formatters/template_formatter.h.

Definition at line 42 of file string_utils.h.

42 {
43 switch (level) {
44 case log_level::critical: return "CRITICAL";
45 case log_level::error: return "ERROR";
46 case log_level::warning: return "WARNING";
47 case log_level::info: return "INFO";
48 case log_level::debug: return "DEBUG";
49 case log_level::trace: return "TRACE";
50 case log_level::off: return "OFF";
51 }
52 return "UNKNOWN";
53 }

Referenced by kcenon::logger::json_formatter::format(), kcenon::logger::timestamp_formatter::format(), kcenon::logger::network_writer::format_for_network(), and kcenon::logger::template_formatter::resolve_placeholder().

Here is the caller graph for this function:

◆ replace_all()

static std::string kcenon::logger::utils::string_utils::replace_all ( const std::string & str,
const std::string & from,
const std::string & to )
inlinestatic

Replace all occurrences of a substring.

Parameters
strSource string
fromSubstring to replace
toReplacement substring
Returns
String with all replacements made
Note
If 'from' is empty, returns original string.
Thread-safe. Creates a new string.

Definition at line 262 of file string_utils.h.

266 {
267 if (from.empty()) {
268 return str;
269 }
270
271 std::string result = str;
272 size_t pos = 0;
273
274 while ((pos = result.find(from, pos)) != std::string::npos) {
275 result.replace(pos, from.length(), to);
276 pos += to.length();
277 }
278
279 return result;
280 }

◆ to_lower()

static std::string kcenon::logger::utils::string_utils::to_lower ( const std::string & str)
inlinestatic

Convert string to lowercase.

Parameters
strString to convert
Returns
Lowercase version of string
Note
Only converts ASCII characters (A-Z → a-z).
Thread-safe. Creates a new string.
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/formatters/template_formatter.h.

Definition at line 224 of file string_utils.h.

224 {
225 std::string result = str;
226 for (char& c : result) {
227 if (c >= 'A' && c <= 'Z') {
228 c = c + ('a' - 'A');
229 }
230 }
231 return result;
232 }

Referenced by kcenon::logger::template_formatter::resolve_placeholder().

Here is the caller graph for this function:

◆ to_upper()

static std::string kcenon::logger::utils::string_utils::to_upper ( const std::string & str)
inlinestatic

Convert string to uppercase.

Parameters
strString to convert
Returns
Uppercase version of string
Note
Only converts ASCII characters (a-z → A-Z).
Thread-safe. Creates a new string.

Definition at line 242 of file string_utils.h.

242 {
243 std::string result = str;
244 for (char& c : result) {
245 if (c >= 'a' && c <= 'z') {
246 c = c - ('a' - 'A');
247 }
248 }
249 return result;
250 }

◆ trim()

static std::string kcenon::logger::utils::string_utils::trim ( const std::string & str)
inlinestatic

Trim whitespace from both ends of string.

Parameters
strString to trim
Returns
Trimmed string
Note
Removes spaces, tabs, newlines, and other whitespace characters.
Thread-safe. Creates a new string.

Definition at line 204 of file string_utils.h.

204 {
205 const char* whitespace = " \t\n\r\f\v";
206
207 size_t start = str.find_first_not_of(whitespace);
208 if (start == std::string::npos) {
209 return ""; // String is all whitespace
210 }
211
212 size_t end = str.find_last_not_of(whitespace);
213 return str.substr(start, end - start + 1);
214 }

The documentation for this class was generated from the following file: