summaryrefslogtreecommitdiff
path: root/src/core.c
blob: 978f1694be0fdf793a6220de89031a84df21401f (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * 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 Core
 */

#include "nslog_internal.h"

static bool nslog__corked = true;

static struct nslog_cork_chain {
	struct nslog_cork_chain *next;
	nslog_entry_context_t context;
	char message[0]; /* NUL terminated */
} *nslog__cork_chain = NULL, *nslog__cork_chain_last = NULL;

static nslog_callback nslog__cb = NULL;
static void *nslog__cb_ctx = NULL;

static nslog_category_t *nslog__all_categories = NULL;

const char *nslog_level_name(nslog_level level)
{
	switch (level) {
	case NSLOG_LEVEL_DEEPDEBUG:
		return "DEEPDEBUG";
	case NSLOG_LEVEL_DEBUG:
		return "DEBUG";
	case NSLOG_LEVEL_VERBOSE:
		return "VERBOSE";
	case NSLOG_LEVEL_INFO:
		return "INFO";
	case NSLOG_LEVEL_WARNING:
		return "WARNING";
	case NSLOG_LEVEL_ERROR:
		return "ERROR";
	case NSLOG_LEVEL_CRITICAL:
		return "CRITICAL";
	};

	return "**UNKNOWN**";
}


static void nslog__normalise_category(nslog_category_t *cat)
{
	if (cat->parent == NULL) {
		cat->name = strdup(cat->cat_name);
		cat->namelen = strlen(cat->name);
	} else {
		nslog__normalise_category(cat->parent);
		cat->name = malloc(strlen(cat->parent->name) + strlen(cat->cat_name) + 2);
		strcpy(cat->name, cat->parent->name);
		strcat(cat->name, "/");
		strcat(cat->name, cat->cat_name);
		cat->next = nslog__all_categories;
		cat->namelen = strlen(cat->name);
		nslog__all_categories = cat;
	}
}

static void nslog__log_corked(nslog_entry_context_t *ctx,
			      int measured_len,
			      const char *fmt,
			      va_list args)
{
	/* If corked, we need to store a copy */
	struct nslog_cork_chain *newcork = malloc(sizeof(*newcork) + measured_len + 1);
	if (newcork == NULL) {
		/* Wow, something went wrong */
		return;
	}
	newcork->context = *ctx;
	vsprintf(newcork->message, fmt, args);
	if (nslog__cork_chain == NULL) {
		nslog__cork_chain = nslog__cork_chain_last = newcork;
	} else {
		nslog__cork_chain_last->next = newcork;
		nslog__cork_chain_last = newcork;
	}
}

static void nslog__log_uncorked(nslog_entry_context_t *ctx,
				const char *fmt,
				va_list args)
{
	/* TODO: Add filtering here */
	if (nslog__cb != NULL) {
		if (ctx->category->name == NULL) {
			nslog__normalise_category(ctx->category);
		}
		if (nslog__filter_matches(ctx))
			(*nslog__cb)(nslog__cb_ctx, ctx, fmt, args);
	}
}

void nslog__log(nslog_entry_context_t *ctx,
		const char *pattern,
		...)
{
	va_list ap;
	va_start(ap, pattern);
	if (nslog__corked) {
		va_list ap2;
		va_copy(ap2, ap);
		int slen = vsnprintf(NULL, 0, pattern, ap);
		va_end(ap);
		nslog__log_corked(ctx, slen, pattern, ap2);
		va_end(ap2);
	} else {
		nslog__log_uncorked(ctx, pattern, ap);
		va_end(ap);
	}
}

nslog_error nslog_set_render_callback(nslog_callback cb, void *context)
{
	nslog__cb = cb;
	nslog__cb_ctx = context;

	return NSLOG_NO_ERROR;
}


static void __nslog__deliver_corked_entry(nslog_entry_context_t *ctx,
					  const char *fmt,
					  ...)
{
	va_list args;
	va_start(args, fmt);
	if (nslog__cb != NULL) {
		(*nslog__cb)(nslog__cb_ctx, ctx, fmt, args);
	}
	va_end(args);
}

nslog_error nslog_uncork()
{
	if (nslog__corked) {
		while (nslog__cork_chain != NULL) {
			struct nslog_cork_chain *ent = nslog__cork_chain;
			nslog__cork_chain = ent->next;
			if (ent->context.category->name == NULL) {
				nslog__normalise_category(ent->context.category);
			}
			if (nslog__filter_matches(&ent->context))
				__nslog__deliver_corked_entry(&ent->context,
							      "%s", ent->message);
			free(ent);
		}
		nslog__corked = false;
	}
	return NSLOG_NO_ERROR;
}