#include "json_print.h"
static json_writer_t *_jw;
-static FILE *_fp;
#define _IS_JSON_CONTEXT(type) ((type & PRINT_JSON || type & PRINT_ANY) && _jw)
#define _IS_FP_CONTEXT(type) (!_jw && (type & PRINT_FP || type & PRINT_ANY))
-void new_json_obj(int json, FILE *fp)
+void new_json_obj(int json)
{
if (json) {
- _jw = jsonw_new(fp);
+ _jw = jsonw_new(stdout);
if (!_jw) {
perror("json object");
exit(1);
}
- jsonw_pretty(_jw, true);
+ if (pretty)
+ jsonw_pretty(_jw, true);
jsonw_start_array(_jw);
}
- set_current_fp(fp);
}
void delete_json_obj(void)
return _jw != NULL;
}
-void set_current_fp(FILE *fp)
-{
- if (!fp) {
- fprintf(stderr, "Error: invalid file pointer.\n");
- exit(1);
- }
- _fp = fp;
-}
-
json_writer_t *get_json_writer(void)
{
return _jw;
jsonw_name(_jw, str);
jsonw_start_array(_jw);
} else if (_IS_FP_CONTEXT(type)) {
- fprintf(_fp, "%s", str);
+ printf("%s", str);
}
}
void close_json_array(enum output_type type, const char *str)
{
if (_IS_JSON_CONTEXT(type)) {
- jsonw_pretty(_jw, false);
jsonw_end_array(_jw);
- jsonw_pretty(_jw, true);
} else if (_IS_FP_CONTEXT(type)) {
- fprintf(_fp, "%s", str);
+ printf("%s", str);
}
}
* functions handling different types
*/
#define _PRINT_FUNC(type_name, type) \
+ __attribute__((format(printf, 4, 0))) \
void print_color_##type_name(enum output_type t, \
enum color_attr color, \
const char *key, \
else \
jsonw_##type_name##_field(_jw, key, value); \
} else if (_IS_FP_CONTEXT(t)) { \
- color_fprintf(_fp, color, fmt, value); \
+ color_fprintf(stdout, color, fmt, value); \
} \
}
_PRINT_FUNC(int, int);
+_PRINT_FUNC(s64, int64_t);
_PRINT_FUNC(hu, unsigned short);
-_PRINT_FUNC(uint, uint64_t);
+_PRINT_FUNC(uint, unsigned int);
+_PRINT_FUNC(u64, uint64_t);
+_PRINT_FUNC(luint, unsigned long int);
_PRINT_FUNC(lluint, unsigned long long int);
+_PRINT_FUNC(float, double);
#undef _PRINT_FUNC
void print_color_string(enum output_type type,
else
jsonw_string_field(_jw, key, value);
} else if (_IS_FP_CONTEXT(type)) {
- color_fprintf(_fp, color, fmt, value);
+ color_fprintf(stdout, color, fmt, value);
}
}
else
jsonw_bool(_jw, value);
} else if (_IS_FP_CONTEXT(type)) {
- color_fprintf(_fp, color, fmt, value ? "true" : "false");
+ color_fprintf(stdout, color, fmt, value ? "true" : "false");
}
}
snprintf(b1, sizeof(b1), "%#x", hex);
print_string(PRINT_JSON, key, NULL, b1);
} else if (_IS_FP_CONTEXT(type)) {
- color_fprintf(_fp, color, fmt, hex);
+ color_fprintf(stdout, color, fmt, hex);
}
}
else
jsonw_string(_jw, b1);
} else if (_IS_FP_CONTEXT(type)) {
- color_fprintf(_fp, color, fmt, hex);
+ color_fprintf(stdout, color, fmt, hex);
}
}
else
jsonw_null(_jw);
} else if (_IS_FP_CONTEXT(type)) {
- color_fprintf(_fp, color, fmt, value);
+ color_fprintf(stdout, color, fmt, value);
}
}
+
+/* Print line seperator (if not in JSON mode) */
+void print_nl(void)
+{
+ if (!_jw)
+ printf("%s", _SL_);
+}