~q3cpma/mus

ref: cf29c2237d0c99e5240a55895c07564ef4635392 mus/mus_player/log.c -rw-r--r-- 1.8 KiB
cf29c223q3cpma Add a #pragma once to the new xoshiro128plus.h header 1 year, 7 months ago
                                                                                
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
#include <stdarg.h>
#include <stdio.h>
#include <time.h>

#include "log.h"
#include "misc.h"


static int log_mask = ~0;
static char *log_ident;
static Log_type log_type;
static char strfmt_buf[256];


static int log_stderr_setmask(int mask)
{
	int old = log_mask;
	if (mask)
		log_mask = mask;
	return old;
}

static void log_stderr(int prio, const char *fmt, ...)
{
	if (!(LOG_MASK(prio) & log_mask))
		return;

	char *loglevel;
	/* Can't use an array because LOG_ values aren't guaranteed to be
	 * sequential or even small by POSIX */
	switch (prio)
	{
		case LOG_ALERT:
			loglevel = "ALERT";
			break;

		case LOG_CRIT:
			loglevel = "CRIT";
			break;

		case LOG_DEBUG:
			loglevel = "DEBUG";
			break;

		case LOG_EMERG:
			loglevel = "EMERG";
			break;

		case LOG_ERR:
			loglevel = "ERR";
			break;

		case LOG_INFO:
			loglevel = "INFO";
			break;

		case LOG_NOTICE:
			loglevel = "NOTICE";
			break;

		case LOG_WARNING:
			loglevel = "WARNING";
			break;

		default:
			loglevel = "UNKNOWN";
	}

	va_list args;
	va_start(args, fmt);

	time_t now = time(NULL);
	/* Syslog-like output: "time (priority) ident: msg" */
	strftime(strfmt_buf, sizeof(strfmt_buf) - 1, "%b %d %T", localtime(&now));
	fprintf(stderr, "%s (%s) %s: ", strfmt_buf, loglevel, log_ident);
	vfprintf(stderr, fmt, args);
	fputc('\n', stderr);
	va_end(args);
}

void log_init(Log_type type, const char *ident)
{
	log_type = type;
	switch (type)
	{
		case LOG_TYPE_SYSLOG:
			openlog(ident, 0, LOG_USER);
			log_append = syslog;
			log_setmask = setlogmask;
			break;

		case LOG_TYPE_STDERR:
		    setvbuf(stderr, NULL, _IOLBF, 0);
			log_ident = xstrdup(ident);
			log_append = log_stderr;
			log_setmask = log_stderr_setmask;
	}
}

void log_free(void)
{
	if (log_type == LOG_TYPE_SYSLOG)
		closelog();
	if (log_type == LOG_TYPE_STDERR)
		free(log_ident);
}