Debian Codegraph @

src/log_json.c

raw symbols SCIP
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) 2025 Linutronix GmbH
4 */
5
6 #include <errno.h>
7 #include <inttypes.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/socket.h>
13 #include <time.h>
14 #include <unistd.h>
15
16 #include "app_config.h"
17
18 #include "config.h"
19 #include "log.h"
20 #include "log_json.h"
21 #include "net.h"
22 #include "stat.h"
23 #include "thread.h"
24 #include "utils.h"
25
26 static void log_json_add_traffic_class(struct log_json_thread_context *ctx,
27 const struct statistics *stat, const char *tc)
28 {
29 char stat_message[4096];
30 int ret;
31
32 /* Convert stats to json */
33 ret = stat_to_json(stat_message, sizeof(stat_message), stat, tc,
34 app_config.log_json_measurement_name);
35 if (ret) {
36 log_message(LOG_LEVEL_DEBUG, "JsonTx: Failed to compose JSON message: %s\n",
37 strerror(errno));
38 return;
39 }
40
41 /* Send it via UDP */
42 switch (ctx->dest.ss_family) {
43 case AF_INET:
44 ret = sendto(ctx->socket, stat_message, strlen(stat_message), 0,
45 (struct sockaddr_in *)&ctx->dest, sizeof(struct sockaddr_in));
46 break;
47 case AF_INET6:
48 ret = sendto(ctx->socket, stat_message, strlen(stat_message), 0,
49 (struct sockaddr_in6 *)&ctx->dest, sizeof(struct sockaddr_in6));
50 break;
51 default:
52 ret = -EINVAL;
53 }
54
55 if (ret < 0)
56 log_message(LOG_LEVEL_DEBUG, "JsonTx: sendto() for statistic message failed: %s\n",
57 strerror(errno));
58 }
59
60 void *log_json_publisher(void *data)
61 {
62 struct log_json_thread_context *ctx = data;
63 uint64_t period = app_config.stats_collection_interval_ns;
64 struct statistics stats[NUM_FRAME_TYPES];
65 struct timespec time;
66 int ret;
67
68 /*
69 * Send the statistics periodically to the JSON/UDP host. This thread can run with low
70 * priority to not influence to Application Tasks that much.
71 */
72 ret = clock_gettime(app_config.application_clock_id, &time);
73 if (ret) {
74 fprintf(stderr, "JSON: clock_gettime() failed: %s!", strerror(errno));
75 goto out;
76 }
77
78 while (!ctx->stop) {
79 increment_period(&time, period);
80 ret = clock_nanosleep(app_config.application_clock_id, TIMER_ABSTIME, &time, NULL);
81 if (ret) {
82 pthread_error(ret, "JSON: clock_nanosleep() failed");
83 goto out;
84 }
85
86 /* Get latest statistics data */
87 stat_get_global_stats(stats, sizeof(stats));
88
89 /* Publish via UDP */
90 for (int i = 0; i < NUM_FRAME_TYPES; i++) {
91 if (config_is_traffic_class_active(stat_frame_type_to_string(i)))
92 log_json_add_traffic_class(ctx, &stats[i],
93 stat_frame_type_to_string(i));
94 }
95 }
96
97 out:
98 return NULL;
99 }
100
101 struct log_json_thread_context *log_json_thread_create(void)
102 {
103 struct log_json_thread_context *ctx;
104 int ret = 0;
105
106 if (!app_config.log_json)
107 return NULL;
108
109 ctx = calloc(1, sizeof(*ctx));
110 if (!ctx)
111 return NULL;
112
113 /* Allocate UDP client socket */
114 ctx->socket = create_udp_cl_socket(app_config.log_json_host, app_config.log_json_port,
115 &ctx->dest);
116 if (ctx->socket <= 0) {
117 fprintf(stderr, "JSON: Failed to allocate UDP socket for JSON logging!\n");
118 goto err_socket;
119 }
120
121 /* Start dedicated JSON/UDP pubslishing thread */
122 ret = create_rt_thread(&ctx->pid, "LoggerJson", app_config.log_json_thread_priority,
123 app_config.log_json_thread_cpu, log_json_publisher, ctx);
124
125 if (ret) {
126 fprintf(stderr, "JSON: Failed to start JSON Logger thread!\n");
127 goto err_thread;
128 }
129
130 return ctx;
131
132 err_thread:
133 close(ctx->socket);
134 err_socket:
135 free(ctx);
136 return NULL;
137 }
138
139 void log_json_thread_stop(struct log_json_thread_context *ctx)
140 {
141 if (!ctx)
142 return;
143
144 ctx->stop = 1;
145 pthread_join(ctx->pid, NULL);
146 }
147
148 void log_json_thread_free(struct log_json_thread_context *ctx)
149 {
150 if (!ctx)
151 return;
152
153 close(ctx->socket);
154 free(ctx);
155 }
156
157 void log_json_thread_wait_for_finish(struct log_json_thread_context *ctx)
158 {
159 if (!ctx)
160 return;
161
162 pthread_join(ctx->pid, NULL);
163 }