Line data Source code
1 : /*
2 : * Copyright (c) 2013-2019 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_INFORMATION = 50,
63 : SEVERITY_IMPORTANT = 60,
64 : SEVERITY_MINOR = 70,
65 : SEVERITY_DEPRECATED = 80,
66 : SEVERITY_WARNING = 100,
67 : SEVERITY_MAJOR = 150,
68 : SEVERITY_RECOVERABLE_ERROR = 190,
69 : SEVERITY_ERROR = 200,
70 : SEVERITY_CRITICAL = 210,
71 : SEVERITY_ALERT = 220,
72 : SEVERITY_EMERGENCY = 230,
73 : SEVERITY_FATAL = 250,
74 : SEVERITY_OFF = 255,
75 :
76 : SEVERITY_DEFAULT = SEVERITY_INFORMATION,
77 : SEVERITY_MIN = SEVERITY_ALL,
78 : SEVERITY_MAX = SEVERITY_OFF
79 : };
80 :
81 :
82 :
83 :
84 0 : class severity
85 : {
86 : public:
87 : typedef std::shared_ptr<severity> pointer_t;
88 :
89 : severity(severity_t sev, std::string const & name, bool system = false);
90 :
91 : severity_t get_severity() const;
92 : bool is_system() const;
93 :
94 : std::string get_name() const;
95 : string_vector_t get_all_names() const;
96 :
97 : void add_alias(std::string const & name);
98 : string_vector_t get_aliases() const;
99 :
100 : void set_description(std::string const & description);
101 : std::string get_description() const;
102 :
103 : void set_styles(std::string const & color);
104 : std::string get_styles() const;
105 :
106 : private:
107 : severity_t const f_severity;
108 : string_vector_t f_names = string_vector_t();
109 : bool const f_system;
110 : std::string f_description = std::string();
111 : std::string f_styles = std::string();
112 : };
113 :
114 :
115 : void add_severity(severity::pointer_t sev);
116 : severity::pointer_t get_severity(std::string const & name);
117 : severity::pointer_t get_severity(message const & msg, std::string const & name);
118 : severity::pointer_t get_severity(severity_t sev);
119 : severity::pointer_t get_severity(message const & msg, severity_t sev);
120 :
121 : template<typename CharT, typename Traits>
122 : inline std::basic_ostream<CharT, Traits> &
123 : operator << (std::basic_ostream<CharT, Traits> & os, severity_t sev)
124 : {
125 : severity::pointer_t s(get_severity(sev));
126 : if(s == nullptr)
127 : {
128 : os << "(unknown severity: "
129 : << static_cast<int>(sev)
130 : << ")";
131 : }
132 : else
133 : {
134 : os << s->get_name();
135 : }
136 : return os;
137 : }
138 :
139 :
140 :
141 : } // snaplogger namespace
142 : // vim: ts=4 sw=4 et
|