Skip to content

Commit

Permalink
log: allow setting loglevel
Browse files Browse the repository at this point in the history
Signed-off-by: David Bauer <mail@david-bauer.net>
  • Loading branch information
blocktrron committed Apr 4, 2024
1 parent f6c595a commit d426f05
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
6 changes: 5 additions & 1 deletion openwrt/node-whisperer/files/node-whisperer.init
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ load_config() {
ubus -t 10 wait_for node_whisperer

config_load node-whisperer
config_foreach uci_nw node-whisperer
config_foreach uci_nw settings
}

reload_service() {
Expand All @@ -50,10 +50,14 @@ start_service()
{
ENABLED="$(uci -q get node-whisperer.settings.enabled)"
ENABLED="${ENABLED:-1}"
LOG_LEVEL="$(uci -q get node-whisperer.settings.log_level)"
LOG_LEVEL="${LOG_LEVEL:-3}"

[ "$ENABLED" -gt 0 ] || return

procd_open_instance
procd_set_param command "$PROG"
procd_append_param command -l "$LOG_LEVEL"
procd_append_param command -s
procd_close_instance
}
3 changes: 2 additions & 1 deletion openwrt/node-whisperer/files/node-whisperer.uci
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

config node-whisperer 'core'
config settings 'settings'
option enabled '1'
option log_level '3'
list information 'node_id'
list information 'hostname'
list information 'uptime'
Expand Down
16 changes: 16 additions & 0 deletions src/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ static int start_daemon() {
}

int main(int argc, char *argv[]) {
int opt;

while ((opt = getopt(argc, argv, "l:s")) != -1) {
switch (opt) {
case 'l':
log_set_level(atoi(optarg));
break;
case 's':
log_use_syslog(1);
break;
default:
fprintf(stderr, "Usage: %s [-l loglevel] [-s]\n", argv[0]);
exit(EXIT_FAILURE);
}
}

start_daemon();
return 0;
}
33 changes: 25 additions & 8 deletions src/log.c
Original file line number Diff line number Diff line change
@@ -1,56 +1,73 @@
#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include "log.h"

static enum log_level log_level = LOG_INFO;
static enum log_level log_level = LL_INFO;
static int use_syslog = 0;

static void log_vprintf(enum log_level level, const char *fmt, va_list args) {
const char *level_str;

if (level < log_level) {
if (level > log_level) {
return;
}

switch (level) {
case LOG_DEBUG:
case LL_DEBUG:
level_str = "DEBUG";
break;
case LOG_INFO:
case LL_INFO:
level_str = "INFO";
break;
case LOG_ERROR:
case LL_WARNING:
level_str = "WARNING";
break;
case LL_ERROR:
level_str = "ERROR";
break;
case LL_FATAL:
level_str = "FATAL";
break;
default:
level_str = "UNKNOWN";
break;
}
fprintf(stderr, "[%s] ", level_str);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");

if (use_syslog) {
vsyslog(LOG_INFO, fmt, args);
}
}

void log_set_level(enum log_level level) {
log_level = level;
}

void log_use_syslog(int use) {
use_syslog = use;
openlog("node-whisperer", 0, LOG_USER);
}

void log_error(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
log_vprintf(LOG_ERROR, fmt, args);
log_vprintf(LL_ERROR, fmt, args);
va_end(args);
}

void log_info(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
log_vprintf(LOG_INFO, fmt, args);
log_vprintf(LL_INFO, fmt, args);
va_end(args);
}

void log_debug(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
log_vprintf(LOG_DEBUG, fmt, args);
log_vprintf(LL_DEBUG, fmt, args);
va_end(args);
}
11 changes: 6 additions & 5 deletions src/log.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once

enum log_level {
LOG_DEBUG,
LOG_INFO,
LOG_WARNING,
LOG_ERROR,
LOG_FATAL
LL_DEBUG = 5,
LL_INFO = 4,
LL_WARNING = 3,
LL_ERROR = 2,
LL_FATAL = 1,
};

void log_set_level(enum log_level level);
void log_use_syslog(int use);

void log_error(const char *fmt, ...);
void log_info(const char *fmt, ...);
Expand Down

0 comments on commit d426f05

Please sign in to comment.