1 /*
2  * Copyright (c) 2018 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_REGISTER(net_syslog, LOG_LEVEL_DBG);
9 
10 #include <zephyr/kernel.h>
11 
12 #include <zephyr/logging/log_backend.h>
13 #include <zephyr/logging/log_backend_net.h>
14 #include <zephyr/logging/log_ctrl.h>
15 
16 #include <stdlib.h>
17 
18 #include "net_sample_common.h"
19 
20 BUILD_ASSERT(IS_ENABLED(CONFIG_LOG_BACKEND_NET), "syslog backend not enabled");
21 
22 #define SLEEP_BETWEEN_PRINTS 3
23 
main(void)24 int main(void)
25 {
26 	int i, count, sleep;
27 
28 	LOG_DBG("Starting");
29 
30 	wait_for_network();
31 
32 	if (!IS_ENABLED(CONFIG_LOG_BACKEND_NET_AUTOSTART)) {
33 		/* Example how to start the backend if autostart is disabled.
34 		 * This is useful if the application needs to wait the network
35 		 * to be fully up before the syslog-net is able to work.
36 		 */
37 		const struct log_backend *backend = log_backend_net_get();
38 
39 		if (!log_backend_is_active(backend)) {
40 
41 			/* Specifying an address by calling this function will
42 			 * override the value given to LOG_BACKEND_NET_SERVER.
43 			   It can also be called at any other time after the backend
44 			   is started. The net context will be released and
45 			   restarted with the newly specified address.
46 			 */
47 			if (strlen(CONFIG_LOG_BACKEND_NET_SERVER) == 0) {
48 				log_backend_net_set_addr(CONFIG_NET_SAMPLE_SERVER_RUNTIME);
49 			}
50 			log_backend_init(backend);
51 			log_backend_enable(backend, backend->cb->ctx, CONFIG_LOG_MAX_LEVEL);
52 		}
53 	}
54 
55 	if (CONFIG_NET_SAMPLE_SEND_ITERATIONS) {
56 		count = CONFIG_NET_SAMPLE_SEND_ITERATIONS;
57 		sleep = 500;
58 
59 		/* This will let the Docker process to start listening */
60 		k_msleep(1500);
61 
62 		LOG_DBG("Sending total %d messages", 4 * count);
63 	} else {
64 		count = 60 / SLEEP_BETWEEN_PRINTS;
65 		sleep = SLEEP_BETWEEN_PRINTS * MSEC_PER_SEC;
66 	}
67 
68 	/* Allow some setup time before starting to send data */
69 	k_msleep(sleep);
70 
71 	i = count;
72 
73 	do {
74 		LOG_ERR("Error message (%d)", i);
75 		LOG_WRN("Warning message (%d)", i);
76 		LOG_INF("Info message (%d)", i);
77 		LOG_DBG("Debug message (%d)", i);
78 
79 		k_msleep(sleep);
80 
81 	} while (--i);
82 
83 	LOG_DBG("Stopped after %d msg", count);
84 
85 	if (CONFIG_NET_SAMPLE_SEND_ITERATIONS) {
86 		/* We get here when doing Docker based testing */
87 		k_msleep(1000);
88 		exit(0);
89 	}
90 	return 0;
91 }
92