23 #include <string_view>
28 namespace diagnostics {
41 if (!enabled_.load()) {
45 if (error.
level < min_level_.load()) {
49 std::vector<ErrorCallback> callbacks_copy;
51 std::lock_guard<std::mutex> lock(mutex_);
53 add_to_recent_errors(error);
54 add_to_component_errors(error);
55 callbacks_copy = callbacks_;
57 notify_callbacks(callbacks_copy, error);
61 std::lock_guard<std::mutex> lock(mutex_);
62 callbacks_.push_back(std::move(callback));
66 std::lock_guard<std::mutex> lock(mutex_);
79 std::lock_guard<std::mutex> lock(stats_mutex_);
84 std::lock_guard<std::mutex> lock(stats_mutex_);
89 std::lock_guard<std::mutex> lock(mutex_);
90 auto it = errors_by_component_.find(std::string(component));
91 if (it != errors_by_component_.end()) {
98 std::lock_guard<std::mutex> lock(mutex_);
100 size_t start_index = 0;
101 if (recent_errors_.size() > count) {
102 start_index = recent_errors_.size() - count;
105 return std::vector<ErrorInfo>(recent_errors_.begin() +
static_cast<std::ptrdiff_t
>(start_index),
106 recent_errors_.end());
110 std::lock_guard<std::mutex> lock(mutex_);
111 auto it = errors_by_component_.find(std::string(component));
112 return it != errors_by_component_.end() && !it->second.empty();
116 std::lock_guard<std::mutex> lock(mutex_);
117 auto it = errors_by_component_.find(std::string(component));
118 if (it == errors_by_component_.end()) {
122 return static_cast<size_t>(std::count_if(it->second.begin(), it->second.end(),
123 [level](
const ErrorInfo& error) { return error.level == level; }));
126 void ErrorHandler::update_stats(
const ErrorInfo& error) {
127 std::lock_guard<std::mutex> lock(stats_mutex_);
137 if (stats_.
first_error == std::chrono::system_clock::time_point{}) {
143 void ErrorHandler::notify_callbacks(
const std::vector<ErrorCallback>& callbacks,
const ErrorInfo& error) {
144 for (
const auto& callback : callbacks) {
147 }
catch (
const std::exception& e) {
149 UNILINK_LOG_ERROR(
"error_handler",
"callback",
"Error in error callback: " + std::string(e.what()));
151 UNILINK_LOG_ERROR(
"error_handler",
"callback",
"Unknown error in error callback");
156 void ErrorHandler::add_to_recent_errors(
const ErrorInfo& error) {
157 recent_errors_.push_back(error);
160 if (recent_errors_.size() > MAX_RECENT_ERRORS) {
161 recent_errors_.erase(
162 recent_errors_.begin(),
163 recent_errors_.begin() +
static_cast<std::ptrdiff_t
>(recent_errors_.size() - MAX_RECENT_ERRORS));
167 void ErrorHandler::add_to_component_errors(
const ErrorInfo& error) {
168 errors_by_component_[error.component].push_back(error);
171 constexpr
size_t MAX_COMPONENT_ERRORS = 100;
172 auto& component_errors = errors_by_component_[error.component];
173 if (component_errors.size() > MAX_COMPONENT_ERRORS) {
174 component_errors.erase(
175 component_errors.begin(),
176 component_errors.begin() +
static_cast<std::ptrdiff_t
>(component_errors.size() - MAX_COMPONENT_ERRORS));
181 namespace error_reporting {
184 const boost::system::error_code& ec,
bool retryable) {
185 ErrorInfo error(ErrorLevel::ERROR, ErrorCategory::CONNECTION, component, operation, ec.message(), ec, retryable);
186 ErrorHandler::instance().report_error(error);
191 ErrorInfo error(ErrorLevel::ERROR, ErrorCategory::COMMUNICATION, component, operation, message);
193 ErrorHandler::instance().report_error(error);
197 ErrorInfo error(ErrorLevel::ERROR, ErrorCategory::CONFIGURATION, component, operation, message);
198 ErrorHandler::instance().report_error(error);
201 void report_memory_error(std::string_view component, std::string_view operation, std::string_view message) {
202 ErrorInfo error(ErrorLevel::CRITICAL, ErrorCategory::MEMORY, component, operation, message);
203 ErrorHandler::instance().report_error(error);
206 void report_system_error(std::string_view component, std::string_view operation, std::string_view message,
207 const boost::system::error_code& ec) {
208 ErrorInfo error(ErrorLevel::ERROR, ErrorCategory::SYSTEM, component, operation, message, ec);
209 ErrorHandler::instance().report_error(error);
212 void report_warning(std::string_view component, std::string_view operation, std::string_view message) {
213 ErrorInfo error(ErrorLevel::WARNING, ErrorCategory::UNKNOWN, component, operation, message);
214 ErrorHandler::instance().report_error(error);
217 void report_info(std::string_view component, std::string_view operation, std::string_view message) {
218 ErrorInfo error(ErrorLevel::INFO, ErrorCategory::UNKNOWN, component, operation, message);
219 ErrorHandler::instance().report_error(error);
Centralized error handling system.
std::vector< ErrorInfo > get_errors_by_component(std::string_view component) const
Get errors by component.
bool is_enabled() const
Check if error reporting is enabled.
bool has_errors(std::string_view component) const
Check if component has any errors.
void report_error(const ErrorInfo &error)
Report an error.
size_t get_error_count(std::string_view component, ErrorLevel level) const
Get error count for specific component and level.
ErrorLevel get_min_error_level() const
Get current minimum error level.
static ErrorHandler & default_handler()
std::function< void(const ErrorInfo &)> ErrorCallback
static ErrorHandler & instance()
Get singleton instance.
void reset_stats()
Reset error statistics.
std::vector< ErrorInfo > get_recent_errors(size_t count=10) const
Get recent errors.
void clear_callbacks()
Unregister all callbacks.
void register_callback(ErrorCallback callback)
Register error callback.
void set_enabled(bool enabled)
Enable/disable error reporting.
ErrorStats get_error_stats() const
Get error statistics.
void set_min_error_level(ErrorLevel level)
Set minimum error level to report.
#define UNILINK_LOG_ERROR(component, operation, message)
void report_warning(std::string_view component, std::string_view operation, std::string_view message)
Report warning (non-critical issue)
void report_memory_error(std::string_view component, std::string_view operation, std::string_view message)
Report memory-related error.
void report_configuration_error(std::string_view component, std::string_view operation, std::string_view message)
Report configuration error.
void report_communication_error(std::string_view component, std::string_view operation, std::string_view message, bool retryable)
Report communication-related error.
void report_connection_error(std::string_view component, std::string_view operation, const boost::system::error_code &ec, bool retryable)
Report connection-related error.
void report_system_error(std::string_view component, std::string_view operation, std::string_view message, const boost::system::error_code &ec)
Report system-level error.
void report_info(std::string_view component, std::string_view operation, std::string_view message)
Report informational message.
ErrorLevel
Error severity levels.
Comprehensive error information structure.
std::chrono::system_clock::time_point timestamp
Error statistics for monitoring.
std::chrono::system_clock::time_point first_error
size_t errors_by_level[4]
void reset()
Reset all statistics.
size_t errors_by_category[6]