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