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

Classes

struct  error_context
 Structured error context for debugging. More...
 
class  file_utils
 File utility functions for path validation and sanitization. More...
 
class  string_utils
 String utility functions for log formatting and conversion. More...
 
class  time_utils
 Time utility functions for timestamp formatting. More...
 

Functions

void log_error_context (const error_context &context)
 Log error to stderr with context.
 
template<typename F >
common::VoidResult try_write_operation (F &&operation, logger_error_code default_error_code=logger_error_code::file_write_failed)
 Error handling helper for write operations.
 
template<typename F >
common::VoidResult try_open_operation (F &&operation)
 Error handling helper for file open operations.
 
template<typename F >
common::VoidResult try_network_operation (F &&operation)
 Error handling helper for network operations.
 
template<typename F >
common::VoidResult try_encryption_operation (F &&operation)
 Error handling helper for encryption operations.
 
common::VoidResult check_condition (bool condition, logger_error_code error_code, const std::string &message)
 Condition verification helper.
 
template<typename Stream >
common::VoidResult check_stream_state (const Stream &stream, const std::string &operation_name="operation")
 Stream state verification helper.
 
common::VoidResult check_file_exists (const std::filesystem::path &path)
 File existence verification helper.
 
common::VoidResult ensure_directory_exists (const std::filesystem::path &dir)
 Directory creation helper.
 
template<typename F >
void safe_destructor_operation (const std::string &operation_name, F &&operation) noexcept
 Safe operation execution for destructors.
 
template<typename F >
void safe_destructor_result_operation (const std::string &operation_name, F &&operation) noexcept
 Safe operation with result for destructors.
 

Function Documentation

◆ check_condition()

common::VoidResult kcenon::logger::utils::check_condition ( bool condition,
logger_error_code error_code,
const std::string & message )
inline

Condition verification helper.

Checks a boolean condition and returns an error if the condition is false. This helper reduces boilerplate code for precondition checks.

Usage example:

auto check = check_condition(
file_stream_.is_open(),
"File stream is not open"
);
if (check.is_err()) return check;
common::VoidResult check_condition(bool condition, logger_error_code error_code, const std::string &message)
Condition verification helper.
Parameters
conditionThe condition to check
error_codeError code to return if condition is false
messageError message
Returns
common::VoidResult Success if condition is true, error otherwise

Definition at line 282 of file error_handling_utils.h.

286 {
287 if (!condition) {
288 return make_logger_void_result(error_code, message);
289 }
290 return common::ok();
291}
VoidResult ok()
logger_error_code
Error codes specific to the logger system.
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")

References kcenon::logger::make_logger_void_result(), and kcenon::common::ok().

Referenced by kcenon::logger::file_writer::open_internal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_file_exists()

common::VoidResult kcenon::logger::utils::check_file_exists ( const std::filesystem::path & path)
inline

File existence verification helper.

Checks if a file exists at the given path.

Parameters
pathThe file path to check
Returns
common::VoidResult Success if file exists, error otherwise

Definition at line 344 of file error_handling_utils.h.

344 {
345 try {
346 if (!std::filesystem::exists(path)) {
348 logger_error_code::file_open_failed,
349 "File does not exist: " + path.string()
350 );
351 }
352 return common::ok();
353 } catch (const std::filesystem::filesystem_error& e) {
354 return make_logger_void_result(
355 logger_error_code::file_permission_denied,
356 std::string("Cannot access file: ") + e.what()
357 );
358 }
359}

References kcenon::logger::file_open_failed, kcenon::logger::file_permission_denied, kcenon::logger::make_logger_void_result(), and kcenon::common::ok().

Here is the call graph for this function:

◆ check_stream_state()

template<typename Stream >
common::VoidResult kcenon::logger::utils::check_stream_state ( const Stream & stream,
const std::string & operation_name = "operation" )
inline

Stream state verification helper.

Checks if a stream is in a good state and returns an appropriate error if not. This is a specialized version of check_condition for stream operations.

Template Parameters
StreamStream type (e.g., std::ofstream, std::ifstream)
Parameters
streamThe stream to check
operation_nameName of the operation for error message (e.g., "write", "open")
Returns
common::VoidResult Success if stream is good, error otherwise

Definition at line 305 of file error_handling_utils.h.

308 {
309 if (!stream.good()) {
310 if (stream.eof()) {
312 logger_error_code::file_write_failed,
313 "Stream error: Unexpected end of file during " + operation_name
314 );
315 }
316 if (stream.fail()) {
318 logger_error_code::file_write_failed,
319 "Stream error: Logical error during " + operation_name
320 );
321 }
322 if (stream.bad()) {
323 return make_logger_void_result(
324 logger_error_code::file_write_failed,
325 "Stream error: Read/write error during " + operation_name
326 );
327 }
328 return make_logger_void_result(
329 logger_error_code::file_write_failed,
330 "Stream is in an error state after " + operation_name
331 );
332 }
333 return common::ok();
334}

