unilink  0.4.3
A simple C++ library for unified async communication
error_handler.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2025 Jinwoo Sung
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <atomic>
20 #include <functional>
21 #include <memory>
22 #include <mutex>
23 #include <string_view>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "error_types.hpp"
29 
30 namespace unilink {
31 namespace diagnostics {
32 
40  public:
41  using ErrorCallback = std::function<void(const ErrorInfo&)>;
42 
46  static ErrorHandler& instance();
47  static ErrorHandler& default_handler();
48 
51 
56  void report_error(const ErrorInfo& error);
57 
62  void register_callback(ErrorCallback callback);
63 
67  void clear_callbacks();
68 
73  void set_min_error_level(ErrorLevel level);
74 
78  ErrorLevel get_min_error_level() const;
79 
84  void set_enabled(bool enabled);
85 
89  bool is_enabled() const;
90 
94  ErrorStats get_error_stats() const;
95 
99  void reset_stats();
100 
105  std::vector<ErrorInfo> get_errors_by_component(std::string_view component) const;
106 
111  std::vector<ErrorInfo> get_recent_errors(size_t count = 10) const;
112 
117  bool has_errors(std::string_view component) const;
118 
124  size_t get_error_count(std::string_view component, ErrorLevel level) const;
125 
126  private:
127  // Non-copyable, non-movable
128  ErrorHandler(const ErrorHandler&) = delete;
129  ErrorHandler& operator=(const ErrorHandler&) = delete;
130  ErrorHandler(ErrorHandler&&) = delete;
131  ErrorHandler& operator=(ErrorHandler&&) = delete;
132 
133  mutable std::mutex mutex_;
134  std::vector<ErrorCallback> callbacks_;
135  std::atomic<ErrorLevel> min_level_{ErrorLevel::INFO};
136  std::atomic<bool> enabled_{true};
137 
138  // Statistics
139  mutable std::mutex stats_mutex_;
140  ErrorStats stats_;
141  std::vector<ErrorInfo> recent_errors_;
142  std::unordered_map<std::string, std::vector<ErrorInfo>> errors_by_component_;
143 
144  static constexpr size_t MAX_RECENT_ERRORS = 1000;
145 
146  void update_stats(const ErrorInfo& error);
147  void notify_callbacks(const std::vector<ErrorCallback>& callbacks, const ErrorInfo& error);
148  void add_to_recent_errors(const ErrorInfo& error);
149  void add_to_component_errors(const ErrorInfo& error);
150 };
151 
155 namespace error_reporting {
156 
164 UNILINK_API void report_connection_error(std::string_view component, std::string_view operation,
165  const boost::system::error_code& ec, bool retryable = true);
166 
174 UNILINK_API void report_communication_error(std::string_view component, std::string_view operation,
175  std::string_view message, bool retryable = false);
176 
183 UNILINK_API void report_configuration_error(std::string_view component, std::string_view operation,
184  std::string_view message);
185 
192 UNILINK_API void report_memory_error(std::string_view component, std::string_view operation, std::string_view message);
193 
201 UNILINK_API void report_system_error(std::string_view component, std::string_view operation, std::string_view message,
202  const boost::system::error_code& ec = boost::system::error_code{});
203 
210 UNILINK_API void report_warning(std::string_view component, std::string_view operation, std::string_view message);
211 
218 UNILINK_API void report_info(std::string_view component, std::string_view operation, std::string_view message);
219 
220 } // namespace error_reporting
221 
222 } // namespace diagnostics
223 } // namespace unilink
#define UNILINK_API
Definition: visibility.hpp:37