Line data Source code
1 : /*
2 : * License:
3 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
4 : *
5 : * https://snapwebsites.org/
6 : * contact@m2osw.com
7 : *
8 : * This program is free software; you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation; either version 2 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License along
19 : * with this program; if not, write to the Free Software Foundation, Inc.,
20 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 : *
22 : * Authors:
23 : * Alexis Wilke alexis@m2osw.com
24 : * Doug Barbieri doug@m2osw.com
25 : */
26 : #pragma once
27 :
28 : /** \file
29 : * \brief Declaration of the log class used to send error messages.
30 : *
31 : * The library offers a log facility for when messages are generated on
32 : * errors and various output (i.e. --help).
33 : */
34 :
35 : // C++ lib
36 : //
37 : #include <iostream>
38 : #include <sstream>
39 :
40 :
41 : namespace advgetopt
42 : {
43 :
44 :
45 : enum class log_level_t
46 : {
47 : debug,
48 : info,
49 : warning,
50 : error,
51 : fatal
52 : };
53 :
54 : std::string to_string(log_level_t level);
55 :
56 :
57 : typedef void (*log_callback)(log_level_t level, std::string const & message);
58 :
59 : void set_log_callback(log_callback callback);
60 :
61 :
62 4 : class logger
63 : {
64 : public:
65 : template<typename T>
66 1253 : logger & operator << (T const & v)
67 : {
68 1253 : f_log << v;
69 1253 : return *this;
70 : }
71 :
72 221 : logger & operator << (log_level_t const & level)
73 : {
74 221 : f_level = level;
75 221 : return *this;
76 : }
77 :
78 221 : logger & operator << (logger & (*func)(logger &))
79 : {
80 221 : func(*this);
81 221 : return *this;
82 : }
83 :
84 : logger & end();
85 :
86 : private:
87 : log_level_t f_level = log_level_t::error;
88 : std::stringstream f_log = std::stringstream();
89 : };
90 :
91 :
92 221 : inline logger & end(logger & l) { return l.end(); }
93 :
94 :
95 : extern logger log;
96 :
97 :
98 : } // namespace advgetopt
99 : // vim: ts=4 sw=4 et
|