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 :
27 :
28 : /** \file
29 : * \brief Implementation of the logging facility.
30 : *
31 : * The library is very often used by daemons meaning that it will be running
32 : * on its own in the background. For this reason, all the output is done
33 : * through the log facility.
34 : *
35 : * This defines a function which you are expected to call to setup a callback.
36 : * By default, the callback is set to a function that simply prints error
37 : * in std::cerr.
38 : */
39 :
40 : // self
41 : //
42 : #include "advgetopt/log.h"
43 :
44 :
45 : // advgetopt lib
46 : //
47 : #include "advgetopt/exception.h"
48 :
49 :
50 : // C++ lib
51 : //
52 : #include <iostream>
53 :
54 :
55 : // last include
56 : //
57 : #include <snapdev/poison.h>
58 :
59 :
60 :
61 : namespace advgetopt
62 : {
63 :
64 :
65 2 : logger log;
66 :
67 :
68 : namespace
69 : {
70 :
71 :
72 : log_callback g_log_callback = nullptr;
73 :
74 :
75 : } // no name namespace
76 :
77 :
78 :
79 3 : void set_log_callback(log_callback callback)
80 : {
81 3 : g_log_callback = callback;
82 3 : }
83 :
84 :
85 221 : logger & logger::end()
86 : {
87 221 : if(g_log_callback != nullptr)
88 : {
89 220 : g_log_callback(f_level, f_log.str());
90 : }
91 : else
92 : {
93 1 : std::cerr << to_string(f_level) << ": " << f_log.str() << std::endl;
94 : }
95 :
96 221 : f_log.str(std::string());
97 :
98 221 : return *this;
99 : }
100 :
101 :
102 326 : std::string to_string(log_level_t level)
103 : {
104 326 : switch(level)
105 : {
106 : case log_level_t::debug:
107 4 : return "debug";
108 :
109 : case log_level_t::info:
110 3 : return "info";
111 :
112 : case log_level_t::warning:
113 5 : return "warning";
114 :
115 : case log_level_t::error:
116 211 : return "error";
117 :
118 : case log_level_t::fatal:
119 3 : return "fatal";
120 :
121 : }
122 :
123 : throw getopt_exception_invalid("unknown log level ("
124 200 : + std::to_string(static_cast<int>(level))
125 300 : + ")");
126 : }
127 :
128 :
129 6 : } // namespace advgetopt
130 : // vim: ts=4 sw=4 et
|