References kcenon::logger::file_write_failed, kcenon::logger::make_logger_void_result(), and kcenon::common::ok().

Referenced by kcenon::logger::file_writer::flush(), kcenon::logger::console_writer::write(), and kcenon::logger::file_writer::write().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ensure_directory_exists()

common::VoidResult kcenon::logger::utils::ensure_directory_exists ( const std::filesystem::path & dir)
inline

Directory creation helper.

Creates a directory if it doesn't exist, with proper error handling.

Parameters
dirThe directory path to create
Returns
common::VoidResult Success if directory exists or was created, error otherwise

Definition at line 369 of file error_handling_utils.h.

369 {
370 if (dir.empty()) {
371 return common::ok(); // Empty path is valid (current directory)
372 }
373
374 return try_open_operation([&]() -> common::VoidResult {
375 if (!std::filesystem::exists(dir)) {
376 if (!std::filesystem::create_directories(dir)) {
378 logger_error_code::file_permission_denied,
379 "Failed to create directory: " + dir.string()
380 );
381 }
382 }
383 return common::ok();
384 });
385}

References kcenon::logger::file_permission_denied, kcenon::logger::make_logger_void_result(), kcenon::common::ok(), and try_open_operation().

Referenced by kcenon::logger::file_writer::open_internal(), and kcenon::logger::rotating_file_writer::perform_rotation().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ log_error_context()

void kcenon::logger::utils::log_error_context ( const error_context & context)
inline

Log error to stderr with context.

Used primarily in destructors where throwing exceptions is not allowed. Provides diagnostic information without propagating errors.

Parameters
contextError context to log

Definition at line 129 of file error_handling_utils.h.

129 {
130 std::cerr << "[logger_system] Error: " << context.to_string() << std::endl;
131}
std::string to_string() const
Convert error context to a formatted string.

References kcenon::logger::utils::error_context::to_string().

Referenced by safe_destructor_operation(), and safe_destructor_result_operation().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ safe_destructor_operation()

template<typename F >
void kcenon::logger::utils::safe_destructor_operation ( const std::string & operation_name,
F && operation )
inlinenoexcept

Safe operation execution for destructors.

Executes an operation in a destructor-safe manner. Exceptions are caught and logged to stderr instead of being propagated, preventing std::terminate.

This function should be used in destructors where throwing exceptions would be unsafe.

Usage example:

~my_writer() {
safe_destructor_operation("flush", [this]() {
flush();
});
}
void safe_destructor_operation(const std::string &operation_name, F &&operation) noexcept
Safe operation execution for destructors.
Template Parameters
FCallable type
Parameters
operation_nameName of the operation for logging
operationThe operation to execute

Definition at line 410 of file error_handling_utils.h.

413 {
414 try {
415 operation();
416 }
417 catch (const std::exception& e) {
418 error_context ctx(
419 logger_error_code::destructor_cleanup_failed,
420 e.what(),
421 operation_name
422 );
423 log_error_context(ctx);
424 }
425 catch (...) {
426 error_context ctx(
427 logger_error_code::destructor_cleanup_failed,
428 "Unknown exception",
429 operation_name
430 );
431 log_error_context(ctx);
432 }
433}

References kcenon::logger::destructor_cleanup_failed, and log_error_context().

Referenced by kcenon::logger::network_writer::~network_writer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ safe_destructor_result_operation()

template<typename F >
void kcenon::logger::utils::safe_destructor_result_operation ( const std::string & operation_name,
F && operation )
inlinenoexcept

Safe operation with result for destructors.

Similar to safe_destructor_operation but for operations that return common::VoidResult. Logs both exceptions and result errors.

Template Parameters
FCallable type that returns common::VoidResult
Parameters
operation_nameName of the operation for logging
operationThe operation to execute

Definition at line 446 of file error_handling_utils.h.

449 {
450 try {
451 auto result = operation();
452 if (result.is_err()) {
453 error_context ctx(
455 result.error().message,
456 operation_name
457 );
459 }
460 }
461 catch (const std::exception& e) {
462 error_context ctx(
463 logger_error_code::destructor_cleanup_failed,
464 e.what(),
465 operation_name
466 );
467 log_error_context(ctx);
468 }
469 catch (...) {
470 error_context ctx(
471 logger_error_code::destructor_cleanup_failed,
472 "Unknown exception",
473 operation_name
474 );
475 log_error_context(ctx);
476 }
477}
void log_error_context(const error_context &context)
Log error to stderr with context.
logger_error_code get_logger_error_code(const common::VoidResult &result)
Structured error context for debugging.

References kcenon::logger::destructor_cleanup_failed, kcenon::logger::get_logger_error_code(), and log_error_context().

Referenced by kcenon::logger::batch_writer::~batch_writer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_encryption_operation()

template<typename F >
common::VoidResult kcenon::logger::utils::try_encryption_operation ( F && operation)

