Line data Source code
1 : // Snap Websites Servers -- snap websites child
2 : // Copyright (c) 2013-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 : #pragma once
18 :
19 : // ourselves
20 : //
21 : #include "snapwebsites/snap_config.h"
22 :
23 : // TODO: somehow we need to remove this dependency
24 : // it is here to support the snaplog implementation but it creates
25 : // a circular dependency (snap_communicator <-> log)
26 : // See SNAP-623
27 : //
28 : #include "snapwebsites/snap_communicator.h"
29 :
30 : #include <memory>
31 :
32 : namespace snap
33 : {
34 :
35 : namespace logging
36 : {
37 :
38 : typedef
39 : std::weak_ptr<snap_communicator::snap_tcp_client_permanent_message_connection> messenger_t;
40 :
41 : enum class log_level_t
42 : {
43 : LOG_LEVEL_OFF,
44 : LOG_LEVEL_FATAL,
45 : LOG_LEVEL_ERROR,
46 : LOG_LEVEL_WARNING,
47 : LOG_LEVEL_INFO,
48 : LOG_LEVEL_DEBUG,
49 : LOG_LEVEL_TRACE,
50 :
51 : LOG_LEVEL_DEFAULT = LOG_LEVEL_INFO
52 : };
53 :
54 : enum class log_security_t
55 : {
56 : LOG_SECURITY_NONE,
57 : LOG_SECURITY_SECURE
58 : };
59 :
60 : class logger
61 : {
62 : public:
63 : logger(log_level_t const log_level, char const * file = nullptr, char const * func = nullptr, int const line = -1);
64 : logger(logger const & l);
65 : ~logger();
66 :
67 : // avoid assignments
68 : logger & operator = (logger const &) = delete;
69 :
70 : logger & operator () ();
71 : logger & operator () (log_security_t const v);
72 : logger & operator () (char const * s);
73 : logger & operator () (wchar_t const * s);
74 : logger & operator () (std::string const & s);
75 : logger & operator () (std::wstring const & s);
76 : logger & operator () (QString const & s);
77 : logger & operator () (snap_config::snap_config_parameter_ref const & s);
78 : logger & operator () (char const v);
79 : logger & operator () (signed char const v);
80 : logger & operator () (unsigned char const v);
81 : logger & operator () (signed short const v);
82 : logger & operator () (unsigned short const v);
83 : logger & operator () (signed int const v);
84 : logger & operator () (unsigned int const v);
85 : logger & operator () (signed long const v);
86 : logger & operator () (unsigned long const v);
87 : logger & operator () (signed long long const v);
88 : logger & operator () (unsigned long long const v);
89 : logger & operator () (float const v);
90 : logger & operator () (double const v);
91 : logger & operator () (bool const v);
92 : logger & operator () (void const * p);
93 :
94 : protected:
95 : mutable bool f_ignore = false;
96 :
97 : private:
98 : log_level_t f_log_level = log_level_t::LOG_LEVEL_INFO;
99 : char const * f_file = nullptr;
100 : char const * f_func = nullptr;
101 : int32_t f_line = 1;
102 : log_security_t f_security = log_security_t::LOG_SECURITY_SECURE;
103 : QString f_message = QString();
104 : };
105 :
106 : void set_progname ( std::string const & progname );
107 : std::string get_progname ();
108 : void set_log_messenger ( messenger_t messenger );
109 : void configure_console ();
110 : void configure_logfile ( QString const & logfile );
111 : void configure_syslog ();
112 : void configure_messenger ( messenger_t messenger );
113 : void configure_conffile ( QString const & filename );
114 : void unconfigure ();
115 : void reconfigure ();
116 : bool is_configured ();
117 : log_level_t get_log_output_level ();
118 : void set_log_output_level ( log_level_t level );
119 : void reduce_log_output_level ( log_level_t level );
120 : bool is_enabled_for ( log_level_t const level );
121 :
122 : class raii_log_level
123 : {
124 : public:
125 : raii_log_level(log_level_t new_level)
126 : : f_save_log_level(get_log_output_level())
127 : {
128 : set_log_output_level(new_level);
129 : }
130 :
131 : ~raii_log_level()
132 : {
133 : set_log_output_level(f_save_log_level);
134 : }
135 :
136 : private:
137 : log_level_t f_save_log_level = log_level_t::LOG_LEVEL_DEFAULT;
138 : };
139 :
140 : logger & operator << ( logger & l, QString const & msg );
141 : logger & operator << ( logger & l, std::basic_string<char> const & msg );
142 : logger & operator << ( logger & l, std::basic_string<wchar_t> const & msg );
143 : logger & operator << ( logger & l, snap_config::snap_config_parameter_ref const & msg );
144 : logger & operator << ( logger & l, char const * msg );
145 : logger & operator << ( logger & l, wchar_t const * msg );
146 :
147 : template <class T>
148 0 : logger & operator << ( logger & l, T const & msg )
149 : {
150 0 : l( msg );
151 0 : return l;
152 : }
153 :
154 : logger fatal (char const * file = nullptr, char const * func = nullptr, int line = -1);
155 : logger error (char const * file = nullptr, char const * func = nullptr, int line = -1);
156 : logger warning(char const * file = nullptr, char const * func = nullptr, int line = -1);
157 : logger info (char const * file = nullptr, char const * func = nullptr, int line = -1);
158 : logger debug (char const * file = nullptr, char const * func = nullptr, int line = -1);
159 : logger trace (char const * file = nullptr, char const * func = nullptr, int line = -1);
160 :
161 : #define SNAP_LOG_FATAL snap::logging::fatal (__FILE__, __func__, __LINE__)
162 : #define SNAP_LOG_ERROR snap::logging::error (__FILE__, __func__, __LINE__)
163 : #define SNAP_LOG_WARNING snap::logging::warning(__FILE__, __func__, __LINE__)
164 : #define SNAP_LOG_INFO snap::logging::info (__FILE__, __func__, __LINE__)
165 : #define SNAP_LOG_DEBUG snap::logging::debug (__FILE__, __func__, __LINE__)
166 : #define SNAP_LOG_TRACE snap::logging::trace (__FILE__, __func__, __LINE__)
167 :
168 : } // namespace logging
169 : } // namespace snap
170 : // vim: ts=4 sw=4 et
|