Line data Source code
1 : // Copyright (c) 2013-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snaplogger
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 : #pragma once
20 :
21 :
22 : /** \file
23 : * \brief Severity levels for your log messages.
24 : *
25 : * The severity implementation loads the severity configuration file
26 : * and generates a set of severity levels that one can attach to
27 : * log messages.
28 : */
29 :
30 : // self
31 : //
32 : #include "snaplogger/utils.h"
33 :
34 :
35 : // C++ lib
36 : //
37 : #include <memory>
38 :
39 :
40 : // C lib
41 : //
42 : #include <sys/time.h>
43 :
44 :
45 :
46 : namespace snaplogger
47 : {
48 :
49 :
50 : class message;
51 :
52 :
53 : enum class severity_t
54 : {
55 : SEVERITY_ALL = 0,
56 : SEVERITY_TRACE = 10,
57 : SEVERITY_DEBUG = 20,
58 : SEVERITY_NOTICE = 30,
59 : SEVERITY_UNIMPORTANT = 40,
60 : SEVERITY_VERBOSE = 50,
61 : SEVERITY_INFORMATION = 60,
62 : SEVERITY_IMPORTANT = 70,
63 : SEVERITY_MINOR = 80,
64 : SEVERITY_DEPRECATED = 90,
65 : SEVERITY_WARNING = 100,
66 : SEVERITY_MAJOR = 150,
67 : SEVERITY_RECOVERABLE_ERROR = 190,
68 : SEVERITY_ERROR = 200,
69 : SEVERITY_CRITICAL = 210,
70 : SEVERITY_ALERT = 220,
71 : SEVERITY_EMERGENCY = 230,
72 : SEVERITY_FATAL = 250,
73 : SEVERITY_OFF = 255,
74 :
75 : SEVERITY_DEFAULT = SEVERITY_INFORMATION, // WARNING: can dynamically be changed using the severity.ini file
76 : SEVERITY_MIN = SEVERITY_ALL,
77 : SEVERITY_MAX = SEVERITY_OFF
78 : };
79 :
80 :
81 :
82 :
83 4 : class severity
84 : {
85 : public:
86 : typedef std::shared_ptr<severity> pointer_t;
87 :
88 : severity(severity_t sev, std::string const & name, bool system = false);
89 :
90 : severity_t get_severity() const;
91 : bool is_system() const;
92 : void mark_as_registered();
93 : bool is_registered() const;
94 :
95 : std::string get_name() const;
96 : void add_alias(std::string const & name);
97 : string_vector_t get_all_names() const;
98 :
99 : void set_description(std::string const & description);
100 : std::string get_description() const;
101 :
102 : void set_styles(std::string const & styles);
103 : std::string get_styles() const;
104 :
105 : private:
106 : severity_t const f_severity;
107 : string_vector_t f_names = string_vector_t();
108 : bool const f_system;
109 : bool f_registered = false;
110 : std::string f_description = std::string();
111 : std::string f_styles = std::string();
112 : };
113 :
114 : typedef std::map<severity_t, severity::pointer_t> severity_by_severity_t;
115 : typedef std::map<std::string, severity::pointer_t> severity_by_name_t;
116 :
117 : void add_severity(severity::pointer_t sev);
118 : severity::pointer_t get_severity(std::string const & name);
119 : severity::pointer_t get_severity(message const & msg, std::string const & name);
120 : severity::pointer_t get_severity(severity_t sev);
121 : severity::pointer_t get_severity(message const & msg, severity_t sev);
122 : severity_by_name_t get_severities_by_name();
123 : severity_by_severity_t get_severities_by_severity();
124 :
125 : template<typename CharT, typename Traits>
126 : inline std::basic_ostream<CharT, Traits> &
127 20 : operator << (std::basic_ostream<CharT, Traits> & os, severity_t sev)
128 : {
129 40 : severity::pointer_t s(get_severity(sev));
130 20 : if(s == nullptr)
131 : {
132 2 : os << "(unknown severity: "
133 : << static_cast<int>(sev)
134 1 : << ")";
135 : }
136 : else
137 : {
138 19 : os << s->get_name();
139 : }
140 40 : return os;
141 : }
142 :
143 :
144 :
145 : } // snaplogger namespace
146 :
147 :
148 : // outside of namespace so it can be used right up
149 : //
150 : #if defined(__GNUC__) && __GNUC__ >= 7 && __GNUC_MINOR__ >= 5 && __GNUC_PATCHLEVEL__ >= 0
151 : snaplogger::severity::pointer_t operator ""_sev (char const * name, unsigned long size);
152 : #endif
153 :
154 :
155 : // vim: ts=4 sw=4 et
|