Set correct syslog() log level based on debug flag

Also, refactor logging to /dev/kmsg to separate function.

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2017-07-01 22:23:53 +02:00
1 parent 9a4f46dba4
commit ec52a6a240
2 files changed
+27 -20

No files matched your search

+23 -16
View File
@@ -29,6 +29,7 @@
#include "finit.h" #include "finit.h"
#include "log.h" #include "log.h"
static int loglevel = LOG_NOTICE;
void log_toggle_debug(void) void log_toggle_debug(void)
{ {
@@ -37,20 +38,12 @@ void log_toggle_debug(void)
silent = 0; silent = 0;
else else
silent = quiet ? 1 : SILENT_MODE; silent = quiet ? 1 : SILENT_MODE;
logit(LOG_INFO, "Debug mode %s", debug ? "enabled" : "disabled");
logit(LOG_NOTICE, "Debug mode %s", debug ? "enabled" : "disabled");
} }
/* static void early_logit(int prio, const char *fmt, va_list ap)
* Log to /dev/kmsg until syslogd has started, then openlog()
* and continue logging as a regular daemon.
*/
void logit(int prio, const char *fmt, ...)
{ {
va_list ap;
static int _slup = 0;
va_start(ap, fmt);
if (!_slup && !fexist("/dev/log")) {
FILE *fp; FILE *fp;
fp = fopen("/dev/kmsg", "w"); fp = fopen("/dev/kmsg", "w");
@@ -62,20 +55,34 @@ void logit(int prio, const char *fmt, ...)
vfprintf(fp, fmt, ap); vfprintf(fp, fmt, ap);
fclose(fp); fclose(fp);
} else { } else {
fprintf(stderr, "Failed opening /dev/kmsg for appending ...\n");
if (debug || prio <= LOG_ERR) if (debug || prio <= LOG_ERR)
vfprintf(stderr, fmt, ap); vfprintf(stderr, fmt, ap);
} }
va_end(ap);
return;
} }
if (!_slup) { /*
* Log to /dev/kmsg until syslogd has started, then openlog()
* and continue logging as a regular daemon.
*/
void logit(int prio, const char *fmt, ...)
{
va_list ap;
static int up = 0;
va_start(ap, fmt);
if (!up) {
if (!fexist("/dev/log")) {
early_logit(prio, fmt, ap);
goto done;
}
openlog("finit", LOG_PID, LOG_DAEMON); openlog("finit", LOG_PID, LOG_DAEMON);
_slup = 1; setlogmask(LOG_UPTO(loglevel));
up = 1;
} }
vsyslog(prio, fmt, ap); vsyslog(prio, fmt, ap);
done:
va_end(ap); va_end(ap);
} }
+4 -4
View File
@@ -35,10 +35,10 @@
* The default log level is LOG_NOTICE. To toggle LOG_DEBUG messages, * The default log level is LOG_NOTICE. To toggle LOG_DEBUG messages,
* use `initctl debug` or add `--debug` to the kernel cmdline. * use `initctl debug` or add `--debug` to the kernel cmdline.
*/ */
#define _d(fmt, args...) logit(LOG_DEBUG, "%s():" fmt, __func__, ##args) #define _d(fmt, args...) logit(LOG_DEBUG, "%s():" fmt "\n", __func__, ##args)
#define _w(fmt, args...) logit(LOG_WARNING, "%s():" fmt, __func__, ##args) #define _w(fmt, args...) logit(LOG_WARNING, "%s():" fmt "\n", __func__, ##args)
#define _e(fmt, args...) logit(LOG_ERR, "%s():" fmt, __func__, ##args) #define _e(fmt, args...) logit(LOG_ERR, "%s():" fmt "\n", __func__, ##args)
#define _pe(fmt, args...) logit(LOG_ERR, "%s():" fmt ": %m", __func__, ##args) #define _pe(fmt, args...) logit(LOG_ERR, "%s():" fmt ": %m\n", __func__, ##args)
void log_toggle_debug(void); void log_toggle_debug(void);
void logit (int prio, const char *fmt, ...); void logit (int prio, const char *fmt, ...);