| 1 | // SPDX-License-Identifier: BSD-2-Clausecxx . . $ `<file>/src/log_json.c`/ |
| 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"cxx . . $ `<file>/build/app_config.h`/ |
| 17 | |
| 18 | #include "config.h"cxx . . $ `<file>/src/config.h`/ |
| 19 | #include "log.h"cxx . . $ `<file>/src/log.h`/ |
| 20 | #include "log_json.h"cxx . . $ `<file>/src/log_json.h`/ |
| 21 | #include "net.h"cxx . . $ `<file>/src/net.h`/ |
| 22 | #include "stat.h"cxx . . $ `<file>/src/stat.h`/ |
| 23 | #include "thread.h"cxx . . $ `<file>/src/thread.h`/ |
| 24 | #include "utils.h"cxx . . $ `<file>/src/utils.h`/ |
| 25 | |
| 26 | static void log_json_add_traffic_classcxx . . $ log_json_add_traffic_class(ea7b173d9128ac92).(struct log_json_thread_contextcxx . . $ log_json_thread_context# *ctx, |
| 27 | const struct statisticscxx . . $ statistics# *stat, const char *tc) |
| 28 | { |
| 29 | char stat_message[4096]; |
| 30 | int ret; |
| 31 | |
| 32 | /* Convert stats to json */ |
| 33 | ret = stat_to_jsoncxx . . $ stat_to_json(fc2a9ca47478af75).(stat_message, sizeof(stat_message), stat, tc, |
| 34 | app_configcxx . . $ app_config..log_json_measurement_namecxx . . $ application_config#log_json_measurement_name.); |
| 35 | if (ret) { |
| 36 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., "JsonTx: Failed to compose JSON message: %s\n", |
| 37 | strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | /* Send it via UDP */ |
| 42 | switch (ctx->destcxx . . $ log_json_thread_context#dest..ss_familycxx . . $ sockaddr_storage#ss_family.) { |
| 43 | case AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!: |
| 44 | ret = sendtocxx . . $ sendto(efe9044f8eddf5c).(ctx->socketcxx . . $ log_json_thread_context#socket., stat_message, strlencxx . . $ strlen(751346f8406fd082).(stat_message), 0, |
| 45 | (struct sockaddr_incxx . . $ sockaddr_in# *)&ctx->destcxx . . $ log_json_thread_context#dest., sizeof(struct sockaddr_incxx . . $ sockaddr_in#)); |
| 46 | break; |
| 47 | case AF_INET6cxx . . $ `<external>/8962547315054503424/socket.h:105:9`!: |
| 48 | ret = sendtocxx . . $ sendto(efe9044f8eddf5c).(ctx->socketcxx . . $ log_json_thread_context#socket., stat_message, strlencxx . . $ strlen(751346f8406fd082).(stat_message), 0, |
| 49 | (struct sockaddr_in6cxx . . $ sockaddr_in6# *)&ctx->destcxx . . $ log_json_thread_context#dest., sizeof(struct sockaddr_in6cxx . . $ sockaddr_in6#)); |
| 50 | break; |
| 51 | default: |
| 52 | ret = -EINVALcxx . . $ `<external>/8987895939074593727/errno-base.h:26:9`!; |
| 53 | } |
| 54 | |
| 55 | if (ret < 0) |
| 56 | log_messagecxx . . $ log_message(171546b8a219727a).(LOG_LEVEL_DEBUGcxx . . $ LOG_LEVEL_DEBUG., "JsonTx: sendto() for statistic message failed: %s\n", |
| 57 | strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 58 | } |
| 59 | |
| 60 | void *log_json_publishercxx . . $ log_json_publisher(9f162e79bfc6c74).(void *data) |
| 61 | { |
| 62 | struct log_json_thread_contextcxx . . $ log_json_thread_context# *ctx = data; |
| 63 | uint64_tcxx . . $ uint64_t# period = app_configcxx . . $ app_config..stats_collection_interval_nscxx . . $ application_config#stats_collection_interval_ns.; |
| 64 | struct statisticscxx . . $ statistics# stats[NUM_FRAME_TYPEScxx . . $ NUM_FRAME_TYPES.]; |
| 65 | struct timespeccxx . . $ 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_gettimecxx . . $ clock_gettime(65aaefb2502b4987).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., &time); |
| 73 | if (ret) { |
| 74 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "JSON: clock_gettime() failed: %s!", strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 75 | goto out; |
| 76 | } |
| 77 | |
| 78 | while (!ctx->stopcxx . . $ log_json_thread_context#stop.) { |
| 79 | increment_periodcxx . . $ increment_period(4d7c74d32dea27cd).(&time, period); |
| 80 | ret = clock_nanosleepcxx . . $ clock_nanosleep(99dc002056e22615).(app_configcxx . . $ app_config..application_clock_idcxx . . $ application_config#application_clock_id., TIMER_ABSTIMEcxx . . $ `<external>/89459239771508533/time.h:69:10`!, &time, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 81 | if (ret) { |
| 82 | pthread_errorcxx . . $ pthread_error(66835f578de1bcb3).(ret, "JSON: clock_nanosleep() failed"); |
| 83 | goto out; |
| 84 | } |
| 85 | |
| 86 | /* Get latest statistics data */ |
| 87 | stat_get_global_statscxx . . $ stat_get_global_stats(36543745c1651445).(stats, sizeof(stats)); |
| 88 | |
| 89 | /* Publish via UDP */ |
| 90 | for (int i = 0; i < NUM_FRAME_TYPEScxx . . $ NUM_FRAME_TYPES.; i++) { |
| 91 | if (config_is_traffic_class_activecxx . . $ config_is_traffic_class_active(c5f7da30cbc0f4e9).(stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(i))) |
| 92 | log_json_add_traffic_classcxx . . $ log_json_add_traffic_class(ea7b173d9128ac92).(ctx, &stats[i], |
| 93 | stat_frame_type_to_stringcxx . . $ stat_frame_type_to_string(413bfe925ee921da).(i)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | out: |
| 98 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 99 | } |
| 100 | |
| 101 | struct log_json_thread_contextcxx . . $ log_json_thread_context# *log_json_thread_createcxx . . $ log_json_thread_create(28a00b00e83b43e2).(void) |
| 102 | { |
| 103 | struct log_json_thread_contextcxx . . $ log_json_thread_context# *ctx; |
| 104 | int ret = 0; |
| 105 | |
| 106 | if (!app_configcxx . . $ app_config..log_jsoncxx . . $ application_config#log_json.) |
| 107 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 108 | |
| 109 | ctx = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(*ctx)); |
| 110 | if (!ctx) |
| 111 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 112 | |
| 113 | /* Allocate UDP client socket */ |
| 114 | ctx->socketcxx . . $ log_json_thread_context#socket. = create_udp_cl_socketcxx . . $ create_udp_cl_socket(16218bade0b54147).(app_configcxx . . $ app_config..log_json_hostcxx . . $ application_config#log_json_host., app_configcxx . . $ app_config..log_json_portcxx . . $ application_config#log_json_port., |
| 115 | &ctx->destcxx . . $ log_json_thread_context#dest.); |
| 116 | if (ctx->socketcxx . . $ log_json_thread_context#socket. <= 0) { |
| 117 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "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_threadcxx . . $ create_rt_thread(670ec5a211ade64c).(&ctx->pidcxx . . $ log_json_thread_context#pid., "LoggerJson", app_configcxx . . $ app_config..log_json_thread_prioritycxx . . $ application_config#log_json_thread_priority., |
| 123 | app_configcxx . . $ app_config..log_json_thread_cpucxx . . $ application_config#log_json_thread_cpu., log_json_publishercxx . . $ log_json_publisher(9f162e79bfc6c74)., ctx); |
| 124 | |
| 125 | if (ret) { |
| 126 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "JSON: Failed to start JSON Logger thread!\n"); |
| 127 | goto err_thread; |
| 128 | } |
| 129 | |
| 130 | return ctx; |
| 131 | |
| 132 | err_thread: |
| 133 | closecxx . . $ close(7864480464b09eea).(ctx->socketcxx . . $ log_json_thread_context#socket.); |
| 134 | err_socket: |
| 135 | freecxx . . $ free(5d22bdacc48458e8).(ctx); |
| 136 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 137 | } |
| 138 | |
| 139 | void log_json_thread_stopcxx . . $ log_json_thread_stop(804bb534ff7495b0).(struct log_json_thread_contextcxx . . $ log_json_thread_context# *ctx) |
| 140 | { |
| 141 | if (!ctx) |
| 142 | return; |
| 143 | |
| 144 | ctx->stopcxx . . $ log_json_thread_context#stop. = 1; |
| 145 | pthread_joincxx . . $ pthread_join(269902cf949f12c5).(ctx->pidcxx . . $ log_json_thread_context#pid., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 146 | } |
| 147 | |
| 148 | void log_json_thread_freecxx . . $ log_json_thread_free(804bb534ff7495b0).(struct log_json_thread_contextcxx . . $ log_json_thread_context# *ctx) |
| 149 | { |
| 150 | if (!ctx) |
| 151 | return; |
| 152 | |
| 153 | closecxx . . $ close(7864480464b09eea).(ctx->socketcxx . . $ log_json_thread_context#socket.); |
| 154 | freecxx . . $ free(5d22bdacc48458e8).(ctx); |
| 155 | } |
| 156 | |
| 157 | void log_json_thread_wait_for_finishcxx . . $ log_json_thread_wait_for_finish(804bb534ff7495b0).(struct log_json_thread_contextcxx . . $ log_json_thread_context# *ctx) |
| 158 | { |
| 159 | if (!ctx) |
| 160 | return; |
| 161 | |
| 162 | pthread_joincxx . . $ pthread_join(269902cf949f12c5).(ctx->pidcxx . . $ log_json_thread_context#pid., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 163 | } |