1 /*
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2021 Brenden Matthews, Philip Kovacs, et. al.
13  *	(see AUTHORS)
14  * All rights reserved.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  *
28  */
29 
30 #ifndef _LOGGING_H
31 #define _LOGGING_H
32 
33 #include <cstdio>
34 #include <stdexcept>
35 #include "config.h"
36 #include "i18n.h"
37 
38 class fork_throw : public std::runtime_error {
39  public:
fork_throw()40   fork_throw() : std::runtime_error("Fork happened") {}
fork_throw(const std::string & msg)41   fork_throw(const std::string &msg) : std::runtime_error(msg) {}
42 };
43 
44 class unknown_arg_throw : public std::runtime_error {
45  public:
unknown_arg_throw()46   unknown_arg_throw() : std::runtime_error("Unknown argumunt given") {}
unknown_arg_throw(const std::string & msg)47   unknown_arg_throw(const std::string &msg) : std::runtime_error(msg) {}
48 };
49 
50 class combine_needs_2_args_error : public std::runtime_error {
51  public:
combine_needs_2_args_error()52   combine_needs_2_args_error()
53       : std::runtime_error("combine needs arguments: <text1> <text2>") {}
combine_needs_2_args_error(const std::string & msg)54   combine_needs_2_args_error(const std::string &msg)
55       : std::runtime_error(msg) {}
56 };
57 
58 class obj_create_error : public std::runtime_error {
59  public:
obj_create_error()60   obj_create_error() : std::runtime_error("Failed to create object") {}
obj_create_error(const std::string & msg)61   obj_create_error(const std::string &msg) : std::runtime_error(msg) {}
62 };
63 
64 void clean_up(void *memtofree1, void *memtofree2);
65 void clean_up_without_threads(void *memtofree1, void *memtofree2);
66 
67 template <typename... Args>
gettextize_format(const char * format,Args &&...args)68 inline void gettextize_format(const char *format, Args &&...args) {
69   fprintf(stderr, _(format), args...);
70 }
71 
72 // explicit specialization for no arguments to avoid the
73 // "format not a string literal and no format arguments" warning
gettextize_format(const char * format)74 inline void gettextize_format(const char *format) { fputs(_(format), stderr); }
75 
76 template <typename... Args>
NORM_ERR(const char * format,Args &&...args)77 void NORM_ERR(const char *format, Args &&...args) {
78   fprintf(stderr, PACKAGE_NAME ": ");
79   gettextize_format(format, args...);
80   fputs("\n", stderr);
81 }
82 
83 /* critical error */
84 template <typename... Args>
CRIT_ERR(void * memtofree1,void * memtofree2,const char * format,Args &&...args)85 inline void CRIT_ERR(void *memtofree1, void *memtofree2, const char *format,
86                      Args &&...args) {
87   NORM_ERR(format, args...);
88   clean_up(memtofree1, memtofree2);
89   exit(EXIT_FAILURE);
90 }
91 
92 template <typename... Args>
THREAD_CRIT_ERR(void * memtofree1,void * memtofree2,const char * format,Args &&...args)93 inline void THREAD_CRIT_ERR(void *memtofree1, void *memtofree2,
94                             const char *format, Args &&...args) {
95   NORM_ERR(format, args...);
96   clean_up_without_threads(memtofree1, memtofree2);
97   return;
98 }
99 
100 namespace conky {
101 class error : public std::runtime_error {
102  public:
error(const std::string & msg)103   error(const std::string &msg) : std::runtime_error(msg) {}
104 };
105 }  // namespace conky
106 
107 /* debugging output */
108 extern int global_debug_level;
109 
110 #define __DBGP(level, ...)                                               \
111   do {                                                                   \
112     if (global_debug_level > level) {                                    \
113       fprintf(stderr, "DEBUG(%d) [" __FILE__ ":%d]: ", level, __LINE__); \
114       gettextize_format(__VA_ARGS__);                                    \
115       fputs("\n", stderr);                                               \
116     }                                                                    \
117   } while (0)
118 #define DBGP(...) __DBGP(0, __VA_ARGS__)
119 #define DBGP2(...) __DBGP(1, __VA_ARGS__)
120 
121 #endif /* _LOGGING_H */
122