Add output log level options ()

This commit is contained in:
Christian W. Zuckschwerdt 2022-12-19 18:44:29 +01:00 committed by GitHub
parent d312226424
commit 0ff7247336
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 64 additions and 15 deletions

View file

@ -150,6 +150,7 @@ typedef struct data_output {
void (R_API_CALLCONV *output_start)(struct data_output *output, char const *const *fields, int num_fields);
void (R_API_CALLCONV *output_print)(struct data_output *output, data_t *data);
void (R_API_CALLCONV *output_free)(struct data_output *output);
int log_level; ///< the maximum log level (verbosity) allowed, more verbose messages must be ignored.
} data_output_t;
/** Setup known field keys and start output, used by CSV only.

View file

@ -21,10 +21,10 @@
@return The auxiliary data to pass along with data_csv_printer to data_print.
You must release this object with data_output_free once you're done with it.
*/
struct data_output *data_output_csv_create(FILE *file);
struct data_output *data_output_csv_create(int log_level, FILE *file);
struct data_output *data_output_json_create(FILE *file);
struct data_output *data_output_json_create(int log_level, FILE *file);
struct data_output *data_output_kv_create(FILE *file);
struct data_output *data_output_kv_create(int log_level, FILE *file);
#endif /* INCLUDE_OUTPUT_FILE_H_ */

View file

@ -14,6 +14,6 @@
#include "data.h"
struct data_output *data_output_syslog_create(const char *host, const char *port);
struct data_output *data_output_syslog_create(int log_level, const char *host, const char *port);
#endif /* INCLUDE_OUTPUT_UDP_H_ */

View file

@ -1242,6 +1242,7 @@ struct data_output *data_output_http_create(struct mg_mgr *mgr, char const *host
return NULL;
}
http->output.log_level = LOG_TRACE; // sensible default, not parsed from args
http->output.print_data = print_http_data;
http->output.output_free = data_output_http_free;

View file

