summaryrefslogtreecommitdiff
path: root/src/core.c
blob: 0665195439873c3f359fc0bf93535909ab4bf491 (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
/*
 * 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/nslog.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

static bool nslog__corked = true;

static struct nslog_cork_chain {
	struct nslog_cork_chain *next;
	nslog_entry_t *entry;
} *nslog__cork_chain = 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);
	} 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;
		nslog__all_categories = cat;
	}
}

static void nslog__deliver(nslog_entry_t *entry)
{
	/* TODO: Add filtering here */
	if (nslog__cb != NULL) {
		if (entry->category->name == NULL) {
			nslog__normalise_category(entry->category);
		}
		(*nslog__cb)(nslog__cb_ctx, entry);
	}
}

void nslog__log(nslog_category_t *category,
		nslog_level level,
		const char *filename,
		int lineno,
		const char *funcname,
		const char *pattern,
		...)
{
	va_list ap;
	va_start(ap, pattern);
	va_list ap2;
	va_copy(ap2, ap);
	int slen = vsnprintf(NULL, 0, pattern, ap);
	va_end(ap);
	nslog_entry_t *entry = malloc(sizeof(nslog_entry_t) + slen + 1);
	if (entry == NULL) {
		/* We're at ENOMEM! log entry is lost */
		va_end(ap2);
		return;
	}
	entry->category = category;
	entry->level = level;
	entry->filename = filename;
	entry->funcname = funcname;
	entry->lineno = lineno;
	vsprintf(entry->message, pattern, ap2);
	va_end(ap2);
	if (nslog__corked) {
		struct nslog_cork_chain *chained = malloc(sizeof(struct nslog_cork_chain));
		if (chained == NULL) {
			/* ENOMEM during corked operation! wow */
			free(entry);
			return;
		}
		chained->next = nslog__cork_chain;
		chained->entry = entry;
		nslog__cork_chain = chained;
	} else {
		/* Not corked */
		nslog__deliver(entry);
		free(entry);
	}
}

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

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;
			nslog__deliver(ent->entry);
			free(ent->entry);
			free(ent);
		}
		nslog__corked = false;
	}
	return NSLOG_NO_ERROR;
}