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 6a04bb9
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
4 changes: 3 additions & 1 deletion openwrt/node-whisperer/files/node-whisperer.init
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ 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_set_param command "$PROG -s -l $LOG_LEVEL"
procd_close_instance
}
1 change: 1 addition & 0 deletions 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'
option enabled '1'
option loglevel '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;
}
14 changes: 12 additions & 2 deletions src/log.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#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;
}

Expand All @@ -28,12 +30,20 @@ static void log_vprintf(enum log_level level, const char *fmt, va_list args) {
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;
}

void log_error(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
Expand Down
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,
LOG_ERROR = 2,
LOG_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 6a04bb9

Please sign in to comment.