Error handling helper for encryption operations.

Specialized version of try_write_operation for encryption operations. Uses encryption_failed as the default error code.

Template Parameters
FCallable type that returns common::VoidResult
Parameters
operationThe operation to execute
Returns
common::VoidResult Success or error result

Definition at line 254 of file error_handling_utils.h.

254 {
255 return try_write_operation(
256 std::forward<F>(operation),
257 logger_error_code::encryption_failed
258 );
259}
common::VoidResult try_write_operation(F &&operation, logger_error_code default_error_code=logger_error_code::file_write_failed)
Error handling helper for write operations.

References kcenon::logger::encryption_failed, and try_write_operation().

Here is the call graph for this function:

◆ try_network_operation()

template<typename F >
common::VoidResult kcenon::logger::utils::try_network_operation ( F && operation)

Error handling helper for network operations.

Specialized version of try_write_operation for network operations. Uses network_send_failed as the default error code.

Template Parameters
FCallable type that returns common::VoidResult
Parameters
operationThe operation to execute
Returns
common::VoidResult Success or error result

Definition at line 236 of file error_handling_utils.h.

236 {
237 return try_write_operation(
238 std::forward<F>(operation),
239 logger_error_code::network_send_failed
240 );
241}

References kcenon::logger::network_send_failed, and try_write_operation().

Here is the call graph for this function:

◆ try_open_operation()

template<typename F >
common::VoidResult kcenon::logger::utils::try_open_operation ( F && operation)

Error handling helper for file open operations.

Specialized version of try_write_operation for file open operations. Uses file_open_failed as the default error code.

Template Parameters
FCallable type that returns common::VoidResult
Parameters
operationThe operation to execute
Returns
common::VoidResult Success or error result

Definition at line 218 of file error_handling_utils.h.

218 {
219 return try_write_operation(
220 std::forward<F>(operation),
221 logger_error_code::file_open_failed
222 );
223}

References kcenon::logger::file_open_failed, and try_write_operation().

Referenced by ensure_directory_exists(), kcenon::logger::file_writer::open_internal(), and kcenon::logger::rotating_file_writer::perform_rotation().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ try_write_operation()

template<typename F >
common::VoidResult kcenon::logger::utils::try_write_operation ( F && operation,
logger_error_code default_error_code = logger_error_code::file_write_failed )

Error handling helper for write operations.

This function wraps write operations with comprehensive exception handling, mapping different exception types to appropriate logger error codes.

Usage example:

common::VoidResult write(const log_entry& entry) {
// Perform write operation
file_stream_ << formatted << '\n';
return common::ok();
});
}
Represents a single log entry with all associated metadata.
Definition log_entry.h:155
Template Parameters
FCallable type that returns common::VoidResult
Parameters
operationThe operation to execute
default_error_codeError code to use for unexpected exceptions (default: file_write_failed)
Returns
common::VoidResult Success or error result

Definition at line 156 of file error_handling_utils.h.

159 {
160 try {
161 return operation();
162 }
163 catch (const std::filesystem::filesystem_error& e) {
164 // Filesystem-specific errors (permission denied, disk full, etc.)
166 logger_error_code::file_permission_denied,
167 std::string("Filesystem error: ") + e.what()
168 );
169 }
170 catch (const std::ios_base::failure& e) {
171 // I/O operation failures (stream errors, write errors, etc.)
172 return make_logger_void_result(
173 logger_error_code::file_write_failed,
174 std::string("I/O error: ") + e.what()
175 );
176 }
177 catch (const std::system_error& e) {
178 // System-level errors
179 return make_logger_void_result(
180 default_error_code,
181 std::string("System error: ") + e.what()
182 );
183 }
184 catch (const std::bad_alloc& e) {
185 // Memory allocation failures
187 logger_error_code::buffer_overflow,
188 std::string("Memory allocation failed: ") + e.what()
189 );
190 }
191 catch (const std::exception& e) {
192 // Generic exception catch-all
194 default_error_code,
195 std::string("Unexpected error: ") + e.what()
196 );
197 }
198 catch (...) {
199 // Non-standard exception
201 default_error_code,
202 "Unknown error (non-standard exception)"
203 );
204 }
205}

References kcenon::logger::buffer_overflow, kcenon::logger::file_permission_denied, kcenon::logger::file_write_failed, and kcenon::logger::make_logger_void_result().

Referenced by kcenon::logger::rotating_file_writer::cleanup_old_files(), kcenon::logger::console_writer::flush(), kcenon::logger::file_writer::flush(), kcenon::logger::rotating_file_writer::get_backup_files(), kcenon::logger::rotating_file_writer::perform_rotation(), try_encryption_operation(), try_network_operation(), try_open_operation(), kcenon::logger::console_writer::write(), and kcenon::logger::file_writer::write().

Here is the call graph for this function:
Here is the caller graph for this function: