1 /*
2  * Copyright 2003 James Bursa <bursa@users.sourceforge.net>
3  * Copyright 2004 John Tytgat <joty@netsurf-browser.org>
4  *
5  * This file is part of NetSurf, http://www.netsurf-browser.org/
6  *
7  * NetSurf is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * NetSurf 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
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef NETSURF_LOG_H
21 #define NETSURF_LOG_H
22 
23 #include <stdio.h>
24 #include <stdbool.h>
25 
26 #include "utils/errors.h"
27 
28 extern bool verbose_log;
29 
30 /**
31  * Ensures the FILE handle is available to write logging to.
32  *
33  * This is provided by the frontends if required
34  */
35 typedef bool(nslog_ensure_t)(FILE *fptr);
36 
37 /**
38  * Initialise the logging system.
39  *
40  * Sets up everything required for logging. Processes the argv passed
41  * to remove the -v switch for verbose logging. If necessary ensures
42  * the output file handle is available.
43  */
44 extern nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv);
45 
46 /**
47  * Shut down the logging system.
48  *
49  * Shuts down the logging subsystem, resetting to verbose logging and output
50  * to stderr.  Note, if logging is done after calling this, it will be sent
51  * to stderr without filtering.
52  */
53 extern void nslog_finalise(void);
54 
55 /**
56  * Set the logging filter.
57  *
58  * Compiles and enables the given logging filter.
59  */
60 extern nserror nslog_set_filter(const char *filter);
61 
62 /**
63  * Set the logging filter according to the options.
64  */
65 extern nserror nslog_set_filter_by_options(void);
66 
67 /* ensure a logging level is defined */
68 #ifndef NETSURF_LOG_LEVEL
69 #define NETSURF_LOG_LEVEL INFO
70 #endif
71 
72 #define NSLOG_LVL(level) NSLOG_LEVEL_ ## level
73 #define NSLOG_EVL(level) NSLOG_LVL(level)
74 #define NSLOG_COMPILED_MIN_LEVEL NSLOG_EVL(NETSURF_LOG_LEVEL)
75 
76 #ifdef WITH_NSLOG
77 
78 #include <nslog/nslog.h>
79 
80 NSLOG_DECLARE_CATEGORY(netsurf);
81 NSLOG_DECLARE_CATEGORY(llcache);
82 NSLOG_DECLARE_CATEGORY(fetch);
83 NSLOG_DECLARE_CATEGORY(plot);
84 NSLOG_DECLARE_CATEGORY(schedule);
85 NSLOG_DECLARE_CATEGORY(fbtk);
86 NSLOG_DECLARE_CATEGORY(layout);
87 NSLOG_DECLARE_CATEGORY(dukky);
88 NSLOG_DECLARE_CATEGORY(jserrors);
89 
90 #else /* WITH_NSLOG */
91 
92 enum nslog_level {
93 	NSLOG_LEVEL_DEEPDEBUG = 0,
94 	NSLOG_LEVEL_DEBUG = 1,
95 	NSLOG_LEVEL_VERBOSE = 2,
96 	NSLOG_LEVEL_INFO = 3,
97 	NSLOG_LEVEL_WARNING = 4,
98 	NSLOG_LEVEL_ERROR = 5,
99 	NSLOG_LEVEL_CRITICAL = 6
100 };
101 
102 extern void nslog_log(const char *file, const char *func, int ln, const char *format, ...) __attribute__ ((format (printf, 4, 5)));
103 
104 #  ifdef __GNUC__
105 #    define LOG_FN __PRETTY_FUNCTION__
106 #    define LOG_LN __LINE__
107 #  elif defined(__CC_NORCROFT)
108 #    define LOG_FN __func__
109 #    define LOG_LN __LINE__
110 #  else
111 #    define LOG_FN ""
112 #    define LOG_LN __LINE__
113 #  endif
114 
115 #define NSLOG(catname, level, logmsg, args...)				\
116 	do {								\
117 		if (NSLOG_LEVEL_##level >= NSLOG_COMPILED_MIN_LEVEL) {	\
118 			nslog_log(__FILE__, LOG_FN, LOG_LN, logmsg , ##args); \
119 		}							\
120 	} while(0)
121 
122 #endif  /* WITH_NSLOG */
123 
124 #endif
125