Skip to content

Commit

Permalink
main: add flag to redirect to syslog
Browse files Browse the repository at this point in the history
Relying on environment variables to determine whether logs should be
redirected to syslog/journald is unreliable.

For example, desktop environments may be started as systemd transient
services with systemd-cat and INVOCATION_ID may be present in the
environment variables without the user/developer having any control over
it.

Instead of trying to guess in which context grout is executed, add a new
--syslog flag that will explicitly redirect logs to syslog.

Signed-off-by: Robin Jarry <[email protected]>
  • Loading branch information
rjarry committed Feb 7, 2025
1 parent 55baece commit 5c1b89a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
5 changes: 5 additions & 0 deletions docs/grout.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Grout is a software router based on DPDK __rte_graph__.
[**-L** _TYPE_:_LEVEL_]
[**-M** _MODE_]
[**-p**]
[**-S**]
[**-s** _PATH_]
[**-t**]
[**-T** _REGEXP_]
Expand Down Expand Up @@ -80,6 +81,10 @@ Default mode is _overwrite_ and parameter must be specified once only.

Disable automatic micro-sleep.

#### **-S**, **--syslog**

Redirect logs to syslog.

#### **-s**, **--socket** _PATH_

Path the control plane API socket.
Expand Down
8 changes: 2 additions & 6 deletions main/dpdk.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@

int gr_rte_log_type;
static FILE *log_stream;
static bool log_syslog;

static ssize_t log_write(void * /*cookie*/, const char *buf, size_t size) {
ssize_t n;
if (log_syslog) {
if (gr_args()->log_syslog) {
// Syslog error levels are from 0 to 7, so subtract 1 to convert.
syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
n = size;
Expand Down Expand Up @@ -70,11 +69,8 @@ static ssize_t log_write(void * /*cookie*/, const char *buf, size_t size) {
int dpdk_log_init(const struct gr_args *args) {
cookie_io_functions_t log_functions = {.write = log_write};

if (getenv("INVOCATION_ID")) {
// executed by systemd
log_syslog = true;
if (args->log_syslog)
openlog("grout", LOG_PID | LOG_ODELAY, LOG_DAEMON);
}

gr_rte_log_type = rte_log_register_type_and_pick_level("grout", RTE_LOG_NOTICE);
if (gr_rte_log_type < 0)
Expand Down
1 change: 1 addition & 0 deletions main/gr.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ struct gr_args {
unsigned log_level;
bool test_mode;
bool poll_mode;
bool log_syslog;
char **eal_extra_args;
};

Expand Down
2 changes: 1 addition & 1 deletion main/grout.default
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# vim: ft=sh

# Add grout command line flags here and restart grout.service.
GROUT_OPTS=''
GROUT_OPTS='--syslog'
7 changes: 6 additions & 1 deletion main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ static void usage(const char *prog) {
puts(" -h, --help Display this help message and exit.");
puts(" -L, --log-level <type>:<lvl> Specify log level for a specific component.");
puts(" -p, --poll-mode Disable automatic micro-sleep.");
puts(" -S, --syslog Redirect logs to syslog.");
puts(" -s, --socket <path> Path the control plane API socket.");
puts(" Default: GROUT_SOCK_PATH from env or");
printf(" %s).\n", GR_DEFAULT_SOCK_PATH);
Expand All @@ -61,11 +62,12 @@ const struct gr_args *gr_args(void) {
static int parse_args(int argc, char **argv) {
int c;

#define FLAGS ":B:D:L:M:T:Vhps:tvx"
#define FLAGS ":B:D:L:M:T:VhpSs:tvx"
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"log-level", required_argument, NULL, 'L'},
{"poll-mode", no_argument, NULL, 'p'},
{"syslog", no_argument, NULL, 'S'},
{"socket", required_argument, NULL, 's'},
{"test-mode", no_argument, NULL, 't'},
{"trace", required_argument, NULL, 'T'},
Expand Down Expand Up @@ -98,6 +100,9 @@ static int parse_args(int argc, char **argv) {
case 'p':
args.poll_mode = true;
break;
case 'S':
args.log_syslog = true;
break;
case 's':
args.api_sock_path = optarg;
break;
Expand Down

0 comments on commit 5c1b89a

Please sign in to comment.