NetSurf
log.c
Go to the documentation of this file.
1/*
2 * Copyright 2017 Vincent Sanders <vince@netsurf-browser.org>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdarg.h>
20#include <stdio.h>
21
22#include "utils/config.h"
23#include "utils/nsoption.h"
24#include "utils/sys_time.h"
25#include "utils/utsname.h"
26#include "desktop/version.h"
27
28#include "utils/log.h"
29
30/** flag to enable verbose logging */
31bool verbose_log = false;
32
33/** The stream to which logging is sent */
34static FILE *logfile;
35
36/** Subtract the `struct timeval' values X and Y
37 *
38 * \param result The timeval structure to store the result in
39 * \param x The first value
40 * \param y The second value
41 * \return 1 if the difference is negative, otherwise 0.
42 */
43static int
44timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
45{
46 /* Perform the carry for the later subtraction by updating y. */
47 if (x->tv_usec < y->tv_usec) {
48 int nsec = (int)(y->tv_usec - x->tv_usec) / 1000000 + 1;
49 y->tv_usec -= 1000000 * nsec;
50 y->tv_sec += nsec;
51 }
52 if ((int)(x->tv_usec - y->tv_usec) > 1000000) {
53 int nsec = (int)(x->tv_usec - y->tv_usec) / 1000000;
54 y->tv_usec += 1000000 * nsec;
55 y->tv_sec -= nsec;
56 }
57
58 /* Compute the time remaining to wait.
59 tv_usec is certainly positive. */
60 result->tv_sec = x->tv_sec - y->tv_sec;
61 result->tv_usec = x->tv_usec - y->tv_usec;
62
63 /* Return 1 if result is negative. */
64 return x->tv_sec < y->tv_sec;
65}
66
67/**
68 * Obtain a formatted string suitable for prepending to a log message
69 *
70 * \return formatted string of the time since first log call
71 */
72static const char *nslog_gettime(void)
73{
74 static struct timeval start_tv;
75 static char buff[32];
76
77 struct timeval tv;
78 struct timeval now_tv;
79
80 if (!timerisset(&start_tv)) {
81 gettimeofday(&start_tv, NULL);
82 }
83 gettimeofday(&now_tv, NULL);
84
85 timeval_subtract(&tv, &now_tv, &start_tv);
86
87 snprintf(buff, sizeof(buff),"(%ld.%06ld)",
88 (long)tv.tv_sec, (long)tv.tv_usec);
89
90 return buff;
91}
92
93#ifdef WITH_NSLOG
94
95NSLOG_DEFINE_CATEGORY(netsurf, "NetSurf default logging");
96NSLOG_DEFINE_CATEGORY(llcache, "Low level cache");
97NSLOG_DEFINE_CATEGORY(fetch, "Object fetching");
98NSLOG_DEFINE_CATEGORY(plot, "Rendering system");
99NSLOG_DEFINE_CATEGORY(schedule, "Scheduler");
100NSLOG_DEFINE_CATEGORY(fbtk, "Framebuffer toolkit");
101NSLOG_DEFINE_CATEGORY(layout, "Layout");
102NSLOG_DEFINE_CATEGORY(flex, "Flex");
103NSLOG_DEFINE_CATEGORY(dukky, "Duktape JavaScript Binding");
104NSLOG_DEFINE_CATEGORY(jserrors, "JavaScript error messages");
105
106static void
107netsurf_render_log(void *_ctx,
108 nslog_entry_context_t *ctx,
109 const char *fmt,
110 va_list args)
111{
112 fprintf(logfile,
113 "%s [%s %.*s] %.*s:%i %.*s: ",
115 nslog_short_level_name(ctx->level),
116 ctx->category->namelen,
117 ctx->category->name,
118 ctx->filenamelen,
119 ctx->filename,
120 ctx->lineno,
121 ctx->funcnamelen,
122 ctx->funcname);
123
124 vfprintf(logfile, fmt, args);
125
126 /* Log entries aren't newline terminated add one for clarity */
127 fputc('\n', logfile);
128}
129
130/* exported interface documented in utils/log.h */
132nslog_set_filter(const char *filter)
133{
134 nslog_error err;
135 nslog_filter_t *filt = NULL;
136
137 err = nslog_filter_from_text(filter, &filt);
138 if (err != NSLOG_NO_ERROR) {
139 if (err == NSLOG_NO_MEMORY)
140 return NSERROR_NOMEM;
141 else
142 return NSERROR_INVALID;
143 }
144
145 err = nslog_filter_set_active(filt, NULL);
146 filt = nslog_filter_unref(filt);
147 if (err != NSLOG_NO_ERROR) {
148 return NSERROR_NOSPACE;
149 }
150
151 return NSERROR_OK;
152}
153
154#else
155
156void
157nslog_log(const char *file, const char *func, int ln, const char *format, ...)
158{
159 va_list ap;
160
161 if (verbose_log) {
162 fprintf(logfile,
163 "%s %s:%i %s: ",
165 file,
166 ln,
167 func);
168
169 va_start(ap, format);
170
171 vfprintf(logfile, format, ap);
172
173 va_end(ap);
174
175 fputc('\n', logfile);
176 }
177}
178
179/* exported interface documented in utils/log.h */
181nslog_set_filter(const char *filter)
182{
183 (void)(filter);
184 return NSERROR_OK;
185}
186
187
188#endif
189
190nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv)
191{
192 struct utsname utsname;
193 nserror ret = NSERROR_OK;
194
195 if (((*pargc) > 1) &&
196 (argv[1][0] == '-') &&
197 (argv[1][1] == 'v') &&
198 (argv[1][2] == 0)) {
199 int argcmv;
200
201 /* verbose logging to stderr */
202 logfile = stderr;
203
204 /* remove -v from argv list */
205 for (argcmv = 2; argcmv < (*pargc); argcmv++) {
206 argv[argcmv - 1] = argv[argcmv];
207 }
208 (*pargc)--;
209
210 /* ensure we actually show logging */
211 verbose_log = true;
212 } else if (((*pargc) > 2) &&
213 (argv[1][0] == '-') &&
214 (argv[1][1] == 'V') &&
215 (argv[1][2] == 0)) {
216 int argcmv;
217
218 /* verbose logging to file */
219 logfile = fopen(argv[2], "a+");
220
221 /* remove -V and filename from argv list */
222 for (argcmv = 3; argcmv < (*pargc); argcmv++) {
223 argv[argcmv - 2] = argv[argcmv];
224 }
225 (*pargc) -= 2;
226
227 if (logfile == NULL) {
228 /* could not open log file for output */
229 ret = NSERROR_NOT_FOUND;
230 verbose_log = false;
231 } else {
232
233 /* ensure we actually show logging */
234 verbose_log = true;
235 }
236 } else {
237 /* default is logging to stderr */
238 logfile = stderr;
239 }
240
241 /* ensure output file handle is correctly configured */
242 if ((verbose_log == true) &&
243 (ensure != NULL) &&
244 (ensure(logfile) == false)) {
245 /* failed to ensure output configuration */
247 verbose_log = false;
248 }
249
250#ifdef WITH_NSLOG
251
253 NETSURF_BUILTIN_VERBOSE_FILTER :
254 NETSURF_BUILTIN_LOG_FILTER) != NSERROR_OK) {
256 verbose_log = false;
257 } else if (nslog_set_render_callback(netsurf_render_log, NULL) != NSLOG_NO_ERROR) {
259 verbose_log = false;
260 } else if (nslog_uncork() != NSLOG_NO_ERROR) {
262 verbose_log = false;
263 }
264
265#endif
266
267 /* sucessfull logging initialisation so log system info */
268 if (ret == NSERROR_OK) {
269 NSLOG(netsurf, INFO, "NetSurf version '%s'", netsurf_version);
270 if (uname(&utsname) < 0) {
271 NSLOG(netsurf, INFO,
272 "Failed to extract machine information");
273 } else {
274 NSLOG(netsurf, INFO,
275 "NetSurf on <%s>, node <%s>, release <%s>, version <%s>, machine <%s>",
281 }
282 }
283
284 return ret;
285}
286
287/* exported interface documented in utils/log.h */
290{
291 if (verbose_log)
292 return nslog_set_filter(nsoption_charp(verbose_filter));
293 else
294 return nslog_set_filter(nsoption_charp(log_filter));
295}
296
297/* exported interface documented in utils/log.h */
298void
300{
301 NSLOG(netsurf, INFO,
302 "Finalising logging, please report any further messages");
303 verbose_log = true;
304 if (logfile != stderr) {
305 fclose(logfile);
306 logfile = stderr;
307 }
308#ifdef WITH_NSLOG
309 nslog_cleanup();
310#endif
311}
STATIC char result[100]
Definition: arexx.c:77
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOSPACE
Insufficient space.
Definition: errors.h:59
@ NSERROR_NOT_FOUND
Requested item not found.
Definition: errors.h:34
@ NSERROR_INIT_FAILED
Initialisation failed.
Definition: errors.h:38
@ NSERROR_INVALID
Invalid data.
Definition: errors.h:49
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
fbtk_widget_t * fbtk
Definition: gui.c:63
const char *const netsurf_version
User friendly version string.
Definition: gui_menu.c:92
static struct llcache_s * llcache
low level cache state
Definition: llcache.c:267
nserror nslog_init(nslog_ensure_t *ensure, int *pargc, char **argv)
Initialise the logging system.
Definition: log.c:190
bool verbose_log
flag to enable verbose logging
Definition: log.c:31
static const char * nslog_gettime(void)
Obtain a formatted string suitable for prepending to a log message.
Definition: log.c:72
nserror nslog_set_filter_by_options(void)
Set the logging filter according to the options.
Definition: log.c:289
void nslog_finalise(void)
Shut down the logging system.
Definition: log.c:299
static int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
Subtract the ‘struct timeval’ values X and Y.
Definition: log.c:44
nserror nslog_set_filter(const char *filter)
Set the logging filter.
Definition: log.c:181
static FILE * logfile
The stream to which logging is sent.
Definition: log.c:34
void nslog_log(const char *file, const char *func, int ln, const char *format,...)
Definition: log.c:157
bool() nslog_ensure_t(FILE *fptr)
Ensures the FILE handle is available to write logging to.
Definition: log.h:35
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
Information for a single fetch.
Definition: fetch.c:89
system information filled in by uname derived from posix spec.
Definition: utsname.h:31
char release[65]
OS release (e.g., "2.6.28")
Definition: utsname.h:36
char nodename[65]
Name within "some implementation-defined network".
Definition: utsname.h:33
char version[65]
OS version.
Definition: utsname.h:37
char machine[65]
Hardware identifier.
Definition: utsname.h:38
char sysname[65]
Operating system name (e.g., "Linux")
Definition: utsname.h:32
BSD style timeval macros.
#define timerisset(a)
Definition: sys_time.h:38
Option reading and saving interface.
#define nsoption_charp(OPTION)
Get the value of a string option.
Definition: nsoption.h:331
int uname(struct utsname *buf)
Get the system information.
Definition: utils.c:459
Interface to uts API to get name and information about current kernel.
Version information interface.