From 90a949bb94a11fd38ae28e756da02f6abb857161 Mon Sep 17 00:00:00 2001 From: Otavio Pontes Date: Thu, 10 Oct 2019 13:23:38 -0700 Subject: [PATCH] json: Making formatter_json API more independent Only export functions that make sense to be used externally and move function to set/unset json out of the formatter. Signed-off-by: Otavio Pontes --- src/globals.c | 11 +++++++++ src/lib/formatter_json.c | 48 +++++++++++++++------------------------- src/lib/formatter_json.h | 9 ++------ 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/globals.c b/src/globals.c index e884b41e..cd9af5f5 100644 --- a/src/globals.c +++ b/src/globals.c @@ -63,6 +63,17 @@ static bool quiet = false; static bool debug = false; static bool verbose = false; +static void set_json_format(bool on) +{ + if (on) { + log_set_function(log_json); + progress_set_format(json_progress, json_start, json_end); + } else { + log_set_function(NULL); + progress_set_format(NULL, NULL, NULL); + } +} + /* Sets the content_url global variable */ static void set_content_url(char *url) { diff --git a/src/lib/formatter_json.c b/src/lib/formatter_json.c index fa812ea1..e05958d3 100644 --- a/src/lib/formatter_json.c +++ b/src/lib/formatter_json.c @@ -25,14 +25,8 @@ #include "formatter_json.h" #include "log.h" #include "macros.h" -#include "progress.h" #include "strings.h" -static void log_json(FILE *out UNUSED_PARAM, const char *file UNUSED_PARAM, int line UNUSED_PARAM, const char *label, const char *format, va_list args_list) -{ - json_message(label, format, args_list); -} - static void json_sanitize_string(char *full_msg) { int i; @@ -55,30 +49,7 @@ static void json_sanitize_string(char *full_msg) } } -void set_json_format(bool on) -{ - if (on) { - log_set_function(log_json); - progress_set_format(json_progress, json_start, json_end); - } else { - log_set_function(NULL); - progress_set_format(NULL, NULL, NULL); - } -} - -void json_start(const char *op) -{ - fprintf(stdout, "[\n{ \"type\" : \"start\", \"section\" : \"%s\" },\n", op); - fflush(stdout); -} - -void json_end(const char *op, int status) -{ - fprintf(stdout, "{ \"type\" : \"end\", \"section\" : \"%s\", \"status\" : %d }\n]\n", op, status); - fflush(stdout); -} - -void json_message(const char *msg_type, const char *msg, va_list args_list) +static void json_message(const char *msg_type, const char *msg, va_list args_list) { char *full_msg; char *type = NULL; @@ -107,6 +78,23 @@ void json_message(const char *msg_type, const char *msg, va_list args_list) } } +void json_start(const char *op) +{ + fprintf(stdout, "[\n{ \"type\" : \"start\", \"section\" : \"%s\" },\n", op); + fflush(stdout); +} + +void json_end(const char *op, int status) +{ + fprintf(stdout, "{ \"type\" : \"end\", \"section\" : \"%s\", \"status\" : %d }\n]\n", op, status); + fflush(stdout); +} + +void log_json(FILE *out UNUSED_PARAM, const char *file UNUSED_PARAM, int line UNUSED_PARAM, const char *label, const char *format, va_list args_list) +{ + json_message(label, format, args_list); +} + void json_progress(const char *step_description, unsigned int current_step, unsigned int total_steps, int percentage) { fprintf(stdout, "{ \"type\" : \"progress\", " diff --git a/src/lib/formatter_json.h b/src/lib/formatter_json.h index 73e534a4..cf03579e 100644 --- a/src/lib/formatter_json.h +++ b/src/lib/formatter_json.h @@ -13,11 +13,6 @@ extern "C" { #endif -/** - * @brief Enables/Disables the JSON formatter - */ -void set_json_format(bool on); - /** * @brief Generates the initial message of a JSON stream */ @@ -29,9 +24,9 @@ void json_start(const char *); void json_end(const char *, int); /** - * @brief Converts the provided message to a JSON stream + * @brief Log message using Json format */ -void json_message(const char *, const char *, va_list); +void log_json(FILE *out, const char *file, int line, const char *label, const char *format, va_list args_list); /** * @brief Prints the progress of a given step into the JSON stream