14#include <kcenon/common/config/feature_flags.h>
16#include <system_error>
21#if KCENON_HAS_SOURCE_LOCATION
22# include <source_location>
26#ifndef LOGGER_HAS_SOURCE_LOCATION
27# define LOGGER_HAS_SOURCE_LOCATION KCENON_HAS_SOURCE_LOCATION
51#if KCENON_HAS_SOURCE_LOCATION
61 std::string error_message,
63 std::source_location loc = std::source_location::current()
65 message(std::move(error_message)),
70 timestamp(std::chrono::system_clock::now()) {}
82 std::string error_message,
84 std::string file =
"",
87 message(std::move(error_message)),
92 timestamp(std::chrono::system_clock::now()) {}
100 std::ostringstream oss;
130 std::cerr <<
"[logger_system] Error: " << context.
to_string() << std::endl;
163 catch (
const std::filesystem::filesystem_error& e) {
167 std::string(
"Filesystem error: ") + e.what()
170 catch (
const std::ios_base::failure& e) {
174 std::string(
"I/O error: ") + e.what()
177 catch (
const std::system_error& e) {
181 std::string(
"System error: ") + e.what()
184 catch (
const std::bad_alloc& e) {
188 std::string(
"Memory allocation failed: ") + e.what()
191 catch (
const std::exception& e) {
195 std::string(
"Unexpected error: ") + e.what()
202 "Unknown error (non-standard exception)"
220 std::forward<F>(operation),
238 std::forward<F>(operation),
256 std::forward<F>(operation),
285 const std::string& message
304template<
typename Stream>
306 const Stream& stream,
307 const std::string& operation_name =
"operation"
309 if (!stream.good()) {
313 "Stream error: Unexpected end of file during " + operation_name
319 "Stream error: Logical error during " + operation_name
325 "Stream error: Read/write error during " + operation_name
330 "Stream is in an error state after " + operation_name
346 if (!std::filesystem::exists(path)) {
349 "File does not exist: " + path.string()
353 }
catch (
const std::filesystem::filesystem_error& e) {
356 std::string(
"Cannot access file: ") + e.what()
375 if (!std::filesystem::exists(dir)) {
376 if (!std::filesystem::create_directories(dir)) {
379 "Failed to create directory: " + dir.string()
411 const std::string& operation_name,
417 catch (
const std::exception& e) {
447 const std::string& operation_name,
451 auto result = operation();
461 catch (
const std::exception& e) {
Error codes specific to the logger system.
void safe_destructor_operation(const std::string &operation_name, F &&operation) noexcept
Safe operation execution for destructors.
common::VoidResult try_network_operation(F &&operation)
Error handling helper for network operations.
common::VoidResult check_stream_state(const Stream &stream, const std::string &operation_name="operation")
Stream state verification helper.
void log_error_context(const error_context &context)
Log error to stderr with context.
common::VoidResult try_open_operation(F &&operation)
Error handling helper for file open operations.
common::VoidResult check_file_exists(const std::filesystem::path &path)
File existence verification helper.
common::VoidResult try_encryption_operation(F &&operation)
Error handling helper for encryption operations.
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.
common::VoidResult check_condition(bool condition, logger_error_code error_code, const std::string &message)
Condition verification helper.
common::VoidResult ensure_directory_exists(const std::filesystem::path &dir)
Directory creation helper.
void safe_destructor_result_operation(const std::string &operation_name, F &&operation) noexcept
Safe operation with result for destructors.
logger_error_code
Error codes specific to the logger system.
@ destructor_cleanup_failed
common::VoidResult make_logger_void_result(logger_error_code code, const std::string &message="")
logger_error_code get_logger_error_code(const common::VoidResult &result)
std::string logger_error_to_string(logger_error_code code)
Convert logger_error_code to string representation.
Structured error context for debugging.
std::chrono::system_clock::time_point timestamp
std::string function_name
error_context(logger_error_code error_code, std::string error_message, std::string op="", std::string file="", int line=0)
Construct error context with manual location information.
std::string to_string() const
Convert error context to a formatted string.