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 4 : 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 : void add_alias(std::string const & name);
96 : string_vector_t get_all_names() const;
97 :
98 : void set_description(std::string const & description);
99 : std::string get_description() const;
100 :
101 : void set_styles(std::string const & styles);
102 : std::string get_styles() const;
103 :
104 : private:
105 : severity_t const f_severity;
106 : string_vector_t f_names = string_vector_t();
107 : bool const f_system;
108 : std::string f_description = std::string();
109 : std::string f_styles = std::string();
110 : };
111 :
112 :
113 : void add_severity(severity::pointer_t sev);
114 : severity::pointer_t get_severity(std::string const & name);
115 : severity::pointer_t get_severity(message const & msg, std::string const & name);
116 : severity::pointer_t get_severity(severity_t sev);
117 : severity::pointer_t get_severity(message const & msg, severity_t sev);
118 :
119 : template<typename CharT, typename Traits>
120 : inline std::basic_ostream<CharT, Traits> &
121 2 : operator << (std::basic_ostream<CharT, Traits> & os, severity_t sev)
122 : {
123 4 : severity::pointer_t s(get_severity(sev));
124 2 : if(s == nullptr)
125 : {
126 2 : os << "(unknown severity: "
127 : << static_cast<int>(sev)
128 1 : << ")";
129 : }
130 : else
131 : {
132 1 : os << s->get_name();
133 : }
134 4 : return os;
135 : }
136 :
137 :
138 :
139 : } // snaplogger namespace
140 :
141 :
142 : // outside of namespace so it can be used right up
143 : //
144 : snaplogger::severity::pointer_t operator ""_sev (char const * name, unsigned long size);
145 :
146 :
147 : // vim: ts=4 sw=4 et
|