File: logger.c

package info (click to toggle)
uxplay 1.72.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,196 kB
  • sloc: ansic: 22,822; cpp: 2,342; makefile: 7
file content (155 lines) | stat: -rw-r--r-- 3,317 bytes parent folder | download | duplicates (3)
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
/**
 *  Copyright (C) 2011-2012  Juho Vähä-Herttua
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *===============================================================
 * modified by fduncanh 2023
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>

#include "logger.h"
#include "compat.h"

struct logger_s {
	mutex_handle_t lvl_mutex;
	mutex_handle_t cb_mutex;

	int level;
	void *cls;
	logger_callback_t callback;
};

logger_t *
logger_init()
{
	logger_t *logger = calloc(1, sizeof(logger_t));
	assert(logger);

	MUTEX_CREATE(logger->lvl_mutex);
	MUTEX_CREATE(logger->cb_mutex);

	logger->level = LOGGER_WARNING;
	logger->callback = NULL;
	return logger;
}

void
logger_destroy(logger_t *logger)
{
	MUTEX_DESTROY(logger->lvl_mutex);
	MUTEX_DESTROY(logger->cb_mutex);
	free(logger);
}

void
logger_set_level(logger_t *logger, int level)
{
	assert(logger);

	MUTEX_LOCK(logger->lvl_mutex);
	logger->level = level;
	MUTEX_UNLOCK(logger->lvl_mutex);
}

int
logger_get_level(logger_t *logger)
{
        int level;   
        assert(logger);

	MUTEX_LOCK(logger->lvl_mutex);
	level = logger->level;
	MUTEX_UNLOCK(logger->lvl_mutex);

        return level;
}

void
logger_set_callback(logger_t *logger, logger_callback_t callback, void *cls)
{
	assert(logger);

	MUTEX_LOCK(logger->cb_mutex);
	logger->cls = cls;
	logger->callback = callback;
	MUTEX_UNLOCK(logger->cb_mutex);
}

static char *
logger_utf8_to_local(const char *str)
{
	char *ret = NULL;

/* FIXME: This is only implemented on Windows for now */
#if defined(_WIN32) || defined(_WIN64)
	int wclen, mblen;
	WCHAR *wcstr;
	BOOL failed;

	wclen = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
	wcstr = malloc(sizeof(WCHAR) * wclen);
	MultiByteToWideChar(CP_UTF8, 0, str, -1, wcstr, wclen);

	mblen = WideCharToMultiByte(CP_ACP, 0, wcstr, wclen, NULL, 0, NULL, &failed);
	if (failed) {
		/* Invalid characters in input, conversion failed */
		free(wcstr);
		return NULL;
	}

	ret = malloc(sizeof(CHAR) * mblen);
	WideCharToMultiByte(CP_ACP, 0, wcstr, wclen, ret, mblen, NULL, NULL);
	free(wcstr);
#endif

	return ret;
}

void
logger_log(logger_t *logger, int level, const char *fmt, ...)
{
	char buffer[4096];
	va_list ap;

	MUTEX_LOCK(logger->lvl_mutex);
	if (level > logger->level) {
		MUTEX_UNLOCK(logger->lvl_mutex);
		return;
	}
	MUTEX_UNLOCK(logger->lvl_mutex);

	buffer[sizeof(buffer)-1] = '\0';
	va_start(ap, fmt);
	vsnprintf(buffer, sizeof(buffer)-1, fmt, ap);
	va_end(ap);

	MUTEX_LOCK(logger->cb_mutex);
	if (logger->callback) {
		logger->callback(logger->cls, level, buffer);
		MUTEX_UNLOCK(logger->cb_mutex);
	} else {
		char *local;
		MUTEX_UNLOCK(logger->cb_mutex);
		local = logger_utf8_to_local(buffer);
		if (local) {
			fprintf(stderr, "%s\n", local);
			free(local);
		} else {
			fprintf(stderr, "%s\n", buffer);
		}
	}
}