Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syslog output + message logging #59

Merged
merged 5 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ mavlink_routerd_SOURCES = \
src/mavlink-router/main.cpp \
src/mavlink-router/mainloop.cpp \
src/mavlink-router/mainloop.h \
src/mavlink-router/message_log.cpp \
src/mavlink-router/message_log.h \
src/mavlink-router/pollable.h \
src/mavlink-router/pollable.cpp \
src/mavlink-router/timeout.h \
Expand Down Expand Up @@ -220,6 +222,8 @@ mainloop_test_SOURCES = \
src/mavlink-router/logendpoint.h \
src/mavlink-router/mainloop_test.cpp \
src/mavlink-router/mainloop.cpp \
src/mavlink-router/message_log.cpp \
src/mavlink-router/message_log.h \
src/mavlink-router/pollable.cpp \
src/mavlink-router/pollable.h \
src/mavlink-router/timeout.cpp \
Expand Down
4 changes: 2 additions & 2 deletions examples/config.sample
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
# Default: 0 (disabled)
#
# DebugLogLevel
# One of <error>, <warning>, <info> or <debug>. Which debug log
# level is being used by mavlink-router, with <debug> being the
# One of <error>, <warning>, <info>, <debug> or <trace>. Which debug log
# level is being used by mavlink-router, with <trace> being the
# most verbose.
# Default:<info>
#
Expand Down
2 changes: 1 addition & 1 deletion mavlink-router.service.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Description=MAVLink Router

[Service]
Type=simple
ExecStart=@bindir@/mavlink-routerd
ExecStart=@bindir@/mavlink-routerd --syslog
Restart=always
Nice=-20
WatchdogSec=5s
Expand Down
72 changes: 60 additions & 12 deletions src/common/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <sys/uio.h>

#define COLOR_RED "\033[31m"
Expand All @@ -34,6 +35,7 @@
Log::Level Log::_max_level = Level::INFO;
int Log::_target_fd = -1;
bool Log::_show_colors;
Log::Backend Log::_backend = Log::Backend::STDERR;

const char *Log::_get_color(Level level)
{
Expand All @@ -51,28 +53,46 @@ const char *Log::_get_color(Level level)
return COLOR_WHITE;
case Level::DEBUG:
return COLOR_LIGHTBLUE;
case Level::TRACE:
break;
}

return nullptr;
}

int Log::open()
int Log::open(Backend backend)
{
assert_or_return(_target_fd < 0, -1);

/* for now, only logging is supported only to stderr */
_target_fd = STDERR_FILENO;
_backend = backend;

if (isatty(_target_fd))
_show_colors = true;
switch (backend) {
case Backend::STDERR:
_target_fd = STDERR_FILENO;

if (isatty(_target_fd))
_show_colors = true;

break;
case Backend::SYSLOG:
openlog(nullptr, LOG_CONS, LOG_USER);
break;
}

return 0;
}

int Log::close()
{
/* see _target_fd on open() */
fflush(stderr);
switch (_backend) {
case Backend::STDERR:
/* see _target_fd on open() */
fflush(stderr);
break;
case Backend::SYSLOG:
closelog();
break;
}

return 0;
}
Expand All @@ -82,17 +102,14 @@ void Log::set_max_level(Level level)
_max_level = level;
}

void Log::logv(Level level, const char *format, va_list ap)
void Log::logv_to_fd(int fd, Log::Level level, const char *format, va_list ap)
{
struct iovec iovec[6] = { };
const char *color;
int n = 0;
char buffer[LINE_MAX];
int save_errno;

if (_max_level < level)
return;

/* so %m works as expected */
save_errno = errno;

Expand All @@ -115,7 +132,25 @@ void Log::logv(Level level, const char *format, va_list ap)

IOVEC_SET_STRING(iovec[n++], "\n");

(void)writev(_target_fd, iovec, n);
(void)writev(fd, iovec, n);
}

void Log::logv(Level level, const char *format, va_list ap)
{
if (_max_level < level)
return;

switch (_backend) {
case Backend::STDERR:
logv_to_fd(_target_fd, level, format, ap);
break;
case Backend::SYSLOG:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
vsyslog(syslog_log_level(level), format, ap);
#pragma GCC diagnostic pop
break;
}
}

void Log::log(Level level, const char *format, ...)
Expand All @@ -126,3 +161,16 @@ void Log::log(Level level, const char *format, ...)
logv(level, format, ap);
va_end(ap);
}

int Log::syslog_log_level(Log::Level level)
{
switch (level) {
case Level::ERROR: return LOG_ERR;
case Level::WARNING: return LOG_WARNING;
case Level::NOTICE: return LOG_NOTICE;
case Level::INFO: return LOG_INFO;
case Level::DEBUG: return LOG_DEBUG;
case Level::TRACE: return LOG_DEBUG;
}
return LOG_DEBUG;
}
13 changes: 12 additions & 1 deletion src/common/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ class Log {
NOTICE,
INFO,
DEBUG,
TRACE,
};

static int open();
enum class Backend {
STDERR,
SYSLOG,
};

static int open(Backend backend);
static int close();

static Level get_max_level() _pure_ { return _max_level; }
Expand All @@ -43,13 +49,18 @@ class Log {
static void log(Level level, const char *format, ...) _printf_format_(2, 3);

protected:
static void logv_to_fd(int fd, Level level, const char *format, va_list ap);
static const char *_get_color(Level level);

static int syslog_log_level(Level level);

static int _target_fd;
static Level _max_level;
static bool _show_colors;
static Backend _backend;
};

#define log_trace(...) Log::log(Log::Level::TRACE, __VA_ARGS__)
#define log_debug(...) Log::log(Log::Level::DEBUG, __VA_ARGS__)
#define log_info(...) Log::log(Log::Level::INFO, __VA_ARGS__)
#define log_notice(...) Log::log(Log::Level::NOTICE, __VA_ARGS__)
Expand Down
85 changes: 0 additions & 85 deletions src/mavlink-router/auterion_ulog_meta_writer.h

This file was deleted.

Loading