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