/src/trafficserver/include/tsutil/ts_errata.h
Line | Count | Source |
1 | | /** @file Diagnostic definitions and functions. |
2 | | |
3 | | @section license License |
4 | | |
5 | | Licensed to the Apache Software Foundation (ASF) under one |
6 | | or more contributor license agreements. See the NOTICE file |
7 | | distributed with this work for additional information |
8 | | regarding copyright ownership. The ASF licenses this file |
9 | | to you under the Apache License, Version 2.0 (the |
10 | | "License"); you may not use this file except in compliance |
11 | | with the License. You may obtain a copy of the License at |
12 | | |
13 | | http://www.apache.org/licenses/LICENSE-2.0 |
14 | | |
15 | | Unless required by applicable law or agreed to in writing, software |
16 | | distributed under the License is distributed on an "AS IS" BASIS, |
17 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
18 | | See the License for the specific language governing permissions and |
19 | | limitations under the License. |
20 | | */ |
21 | | |
22 | | #pragma once |
23 | | |
24 | | #include <utility> |
25 | | #include <unordered_map> |
26 | | |
27 | | #include "tsutil/ts_diag_levels.h" |
28 | | #include "swoc/TextView.h" |
29 | | #include "tsutil/ts_bw_format.h" |
30 | | #include "swoc/Errata.h" |
31 | | |
32 | | static constexpr swoc::Errata::Severity ERRATA_DIAG{DL_Diag}; |
33 | | static constexpr swoc::Errata::Severity ERRATA_DEBUG{DL_Debug}; |
34 | | static constexpr swoc::Errata::Severity ERRATA_STATUS{DL_Status}; |
35 | | static constexpr swoc::Errata::Severity ERRATA_NOTE{DL_Note}; |
36 | | static constexpr swoc::Errata::Severity ERRATA_WARN{DL_Warning}; |
37 | | static constexpr swoc::Errata::Severity ERRATA_ERROR{DL_Error}; |
38 | | static constexpr swoc::Errata::Severity ERRATA_FATAL{DL_Fatal}; |
39 | | static constexpr swoc::Errata::Severity ERRATA_ALERT{DL_Alert}; |
40 | | static constexpr swoc::Errata::Severity ERRATA_EMERGENCY{DL_Emergency}; |
41 | | |
42 | | inline DiagsLevel |
43 | | diags_level_of(swoc::Errata::Severity s) |
44 | 0 | { |
45 | 0 | return static_cast<DiagsLevel>(static_cast<int>(s)); |
46 | 0 | } |
47 | | |
48 | | // This is treated as an array so must numerically match with @c DiagsLevel |
49 | | static constexpr std::array<swoc::TextView, 9> Severity_Names{ |
50 | | {"Diag", "Debug", "Status", "Note", "Warn", "Error", "Fatal", "Alert", "Emergency"} |
51 | | }; |
52 | | |
53 | | namespace ts |
54 | | { |
55 | | |
56 | | inline std::error_code |
57 | | make_errno_code() |
58 | 0 | { |
59 | 0 | return {errno, std::system_category()}; |
60 | 0 | } |
61 | | |
62 | | inline std::error_code |
63 | | make_errno_code(int err) |
64 | 0 | { |
65 | 0 | return {err, std::system_category()}; |
66 | 0 | } |
67 | | |
68 | | template <typename... Args> |
69 | | void |
70 | | bw_log(DiagsLevel /* lvl ATS_UNUSED */, swoc::TextView fmt, Args &&...args) |
71 | | { |
72 | | swoc::bwprint_v(ts::bw_dbg, fmt, std::forward_as_tuple(args...)); |
73 | | } |
74 | | |
75 | | class err_category : public std::error_category |
76 | | { |
77 | | using self_type = err_category; |
78 | | |
79 | | public: |
80 | | /// @return Name of the category. |
81 | | char const *name() const noexcept override; |
82 | | |
83 | | /** Convert code to condition. |
84 | | * |
85 | | * @param code Numeric error code. |
86 | | * @return The correspodning condition. |
87 | | */ |
88 | | std::error_condition default_error_condition(int code) const noexcept override; |
89 | | |
90 | | /** Is numeric error code equivalent to condition. |
91 | | * |
92 | | * @param code Numeric error code. |
93 | | * @param condition Condition object. |
94 | | * @return @a true if the arguments are equivalent. |
95 | | */ |
96 | | bool equivalent(int code, std::error_condition const &condition) const noexcept override; |
97 | | |
98 | | /** Is error code enumeration value equivalent to error code object. |
99 | | * |
100 | | * @param ec Condition object. |
101 | | * @param code Numeric error code. |
102 | | * @return @a true if the arguments are equivalent. |
103 | | */ |
104 | | bool equivalent(std::error_code const &ec, int code) const noexcept override; |
105 | | |
106 | | /// @return Text for @c code. |
107 | | std::string message(int code) const override; |
108 | | |
109 | | protected: |
110 | | using table_type = std::unordered_map<int, std::string>; |
111 | | /// Mapping from numeric error code to message string. |
112 | | static table_type const _table; |
113 | | }; |
114 | | |
115 | | inline char const * |
116 | | ts::err_category::name() const noexcept |
117 | 0 | { |
118 | 0 | return "trafficserver"; |
119 | 0 | } |
120 | | |
121 | | inline std::error_condition |
122 | | err_category::default_error_condition(int code) const noexcept |
123 | 0 | { |
124 | 0 | return {code, *this}; |
125 | 0 | } |
126 | | |
127 | | inline bool |
128 | | err_category::equivalent(const std::error_code &ec, int code) const noexcept |
129 | 0 | { |
130 | 0 | return ec.value() == code; |
131 | 0 | } |
132 | | |
133 | | inline bool |
134 | | err_category::equivalent(int code, const std::error_condition &condition) const noexcept |
135 | 0 | { |
136 | 0 | return code == condition.value(); |
137 | 0 | } |
138 | | |
139 | | } // namespace ts |