Line data Source code
1 : // Copyright (c) 2013-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/cppthread
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 : /** \file
22 : * \brief Declaration of the log class used to send error messages.
23 : *
24 : * The library offers a log facility for when messages are generated on
25 : * errors and various output (i.e. --help).
26 : */
27 :
28 : // C++ lib
29 : //
30 : #include <iostream>
31 : #include <sstream>
32 :
33 :
34 : namespace cppthread
35 : {
36 :
37 :
38 : enum class log_level_t
39 : {
40 : debug,
41 : info,
42 : warning,
43 : error,
44 : fatal
45 : };
46 :
47 : std::string to_string(log_level_t level);
48 :
49 :
50 : typedef void (*log_callback)(log_level_t level, std::string const & message);
51 :
52 : void set_log_callback(log_callback callback);
53 :
54 :
55 2 : class logger final
56 : {
57 : public:
58 : logger();
59 :
60 : logger & end();
61 : logger & operator << (log_level_t const & level);
62 : logger & operator << (logger & (*func)(logger &));
63 :
64 : template<typename T>
65 0 : logger & operator << (T const & v)
66 : {
67 0 : lock();
68 0 : f_log << v;
69 0 : return *this;
70 : }
71 :
72 : private:
73 : static void lock();
74 : static void unlock();
75 :
76 : log_level_t f_level = log_level_t::error;
77 : std::stringstream f_log = std::stringstream();
78 : };
79 :
80 :
81 0 : inline logger & end(logger & l) { return l.end(); }
82 :
83 :
84 : extern logger log;
85 :
86 :
87 : } // namespace cppthread
88 : // vim: ts=4 sw=4 et
|