summaryrefslogtreecommitdiff
path: root/include/nslog/nslog.h
blob: ffa87bdb3c76b3956f181cf998727579bbc1ee67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright 2017 Daniel Silverstone <dsilvers@netsurf-browser.org>
 *
 * This file is part of libnslog.
 *
 * Licensed under the MIT License,
 *                http://www.opensource.org/licenses/mit-license.php
 */

/**
 * \file
 * NetSurf Logging
 */

#ifndef NSLOG_NSLOG_H_
#define NSLOG_NSLOG_H_

#include <stdarg.h>

typedef enum {
        NSLOG_LEVEL_DEEPDEBUG = 0,
        NSLOG_LEVEL_DEBUG = 1,
        NSLOG_LEVEL_VERBOSE = 2,
        NSLOG_LEVEL_INFO = 3,
        NSLOG_LEVEL_WARNING = 4,
        NSLOG_LEVEL_ERROR = 5,
        NSLOG_LEVEL_CRITICAL = 6,
} nslog_level;

const char *nslog_level_name(nslog_level level);

#define NSLOG_LEVEL_DD          NSLOG_LEVEL_DEEPDEBUG
#define NSLOG_LEVEL_CHAT        NSLOG_LEVEL_VERBOSE
#define NSLOG_LEVEL_WARN        NSLOG_LEVEL_WARNING
#define NSLOG_LEVEL_ERR         NSLOG_LEVEL_ERROR
#define NSLOG_LEVEL_CRIT        NSLOG_LEVEL_CRITICAL

#ifndef NSLOG_COMPILED_MIN_LEVEL
#define NSLOG_COMPILED_MIN_LEVEL NSLOG_LEVEL_DEBUG
#endif

typedef struct nslog_category_s {
        const char *cat_name;
        const char *description;
        struct nslog_category_s *parent;
        char *name;
        struct nslog_category_s *next;
} nslog_category_t;

typedef struct nslog_entry_context_s {
        nslog_category_t *category;
        nslog_level level;
        const char *filename;
	const char *funcname;
        int lineno;
} nslog_entry_context_t;

#define NSLOG_DECLARE_CATEGORY(catname)				\
	extern nslog_category_t __nslog_category_##catname

#define NSLOG_DEFINE_CATEGORY(catname, description)	\
	nslog_category_t __nslog_category_##catname = { \
		#catname,				\
		description,				\
		NULL,					\
		NULL,					\
		NULL,					\
	}

#define NSLOG_DEFINE_SUBCATEGORY(parentcatname, catname, description)	\
	nslog_category_t __nslog_category_##catname = {			\
		#catname,						\
		description,						\
		&__nslog_category_##parentcatname,			\
		NULL,							\
		NULL,							\
	}

#define NSLOG(catname, level, logmsg, args...)				\
	do {								\
		if (NSLOG_LEVEL_##level >= NSLOG_COMPILED_MIN_LEVEL) {	\
			nslog_entry_context_t ctx = {			\
				&__nslog_category_##catname,		\
				NSLOG_LEVEL_##level,			\
				__FILE__,				\
				__PRETTY_FUNCTION__,			\
				__LINE__,				\
			};						\
			nslog__log(&ctx, logmsg, ##args);		\
		}							\
	} while(0)

void nslog__log(nslog_entry_context_t *ctx,
		const char *pattern,
		...) __attribute__ ((format (printf, 2, 3)));

typedef enum {
	NSLOG_NO_ERROR = 0,
	NSLOG_NO_MEMORY = 1,
} nslog_error;

typedef void (*nslog_callback)(void *context, nslog_entry_context_t *ctx,
			       const char *fmt, va_list args);

nslog_error nslog_set_render_callback(nslog_callback cb, void *context);

nslog_error nslog_uncork(void);

#endif /* NSLOG_NSLOG_H_ */