xref: /openbmc/linux/tools/thermal/lib/log.c (revision 3b7c5e8a)
1*3b7c5e8aSDaniel Lezcano // SPDX-License-Identifier: LGPL-2.1+
2*3b7c5e8aSDaniel Lezcano // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
3*3b7c5e8aSDaniel Lezcano #include <stdarg.h>
4*3b7c5e8aSDaniel Lezcano #include <stdio.h>
5*3b7c5e8aSDaniel Lezcano #include <string.h>
6*3b7c5e8aSDaniel Lezcano #include <syslog.h>
7*3b7c5e8aSDaniel Lezcano #include "log.h"
8*3b7c5e8aSDaniel Lezcano 
9*3b7c5e8aSDaniel Lezcano static const char *__ident = "unknown";
10*3b7c5e8aSDaniel Lezcano static int __options;
11*3b7c5e8aSDaniel Lezcano 
12*3b7c5e8aSDaniel Lezcano static const char * const loglvl[] = {
13*3b7c5e8aSDaniel Lezcano 	[LOG_DEBUG]	= "DEBUG",
14*3b7c5e8aSDaniel Lezcano 	[LOG_INFO]	= "INFO",
15*3b7c5e8aSDaniel Lezcano 	[LOG_NOTICE]	= "NOTICE",
16*3b7c5e8aSDaniel Lezcano 	[LOG_WARNING]	= "WARN",
17*3b7c5e8aSDaniel Lezcano 	[LOG_ERR]	= "ERROR",
18*3b7c5e8aSDaniel Lezcano 	[LOG_CRIT]	= "CRITICAL",
19*3b7c5e8aSDaniel Lezcano 	[LOG_ALERT]	= "ALERT",
20*3b7c5e8aSDaniel Lezcano 	[LOG_EMERG]	= "EMERG",
21*3b7c5e8aSDaniel Lezcano };
22*3b7c5e8aSDaniel Lezcano 
log_str2level(const char * lvl)23*3b7c5e8aSDaniel Lezcano int log_str2level(const char *lvl)
24*3b7c5e8aSDaniel Lezcano {
25*3b7c5e8aSDaniel Lezcano 	int i;
26*3b7c5e8aSDaniel Lezcano 
27*3b7c5e8aSDaniel Lezcano 	for (i = 0; i < sizeof(loglvl) / sizeof(loglvl[LOG_DEBUG]); i++)
28*3b7c5e8aSDaniel Lezcano 		if (!strcmp(lvl, loglvl[i]))
29*3b7c5e8aSDaniel Lezcano 			return i;
30*3b7c5e8aSDaniel Lezcano 
31*3b7c5e8aSDaniel Lezcano 	return LOG_DEBUG;
32*3b7c5e8aSDaniel Lezcano }
33*3b7c5e8aSDaniel Lezcano 
logit(int level,const char * format,...)34*3b7c5e8aSDaniel Lezcano extern void logit(int level, const char *format, ...)
35*3b7c5e8aSDaniel Lezcano {
36*3b7c5e8aSDaniel Lezcano 	va_list args;
37*3b7c5e8aSDaniel Lezcano 
38*3b7c5e8aSDaniel Lezcano 	va_start(args, format);
39*3b7c5e8aSDaniel Lezcano 
40*3b7c5e8aSDaniel Lezcano 	if (__options & TO_SYSLOG)
41*3b7c5e8aSDaniel Lezcano 		vsyslog(level, format, args);
42*3b7c5e8aSDaniel Lezcano 
43*3b7c5e8aSDaniel Lezcano 	if (__options & TO_STDERR)
44*3b7c5e8aSDaniel Lezcano 		vfprintf(stderr, format, args);
45*3b7c5e8aSDaniel Lezcano 
46*3b7c5e8aSDaniel Lezcano 	if (__options & TO_STDOUT)
47*3b7c5e8aSDaniel Lezcano 		vfprintf(stdout, format, args);
48*3b7c5e8aSDaniel Lezcano 
49*3b7c5e8aSDaniel Lezcano 	va_end(args);
50*3b7c5e8aSDaniel Lezcano }
51*3b7c5e8aSDaniel Lezcano 
log_init(int level,const char * ident,int options)52*3b7c5e8aSDaniel Lezcano int log_init(int level, const char *ident, int options)
53*3b7c5e8aSDaniel Lezcano {
54*3b7c5e8aSDaniel Lezcano 	if (!options)
55*3b7c5e8aSDaniel Lezcano 		return -1;
56*3b7c5e8aSDaniel Lezcano 
57*3b7c5e8aSDaniel Lezcano 	if (level > LOG_DEBUG)
58*3b7c5e8aSDaniel Lezcano 		return -1;
59*3b7c5e8aSDaniel Lezcano 
60*3b7c5e8aSDaniel Lezcano 	if (!ident)
61*3b7c5e8aSDaniel Lezcano 		return -1;
62*3b7c5e8aSDaniel Lezcano 
63*3b7c5e8aSDaniel Lezcano 	__ident = ident;
64*3b7c5e8aSDaniel Lezcano 	__options = options;
65*3b7c5e8aSDaniel Lezcano 
66*3b7c5e8aSDaniel Lezcano 	if (options & TO_SYSLOG) {
67*3b7c5e8aSDaniel Lezcano 		openlog(__ident, options | LOG_NDELAY, LOG_USER);
68*3b7c5e8aSDaniel Lezcano 		setlogmask(LOG_UPTO(level));
69*3b7c5e8aSDaniel Lezcano 	}
70*3b7c5e8aSDaniel Lezcano 
71*3b7c5e8aSDaniel Lezcano 	return 0;
72*3b7c5e8aSDaniel Lezcano }
73*3b7c5e8aSDaniel Lezcano 
log_exit(void)74*3b7c5e8aSDaniel Lezcano void log_exit(void)
75*3b7c5e8aSDaniel Lezcano {
76*3b7c5e8aSDaniel Lezcano 	closelog();
77*3b7c5e8aSDaniel Lezcano }
78