@ -129,7 +129,7 @@ static void R_API_CALLCONV data_output_json_free(data_output_t *output)
free(output);
}
struct data_output *data_output_json_create(FILE *file)
struct data_output *data_output_json_create(int log_level, FILE *file)
{
data_output_json_t *json = calloc(1, sizeof(data_output_json_t));
if (!json) {
@ -137,6 +137,7 @@ struct data_output *data_output_json_create(FILE *file)
return NULL; // NOTE: returns NULL on alloc failure.
}
json->output.log_level = log_level;
json->output.print_data = print_json_data;
json->output.print_array = print_json_array;
json->output.print_string = print_json_string;
@ -329,7 +330,7 @@ static void R_API_CALLCONV data_output_kv_free(data_output_t *output)
free(output);
}
struct data_output *data_output_kv_create(FILE *file)
struct data_output *data_output_kv_create(int log_level, FILE *file)
{
data_output_kv_t *kv = calloc(1, sizeof(data_output_kv_t));
if (!kv) {
@ -337,6 +338,7 @@ struct data_output *data_output_kv_create(FILE *file)
return NULL; // NOTE: returns NULL on alloc failure.
}
kv->output.log_level = log_level;
kv->output.print_data = print_kv_data;
kv->output.print_array = print_kv_array;
kv->output.print_string = print_kv_string;
@ -546,7 +548,7 @@ static void R_API_CALLCONV data_output_csv_free(data_output_t *output)
free(csv);
}
struct data_output *data_output_csv_create(FILE *file)
struct data_output *data_output_csv_create(int log_level, FILE *file)
{
data_output_csv_t *csv = calloc(1, sizeof(data_output_csv_t));
if (!csv) {
@ -554,6 +556,7 @@ struct data_output *data_output_csv_create(FILE *file)
return NULL; // NOTE: returns NULL on alloc failure.
}
csv->output.log_level = log_level;
csv->output.print_data = print_csv_data;
csv->output.print_array = print_csv_array;
csv->output.print_string = print_csv_string;

View file

@ -193,7 +193,7 @@ static void R_API_CALLCONV data_output_syslog_free(data_output_t *output)
free(syslog);
}
struct data_output *data_output_syslog_create(const char *host, const char *port)
struct data_output *data_output_syslog_create(int log_level, const char *host, const char *port)
{
data_output_syslog_t *syslog = calloc(1, sizeof(data_output_syslog_t));
if (!syslog) {
@ -210,6 +210,7 @@ struct data_output *data_output_syslog_create(const char *host, const char *port
}
#endif
syslog->output.log_level = log_level;
syslog->output.output_print = data_output_syslog_print;
syslog->output.output_free = data_output_syslog_free;
// Severity 5 "Notice", Facility 20 "local use 4"

View file

@ -928,6 +928,43 @@ void flush_report_data(r_cfg_t *cfg)
/* setup */
static int lvlarg_param(char **param, int default_verb)
{
if (!param || !*param) {
return default_verb;
}
// parse ", v = %d"
char *p = *param;
if (*p != ',') {
return default_verb;
}
p++;
while (*p == ' ' || *p == '\t')
p++;
if (*p != 'v') {
fprintf(stderr, "Unknown output option \"%s\"\n", *param);
exit(1);
}
p++;
while (*p == ' ' || *p == '\t')
p++;
if (*p != '=') {
fprintf(stderr, "Unknown output option \"%s\"\n", *param);
exit(1);
}
p++;
while (*p == ' ' || *p == '\t')
p++;
char *endptr;
int val = strtol(p, &endptr, 10);
if (p == endptr) {
fprintf(stderr, "Invalid output option \"%s\"\n", *param);
exit(1);
}
*param = endptr;
return val;
}
static FILE *fopen_output(char *param)
{
FILE *file;
@ -944,12 +981,14 @@ static FILE *fopen_output(char *param)
void add_json_output(r_cfg_t *cfg, char *param)
{
list_push(&cfg->output_handler, data_output_json_create(fopen_output(param)));
int log_level = lvlarg_param(&param, 0);
list_push(&cfg->output_handler, data_output_json_create(log_level, fopen_output(param)));
}
void add_csv_output(r_cfg_t *cfg, char *param)
{
list_push(&cfg->output_handler, data_output_csv_create(fopen_output(param)));
int log_level = lvlarg_param(&param, 0);
list_push(&cfg->output_handler, data_output_csv_create(log_level, fopen_output(param)));
}
void start_outputs(r_cfg_t *cfg, char const *const *well_known)
@ -966,7 +1005,8 @@ void start_outputs(r_cfg_t *cfg, char const *const *well_known)
void add_kv_output(r_cfg_t *cfg, char *param)
{
list_push(&cfg->output_handler, data_output_kv_create(fopen_output(param)));
int log_level = lvlarg_param(&param, LOG_TRACE);
list_push(&cfg->output_handler, data_output_kv_create(log_level, fopen_output(param)));
}
void add_mqtt_output(r_cfg_t *cfg, char *param)
@ -981,16 +1021,18 @@ void add_influx_output(r_cfg_t *cfg, char *param)
void add_syslog_output(r_cfg_t *cfg, char *param)
{
int log_level = lvlarg_param(&param, LOG_WARNING);
char *host = "localhost";
char *port = "514";
hostport_param(param, &host, &port);
print_logf(LOG_CRITICAL, "Syslog UDP", "Sending datagrams to %s port %s", host, port);
list_push(&cfg->output_handler, data_output_syslog_create(host, port));
list_push(&cfg->output_handler, data_output_syslog_create(log_level, host, port));
}
void add_http_output(r_cfg_t *cfg, char *param)
{
// Note: no log_level, the HTTP-API consumes all log levels.
char *host = "0.0.0.0";
char *port = "8433";
hostport_param(param, &host, &port);
@ -1001,6 +1043,7 @@ void add_http_output(r_cfg_t *cfg, char *param)
void add_trigger_output(r_cfg_t *cfg, char *param)
{
// Note: no log_level, we never trigger on logs.
list_push(&cfg->output_handler, data_output_trigger_create(fopen_output(param)));
}

View file

@ -37,9 +37,9 @@ int main(void)
NULL);
const char *fields[] = { "label", "house_code", "temp", "array", "array2", "array3", "data", "house_code" };
void *json_output = data_output_json_create(stdout);
void *kv_output = data_output_kv_create(stdout);
void *csv_output = data_output_csv_create(stdout);
void *json_output = data_output_json_create(0, stdout);
void *kv_output = data_output_kv_create(0, stdout);
void *csv_output = data_output_csv_create(0, stdout);
data_output_start(csv_output, fields, sizeof fields / sizeof *fields);
data_output_print(json_output, data); fprintf(stdout, "\n");