logging: runtime setting of syslog server ip address
Added functionality to change the syslog server's ip address at runtime as well as sample for syslog network backend Signed-off-by: David Corbeil <david.corbeil@dynon.com>
This commit is contained in:
parent
f51575212b
commit
d732a8678b
34
include/zephyr/logging/log_backend_net.h
Normal file
34
include/zephyr/logging/log_backend_net.h
Normal file
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (c) 2023 David Corbeil
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#ifndef ZEPHYR_LOG_BACKEND_NET_H_
|
||||
#define ZEPHYR_LOG_BACKEND_NET_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Allows user to set a server IP address at runtime
|
||||
*
|
||||
* @details This function allows the user to set an IPv4 or IPv6 address at runtime. It can be
|
||||
* called either before or after the backend has been initialized. If it gets called when
|
||||
* the net logger backend context is running, it'll release it and create another one with
|
||||
* the new address next time process() gets called.
|
||||
*
|
||||
* @param addr String that contains the IP address.
|
||||
*
|
||||
* @return True if parsing could be done, false otherwise.
|
||||
*/
|
||||
bool log_backend_net_set_addr(const char *addr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* ZEPHYR_LOG_BACKEND_NET_H_ */
|
|
@ -12,4 +12,20 @@ config NET_SAMPLE_SEND_ITERATIONS
|
|||
Send sample data this many times before exiting. A value of
|
||||
zero means that the defaults in the application are used.
|
||||
|
||||
|
||||
config NET_SAMPLE_SERVER_RUNTIME
|
||||
string "Syslog server IP address set at runtime"
|
||||
help
|
||||
Server address for the syslog server.
|
||||
This server address gets set at rumtime by the sample
|
||||
app defore the backend is initialized. This can be
|
||||
either IPv4 or IPv6 address. Server listen UDP port
|
||||
number can be configured here too.
|
||||
Following syntax is supported:
|
||||
192.0.2.1:514
|
||||
192.0.2.42
|
||||
[2001:db8::1]:514
|
||||
[2001:db8::2]
|
||||
2001:db::42
|
||||
|
||||
source "Kconfig.zephyr"
|
||||
|
|
3
samples/net/syslog_net/overlay-runtime-srv-addr.conf
Normal file
3
samples/net/syslog_net/overlay-runtime-srv-addr.conf
Normal file
|
@ -0,0 +1,3 @@
|
|||
CONFIG_LOG_BACKEND_NET_SERVER=""
|
||||
CONFIG_LOG_BACKEND_NET_AUTOSTART=n
|
||||
CONFIG_NET_SAMPLE_SERVER_RUNTIME="192.0.2.2:514"
|
|
@ -31,3 +31,9 @@ tests:
|
|||
filter: TOOLCHAIN_HAS_NEWLIB == 1
|
||||
extra_configs:
|
||||
- CONFIG_LOG_BACKEND_NET_AUTOSTART=n
|
||||
sample.net.syslog.runtime_srv_addr:
|
||||
filter: TOOLCHAIN_HAS_NEWLIB == 1
|
||||
extra_configs:
|
||||
- CONFIG_LOG_BACKEND_NET_AUTOSTART=n
|
||||
- CONFIG_LOG_BACKEND_NET_SERVER=""
|
||||
- CONFIG_NET_SAMPLE_SERVER_RUNTIME="192.0.2.2:514"
|
||||
|
|
|
@ -10,6 +10,7 @@ LOG_MODULE_REGISTER(net_syslog, LOG_LEVEL_DBG);
|
|||
#include <zephyr/kernel.h>
|
||||
|
||||
#include <zephyr/logging/log_backend.h>
|
||||
#include <zephyr/logging/log_backend_net.h>
|
||||
#include <zephyr/logging/log_ctrl.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
@ -36,6 +37,16 @@ int main(void)
|
|||
const struct log_backend *backend = log_backend_net_get();
|
||||
|
||||
if (!log_backend_is_active(backend)) {
|
||||
|
||||
/* Specifying an address by calling this function will
|
||||
* override the value given to LOG_BACKEND_NET_SERVER.
|
||||
It can also be called at any other time after the backend
|
||||
is started. The net context will be released and
|
||||
restarted with the newly specified address.
|
||||
*/
|
||||
if (strlen(CONFIG_LOG_BACKEND_NET_SERVER) == 0) {
|
||||
log_backend_net_set_addr(CONFIG_NET_SAMPLE_SERVER_RUNTIME);
|
||||
}
|
||||
log_backend_init(backend);
|
||||
log_backend_enable(backend, backend->cb->ctx, CONFIG_LOG_MAX_LEVEL);
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ LOG_MODULE_REGISTER(log_backend_net, CONFIG_LOG_DEFAULT_LEVEL);
|
|||
#include <zephyr/logging/log_backend.h>
|
||||
#include <zephyr/logging/log_core.h>
|
||||
#include <zephyr/logging/log_output.h>
|
||||
#include <zephyr/logging/log_backend_net.h>
|
||||
#include <zephyr/net/net_pkt.h>
|
||||
#include <zephyr/net/net_context.h>
|
||||
|
||||
|
@ -196,19 +197,39 @@ static int format_set(const struct log_backend *const backend, uint32_t log_type
|
|||
return 0;
|
||||
}
|
||||
|
||||
static void init_net(struct log_backend const *const backend)
|
||||
bool log_backend_net_set_addr(const char *addr)
|
||||
{
|
||||
ARG_UNUSED(backend);
|
||||
int ret;
|
||||
if (net_init_done) {
|
||||
const struct log_backend *backend = log_backend_net_get();
|
||||
/* Release context so it can be recreated with the specified ip address
|
||||
* next time process() is called
|
||||
*/
|
||||
net_context_put(backend->cb->ctx);
|
||||
net_init_done = false;
|
||||
}
|
||||
|
||||
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;
|
||||
bool ret = net_ipaddr_parse(addr, strlen(addr), &server_addr);
|
||||
|
||||
if (!ret) {
|
||||
LOG_ERR("Cannot parse syslog server address");
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void init_net(struct log_backend const *const backend)
|
||||
{
|
||||
ARG_UNUSED(backend);
|
||||
|
||||
if (strlen(CONFIG_LOG_BACKEND_NET_SERVER) != 0) {
|
||||
bool ret = log_backend_net_set_addr(CONFIG_LOG_BACKEND_NET_SERVER);
|
||||
|
||||
if (!ret) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
log_backend_deactivate(log_backend_net_get());
|
||||
|
|
Loading…
Reference in a new issue