Skip to content

Commit

Permalink
API to configure syslog server address at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanseifert committed Sep 5, 2024
1 parent f24ccd7 commit 2b611de
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
3 changes: 3 additions & 0 deletions include/zephyr/logging/log_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/util.h>
#include <zephyr/logging/log_output.h>
#include <zephyr/net/net_pkt.h>

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -297,6 +298,8 @@ static inline int log_backend_format_set(const struct log_backend *backend, uint
return backend->api->format_set(backend, log_type);
}

int log_backend_net_set_server(struct sockaddr *newAddr);

/**
* @}
*/
Expand Down
45 changes: 38 additions & 7 deletions subsys/logging/log_backend_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ struct sockaddr server_addr;
static bool panic_mode;
static uint32_t log_format_current = CONFIG_LOG_BACKEND_NET_OUTPUT_DEFAULT;

// flag set when the log server has been overridden
static bool gAddressOverride = false;

const struct log_backend *log_backend_net_get(void);

NET_PKT_SLAB_DEFINE(syslog_tx_pkts, CONFIG_LOG_BACKEND_NET_MAX_BUF);
Expand Down Expand Up @@ -201,14 +204,17 @@ static void init_net(struct log_backend const *const backend)
ARG_UNUSED(backend);
int ret;

net_sin(&server_addr)->sin_port = htons(514);
// only apply from config if never set
if(!gAddressOverride) {
net_sin(&server_addr)->sin_port = htons(514);

ret = net_ipaddr_parse(CONFIG_LOG_BACKEND_NET_SERVER,
sizeof(CONFIG_LOG_BACKEND_NET_SERVER) - 1,
&server_addr);
if (ret == 0) {
LOG_ERR("Cannot configure syslog server address");
return;
ret = net_ipaddr_parse(CONFIG_LOG_BACKEND_NET_SERVER,
sizeof(CONFIG_LOG_BACKEND_NET_SERVER) - 1,
&server_addr);
if (ret == 0) {
LOG_ERR("Cannot configure syslog server address");
return;
}
}

log_backend_deactivate(log_backend_net_get());
Expand Down Expand Up @@ -236,3 +242,28 @@ const struct log_backend *log_backend_net_get(void)
{
return &log_backend_net;
}

/**
* @brief Set IP address of remote syslog server
*
* This must be called before the log backend is initialized.
*
* If not called, the default specified via config (CONFIG_LOG_BACKEND_NET_SERVER) is used.
*/
int log_backend_net_set_server(struct sockaddr *newAddr) {
if(!newAddr) {
return -EFAULT;
}

// musn't be initialized yet
if(net_init_done) {
// TODO: we could probably force re-initializing the socket
return -EINVAL;
}

// set it
server_addr = *newAddr;
gAddressOverride = true;

return 0;
}

0 comments on commit 2b611de

Please sign in to comment.