SafeAPI RBC 2oo2 Generic Application
IL/CTC route and train logic for the safeAPIRBC2oo2 reference RBC
Loading...
Searching...
No Matches
gateway_c_logctl.c
Go to the documentation of this file.
1
5#include "gateway_c_logctl.h"
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10
11#include "safeapi/oal/log/sapi_log.h"
12
13#include "common/rbc_wire_types.h"
14#include "common/site_config.h"
16
20#define GATEWAY_C_LOGCTL_LINE_MAX 16U
21
26static int s_applied_level = -1;
27
29static bool gateway_c_logctl_read_level(const char *path, sapi_log_level_t *out_level);
30
31
33void gateway_c_logctl_poll(gateway_c_context_t *ctx)
34{
35 const char *path = getenv("SAPI_RBC_LOG_LEVEL_FILE");
36 sapi_log_level_t level;
37
38 if ((ctx == NULL) || (path == NULL))
39 {
40 return;
41 }
42 if (!gateway_c_logctl_read_level(path, &level))
43 {
44 return; /* missing file, empty, or unrecognised - leave things be */
45 }
46 if ((int)level == s_applied_level)
47 {
48 return; /* unchanged since last apply */
49 }
50
51 s_applied_level = (int)level;
52 sapi_log_set_level(level);
53
54 {
55 uint8_t frame[RBC_ENVELOPE_WIRE_SIZE];
56
57 rbc_envelope_write_set_log_level(frame, (uint8_t)level);
59 }
60
61 sapi_log_write_event(level, site_config_name(ctx->site), 0U, "GW", "A,B", "LOG_LEVEL",
62 "applied + broadcast", sapi_log_level_to_string(level));
63}
64
65
66/****Local functions ****/
67
68static bool gateway_c_logctl_read_level(const char *path, sapi_log_level_t *out_level)
69{
70 FILE *fp;
72 size_t i;
73 bool ok = false;
74
75 fp = fopen(path, "r");
76 if (fp == NULL)
77 {
78 return false;
79 }
80 if (fgets(line, (int)sizeof(line), fp) != NULL)
81 {
82 /* Trim the first trailing CR/LF/space so "DEBUG\n" parses. */
83 for (i = 0U; i < sizeof(line); i++)
84 {
85 if (line[i] == '\0')
86 {
87 break;
88 }
89 if ((line[i] == '\n') || (line[i] == '\r') || (line[i] == ' ') || (line[i] == '\t'))
90 {
91 line[i] = '\0';
92 break;
93 }
94 }
95 ok = (sapi_log_level_from_string(line, out_level) == SAPI_STATUS_OK);
96 }
97 (void)fclose(fp);
98 return ok;
99}
void gateway_c_il_handler_broadcast_to_ab(const uint8_t bytes[RBC_ENVELOPE_WIRE_SIZE])
Sends one RBC_ENVELOPE_WIRE_SIZE frame to BOTH A and B on the IL relay link. Used by gateway_c_logctl...
ILHandler - Role C Gateway's TCP/IP server for the ONE multiplexed IL sim connection and the ONE dedi...
#define GATEWAY_C_LOGCTL_LINE_MAX
static bool gateway_c_logctl_read_level(const char *path, sapi_log_level_t *out_level)
static int s_applied_level
void gateway_c_logctl_poll(gateway_c_context_t *ctx)
Called once per C cycle (gateway_c_execute()). If SAPI_RBC_LOG_LEVEL_FILE is set and its first line n...
Role C's runtime log-level control "service": one place from which the whole RBC (A,...