Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: LGPL-2.1+
0002 // Copyright (C) 2022, Linaro Ltd - Daniel Lezcano <daniel.lezcano@linaro.org>
0003 #include <stdarg.h>
0004 #include <stdio.h>
0005 #include <string.h>
0006 #include <syslog.h>
0007 #include "log.h"
0008 
0009 static const char *__ident = "unknown";
0010 static int __options;
0011 
0012 static const char * const loglvl[] = {
0013     [LOG_DEBUG] = "DEBUG",
0014     [LOG_INFO]  = "INFO",
0015     [LOG_NOTICE]    = "NOTICE",
0016     [LOG_WARNING]   = "WARN",
0017     [LOG_ERR]   = "ERROR",
0018     [LOG_CRIT]  = "CRITICAL",
0019     [LOG_ALERT] = "ALERT",
0020     [LOG_EMERG] = "EMERG",
0021 };
0022 
0023 int log_str2level(const char *lvl)
0024 {
0025     int i;
0026 
0027     for (i = 0; i < sizeof(loglvl) / sizeof(loglvl[LOG_DEBUG]); i++)
0028         if (!strcmp(lvl, loglvl[i]))
0029             return i;
0030 
0031     return LOG_DEBUG;
0032 }
0033 
0034 extern void logit(int level, const char *format, ...)
0035 {
0036     va_list args;
0037 
0038     va_start(args, format);
0039 
0040     if (__options & TO_SYSLOG)
0041         vsyslog(level, format, args);
0042 
0043     if (__options & TO_STDERR)
0044         vfprintf(stderr, format, args);
0045 
0046     if (__options & TO_STDOUT)
0047         vfprintf(stdout, format, args);
0048 
0049     va_end(args);
0050 }
0051 
0052 int log_init(int level, const char *ident, int options)
0053 {
0054     if (!options)
0055         return -1;
0056 
0057     if (level > LOG_DEBUG)
0058         return -1;
0059 
0060     if (!ident)
0061         return -1;
0062 
0063     __ident = ident;
0064     __options = options;
0065 
0066     if (options & TO_SYSLOG) {
0067         openlog(__ident, options | LOG_NDELAY, LOG_USER);
0068         setlogmask(LOG_UPTO(level));
0069     }
0070 
0071     return 0;
0072 }
0073 
0074 void log_exit(void)
0075 {
0076     closelog();
0077 }