1 | /** |
2 | * Copyright (c) Glow Contributors. See CONTRIBUTORS file. |
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 | #ifndef GLOW_RUNTIME_ERROR_REPORTER_H |
17 | #define GLOW_RUNTIME_ERROR_REPORTER_H |
18 | |
19 | #include "glow/Support/Error.h" |
20 | |
21 | #include <memory> |
22 | #include <string> |
23 | #include <vector> |
24 | |
25 | namespace glow { |
26 | |
27 | /// Interface for exporting runtime statistics. The base implementation |
28 | /// delegates to any subclass registered via `ErrorReporter`. |
29 | class ErrorReporter { |
30 | public: |
31 | /// Dtor. |
32 | virtual ~ErrorReporter() = default; |
33 | |
34 | /// Report error to some sink. |
35 | virtual void report(const std::string &msg) = 0; |
36 | }; |
37 | |
38 | /// Registry of ErrorReporters.. |
39 | class ErrorReporterRegistry final { |
40 | public: |
41 | /// Start all the error reporters. |
42 | void report(const std::string &msg) { |
43 | for (auto *r : reporters_) { |
44 | r->report(msg); |
45 | } |
46 | } |
47 | |
48 | /// Register a ErrorReporter. |
49 | void registerErrorReporter(ErrorReporter *reporter); |
50 | |
51 | /// Revoke a ErrorReporter. |
52 | void revokeErrorReporter(ErrorReporter *reporter); |
53 | |
54 | /// Static singleton ErrorReporterRegistry. |
55 | static std::shared_ptr<ErrorReporterRegistry> ErrorReporters(); |
56 | |
57 | private: |
58 | /// Registered ErrorReporter.. |
59 | std::vector<ErrorReporter *> reporters_; |
60 | }; |
61 | |
62 | /// Reports the error with registered reporters and returns the same error for |
63 | /// pipelining. |
64 | detail::GlowError reportOnError(detail::GlowError error); |
65 | |
66 | } // namespace glow |
67 | |
68 | /// Unwraps the T from within an Expected<T>. If the Expected<T> contains |
69 | /// an ErrorValue, the program will report it and exit. |
70 | #define REPORT_AND_EXIT_ON_ERR(...) \ |
71 | (glow::detail::exitOnError(__FILE__, __LINE__, \ |
72 | glow::reportOnError(__VA_ARGS__))) |
73 | |
74 | #endif // GLOW_RUNTIME_ERROR_REPORTER_H |
75 | |