Branch data Line data Source code
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 : : #include <exception>
19 : : #include <memory>
20 : : #include <string>
21 : : #include <string_view>
22 : : #include <utility>
23 : :
24 : : #include "wirestead/diagnostics/logger.hpp"
25 : :
26 : : namespace wirestead::diagnostics {
27 : : // Notification exceptions never become another user error notification.
28 : : // Even a logging failure must not escape an I/O completion.
29 : : template <class Callback, class... Args>
30 : 14824 : bool invoke_callback(std::string_view component, std::string_view operation, const Callback& callback,
31 : : Args&&... args) noexcept {
32 [ + + ]: 14824 : if (!callback) return true;
33 : : try {
34 : 14530 : callback(std::forward<Args>(args)...);
35 : 14486 : return true;
36 : 69 : } catch (const std::exception& error) {
37 : : try {
38 : 78 : WIRESTEAD_LOG_ERROR(component, operation, std::string("Exception in callback: ") + error.what());
39 : 0 : } catch (...) {
40 : : }
41 : 34 : } catch (...) {
42 : : try {
43 : 17 : WIRESTEAD_LOG_ERROR(component, operation, "Unknown exception in callback");
44 : 0 : } catch (...) {
45 : : }
46 : : }
47 : 43 : return false;
48 : : }
49 : : template <class Callback, class... Args>
50 : 16160 : bool invoke_callback(std::string_view component, std::string_view operation, const std::shared_ptr<Callback>& callback,
51 : : Args&&... args) noexcept {
52 [ + + + + ]: 16160 : return !callback || invoke_callback(component, operation, *callback, std::forward<Args>(args)...);
53 : : }
54 : : } // namespace wirestead::diagnostics
|