Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ typedef struct data_output {
void (R_API_CALLCONV *print_int)(struct data_output *output, int data, char const *format);
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_reopen)(struct data_output *output);
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;
Expand All @@ -202,6 +203,9 @@ R_API void data_output_start(struct data_output *output, char const *const *fiel
/** Prints a structured data object, flushes the output if applicable. */
R_API void data_output_print(struct data_output *output, data_t *data);

/** Reopen this output by closing and opening file descriptors as needed. */
R_API void data_output_reopen(struct data_output *output);

R_API void data_output_free(struct data_output *output);

/* data output helpers */
Expand Down
8 changes: 4 additions & 4 deletions include/output_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
/** Construct data output for CSV printer.

@param log_level the highest log level to process
@param file the output stream
@param path the output stream path, defaults to stdout if NULL
@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(int log_level, FILE *file);
struct data_output *data_output_csv_create(int log_level, char const *path);

struct data_output *data_output_json_create(int log_level, FILE *file);
struct data_output *data_output_json_create(int log_level, char const *path);

struct data_output *data_output_kv_create(int log_level, FILE *file);
struct data_output *data_output_kv_create(int log_level, char const *path);

#endif /* INCLUDE_OUTPUT_FILE_H_ */
4 changes: 2 additions & 2 deletions include/output_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
/** Construct data output for LOG printer.

@param log_level the highest log level to process
@param file the optional output stream, defaults to stderr
@param path the optional output stream path, defaults to stderr if NULL
@return The auxiliary data to pass along with data_log_printer to data_print.
You must release this object with data_output_free once you're done with it.
*/
struct data_output *data_output_log_create(int log_level, FILE *file);
struct data_output *data_output_log_create(int log_level, char const *path);

#endif /* INCLUDE_OUTPUT_LOG_H_ */
4 changes: 2 additions & 2 deletions include/output_trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
/// $ echo oneshot | sudo tee /sys/class/leds/led0/trigger
/// $ rtl_433 ... -F trigger:/sys/class/leds/led0/shot
///
/// @param file a trigger output stream
/// @param path a trigger output stream path
/// @return The initialized data output.
/// You must release this object with data_output_free once you're done with it.
struct data_output *data_output_trigger_create(FILE *file);
struct data_output *data_output_trigger_create(char const *path);

#endif /* INCLUDE_OUTPUT_TRIGGER_H_ */
2 changes: 2 additions & 0 deletions include/r_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ void add_rtltcp_output(struct r_cfg *cfg, char *param);

void start_outputs(struct r_cfg *cfg, char const *const *well_known);

void reopen_outputs(struct r_cfg *cfg);

void add_sr_dumper(struct r_cfg *cfg, char const *spec, int overwrite);

void reopen_dumpers(struct r_cfg *cfg);
Expand Down
7 changes: 7 additions & 0 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ R_API void data_output_start(struct data_output *output, char const *const *fiel
output->output_start(output, fields, num_fields);
}

R_API void data_output_reopen(struct data_output *output)
{
if (!output || !output->output_reopen)
return;
output->output_reopen(output);
}

R_API void data_output_free(data_output_t *output)
{
if (!output)
Expand Down
158 changes: 123 additions & 35 deletions src/output_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

typedef struct {
struct data_output output;
char const *path;
FILE *file;
} data_output_json_t;

Expand Down Expand Up @@ -122,31 +123,58 @@ static void R_API_CALLCONV data_output_json_print(data_output_t *output, data_t
}
}

static void R_API_CALLCONV data_output_json_reopen(data_output_t *output)
{
data_output_json_t *json = (data_output_json_t *)output;

if (json->file && json->file != stdout && json->file != stderr) {
fclose(json->file);
}

if (!json->path || !*json->path) {
json->file = stdout; // No path given
}
else if (*json->path == '-' && json->path[1] == '\0') {
json->file = stdout; // STDOUT requested
}
else {
json->file = fopen(json->path, "a");
if (!json->file) {
fprintf(stderr, "rtl_433: failed to open output file\n");
exit(1);
}
}
}

static void R_API_CALLCONV data_output_json_free(data_output_t *output)
{
if (!output)
if (!output) {
return;
}

free(output);
}

struct data_output *data_output_json_create(int log_level, FILE *file)
struct data_output *data_output_json_create(int log_level, char const *path)
{
data_output_json_t *json = calloc(1, sizeof(data_output_json_t));
if (!json) {
WARN_CALLOC("data_output_json_create()");
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;
json->output.print_double = print_json_double;
json->output.print_int = print_json_int;
json->output.output_print = data_output_json_print;
json->output.output_free = data_output_json_free;
json->file = file;
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;
json->output.print_double = print_json_double;
json->output.print_int = print_json_int;
json->output.output_print = data_output_json_print;
json->output.output_reopen = data_output_json_reopen;
json->output.output_free = data_output_json_free;
json->path = path;

data_output_json_reopen(&json->output);

return (struct data_output *)json;
}
Expand Down Expand Up @@ -190,6 +218,7 @@ static int kv_break_after_key(char const *key)

typedef struct {
struct data_output output;
char const *path;
FILE *file;
void *term;
int color;
Expand Down Expand Up @@ -389,37 +418,65 @@ static void R_API_CALLCONV data_output_kv_print(data_output_t *output, data_t *d
}
}

static void R_API_CALLCONV data_output_kv_reopen(data_output_t *output)
{
data_output_kv_t *kv = (data_output_kv_t *)output;

if (kv->file && kv->file != stdout && kv->file != stderr) {
fclose(kv->file);
}

if (!kv->path || !*kv->path) {
kv->file = stdout; // No path given
}
else if (*kv->path == '-' && kv->path[1] == '\0') {
kv->file = stdout; // STDOUT requested
}
else {
kv->file = fopen(kv->path, "a");
if (!kv->file) {
fprintf(stderr, "rtl_433: failed to open output file\n");
exit(1);
}
}
}

static void R_API_CALLCONV data_output_kv_free(data_output_t *output)
{
data_output_kv_t *kv = (data_output_kv_t *)output;

if (!output)
if (!output) {
return;
}

if (kv->color)
if (kv->color) {
term_free(kv->term);
}

free(output);
}
struct data_output *data_output_kv_create(int log_level, FILE *file)
struct data_output *data_output_kv_create(int log_level, char const *path)
{
data_output_kv_t *kv = calloc(1, sizeof(data_output_kv_t));
if (!kv) {
WARN_CALLOC("data_output_kv_create()");
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;
kv->output.print_double = print_kv_double;
kv->output.print_int = print_kv_int;
kv->output.output_print = data_output_kv_print;
kv->output.output_free = data_output_kv_free;
kv->file = file;
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;
kv->output.print_double = print_kv_double;
kv->output.print_int = print_kv_int;
kv->output.output_print = data_output_kv_print;
kv->output.output_reopen = data_output_kv_reopen;
kv->output.output_free = data_output_kv_free;
kv->path = path;

kv->term = term_init(file);
data_output_kv_reopen(&kv->output);

kv->term = term_init(kv->file);
kv->color = term_has_color(kv->term);

kv->ring_bell = 0; // TODO: enable if requested...
Expand All @@ -431,6 +488,7 @@ struct data_output *data_output_kv_create(int log_level, FILE *file)

typedef struct {
struct data_output output;
char const *path;
FILE *file;
const char **fields;
const char *separator;
Expand Down Expand Up @@ -611,32 +669,62 @@ static void R_API_CALLCONV data_output_csv_print(data_output_t *output, data_t *
fflush(csv->file);
}

static void R_API_CALLCONV data_output_csv_reopen(data_output_t *output)
{
data_output_csv_t *csv = (data_output_csv_t *)output;

if (csv->file && csv->file != stdout && csv->file != stderr) {
fclose(csv->file);
}

if (!csv->path || !*csv->path) {
csv->file = stdout; // No path given
}
else if (*csv->path == '-' && csv->path[1] == '\0') {
csv->file = stdout; // STDOUT requested
}
else {
csv->file = fopen(csv->path, "a");
if (!csv->file) {
fprintf(stderr, "rtl_433: failed to open output file\n");
exit(1);
}
}
}

static void R_API_CALLCONV data_output_csv_free(data_output_t *output)
{
data_output_csv_t *csv = (data_output_csv_t *)output;

if (!output) {
return;
}

free((void *)csv->fields);
free(csv);
}

struct data_output *data_output_csv_create(int log_level, FILE *file)
struct data_output *data_output_csv_create(int log_level, char const *path)
{
data_output_csv_t *csv = calloc(1, sizeof(data_output_csv_t));
if (!csv) {
WARN_CALLOC("data_output_csv_create()");
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;
csv->output.print_double = print_csv_double;
csv->output.print_int = print_csv_int;
csv->output.output_start = data_output_csv_start;
csv->output.output_print = data_output_csv_print;
csv->output.output_free = data_output_csv_free;
csv->file = file;
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;
csv->output.print_double = print_csv_double;
csv->output.print_int = print_csv_int;
csv->output.output_start = data_output_csv_start;
csv->output.output_print = data_output_csv_print;
csv->output.output_reopen = data_output_csv_reopen;
csv->output.output_free = data_output_csv_free;
csv->path = path;

data_output_csv_reopen(&csv->output);

return (struct data_output *)csv;
}
Loading
Loading