Skip to content

Commit

Permalink
Remove globals and refactor to complete implementation of logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
wargio committed Apr 18, 2024
1 parent 77f8f80 commit c1f571e
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 93 deletions.
4 changes: 2 additions & 2 deletions librz/bin/dwarf/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ static bool CU_attrs_parse(
RzBinDwarfCompUnit *cu,
RzBinDwarfAbbrevDecl *abbrev_decl) {

RZ_LOG_SILLY("0x%" PFMT64x ":\t%s%s [%" PFMT64d "] %s\n",
RZ_LOG_DEBUG("0x%" PFMT64x ":\t%s%s [%" PFMT64d "] %s\n",
die->offset, rz_str_indent(die->depth), rz_bin_dwarf_tag(die->tag),
die->abbrev_code, rz_bin_dwarf_children(die->has_children));
RzBinDwarfAttrSpec *spec = NULL;
Expand Down Expand Up @@ -195,7 +195,7 @@ static bool CU_dies_parse(
};
// there can be "null" entries that have abbr_code == 0
if (!abbrev_code) {
RZ_LOG_SILLY("0x%" PFMT64x ":\t%sNULL\n", offset, rz_str_indent(die.depth));
RZ_LOG_DEBUG("0x%" PFMT64x ":\t%sNULL\n", offset, rz_str_indent(die.depth));
rz_vector_push(&unit->dies, &die);
depth--;
if (depth <= 0) {
Expand Down
43 changes: 21 additions & 22 deletions librz/core/cconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -2880,35 +2880,31 @@ static bool cb_log_config_traplevel(void *coreptr, void *nodeptr) {

static bool cb_log_config_file(void *coreptr, void *nodeptr) {
RzConfigNode *node = (RzConfigNode *)nodeptr;
const char *value = node->value;
rz_log_set_file(value);
return true;
return rz_log_set_file(node->value);
}

static bool cb_log_config_srcinfo(void *coreptr, void *nodeptr) {
static bool cb_log_config_show_sources(void *coreptr, void *nodeptr) {
RzConfigNode *node = (RzConfigNode *)nodeptr;
const char *value = node->value;
switch (value[0]) {
case 't':
case 'T':
rz_log_set_srcinfo(true);
break;
default:
rz_log_set_srcinfo(false);
if (rz_str_is_true(value)) {
rz_log_set_show_sources(true);
} else if (rz_str_is_false(value)) {
rz_log_set_show_sources(false);
} else {
return false;
}
return true;
}

static bool cb_log_config_colors(void *coreptr, void *nodeptr) {
RzConfigNode *node = (RzConfigNode *)nodeptr;
const char *value = node->value;
switch (value[0]) {
case 't':
case 'T':
if (rz_str_is_true(value)) {
rz_log_set_colors(true);
break;
default:
} else if (rz_str_is_false(value)) {
rz_log_set_colors(false);
} else {
return false;
}
return true;
}
Expand Down Expand Up @@ -3338,21 +3334,24 @@ RZ_API int rz_core_config_init(RzCore *core) {
// RZ_LOGLEVEL / log.level
p = rz_sys_getenv("RZ_LOGLEVEL");
SETICB("log.level", p ? atoi(p) : RZ_DEFAULT_LOGLVL, cb_log_config_level, "Target log level/severity"
" (0:SILLY, 1:DEBUG, 2:VERBOSE, 3:INFO, 4:WARN, 5:ERROR, 6:FATAL)");
" (0:DEBUG, 1:VERBOSE, 2:INFO, 3:WARN, 4:ERROR, 5:FATAL)");
free(p);
// RZ_LOGTRAP_LEVEL / log.traplevel
p = rz_sys_getenv("RZ_LOGTRAPLEVEL");
SETICB("log.traplevel", p ? atoi(p) : RZ_LOGLVL_FATAL, cb_log_config_traplevel, "Log level for trapping rizin when hit"
" (0:SILLY, 1:VERBOSE, 2:DEBUG, 3:INFO, 4:WARN, 5:ERROR, 6:FATAL)");
SETICB("log.traplevel", p ? atoi(p) : RZ_DEFAULT_LOGLVL_TRAP, cb_log_config_traplevel, "Log level for trapping rizin when hit"
" (0:VERBOSE, 1:DEBUG, 2:INFO, 3:WARN, 4:ERROR, 5:FATAL)");
free(p);

// RZ_LOGFILE / log.file
p = rz_sys_getenv("RZ_LOGFILE");
SETCB("log.file", p ? p : "", cb_log_config_file, "Logging output filename / path");
free(p);
// RZ_LOGSRCINFO / log.srcinfo
p = rz_sys_getenv("RZ_LOGSRCINFO");
SETCB("log.srcinfo", p ? p : "false", cb_log_config_srcinfo, "Should the log output contain src info (filename:lineno)");

// RZ_LOGSHOWSOURCES / log.show.sources
p = rz_sys_getenv("RZ_LOGSHOWSOURCES");
SETCB("log.show.sources", p ? p : "false", cb_log_config_show_sources, "Should the log output contain src info (filename:lineno)");
free(p);

// RZ_LOGCOLORS / log.colors
p = rz_sys_getenv("RZ_LOGCOLORS");
SETCB("log.colors", p ? p : "false", cb_log_config_colors, "Should the log output use colors (TODO)");
Expand Down
28 changes: 14 additions & 14 deletions librz/include/rz_util/rz_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@
#endif

typedef enum rz_log_level {
RZ_LOGLVL_SILLY = 0,
RZ_LOGLVL_DEBUG = 1,
RZ_LOGLVL_VERBOSE = 2,
RZ_LOGLVL_INFO = 3,
RZ_LOGLVL_WARN = 4,
RZ_LOGLVL_ERROR = 5,
RZ_LOGLVL_FATAL = 6, // This will call rz_sys_breakpoint() and trap the process for debugging!
RZ_LOGLVL_DEBUG = 0,
RZ_LOGLVL_VERBOSE,
RZ_LOGLVL_INFO,
RZ_LOGLVL_WARN,
RZ_LOGLVL_ERROR,
RZ_LOGLVL_FATAL, ///< This will call rz_sys_breakpoint() and trap the process for debugging!
/* other flags */
RZ_LOGLVL_SIZE,
RZ_LOGLVL_NONE = 0xFF
} RzLogLevel;

Expand All @@ -30,6 +31,8 @@ typedef enum rz_log_level {
#define RZ_DEFAULT_LOGLVL RZ_LOGLVL_ERROR
#endif

#define RZ_DEFAULT_LOGLVL_TRAP RZ_LOGLVL_FATAL

typedef void (*RzLogCallback)(const char *output, const char *funcname, const char *filename,
ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr, ...) RZ_PRINTF_CHECK(7, 8);

Expand All @@ -40,12 +43,9 @@ typedef void (*RzLogCallback)(const char *output, const char *funcname, const ch
__LINE__, lvl, tag, fmtstr, ##__VA_ARGS__);

#if RZ_BUILD_DEBUG
#define RZ_LOG_SILLY(fmtstr, ...) rz_log(MACRO_LOG_FUNC, __FILE__, \
__LINE__, RZ_LOGLVL_SILLY, NULL, fmtstr, ##__VA_ARGS__);
#define RZ_LOG_DEBUG(fmtstr, ...) rz_log(MACRO_LOG_FUNC, __FILE__, \
__LINE__, RZ_LOGLVL_DEBUG, NULL, fmtstr, ##__VA_ARGS__);
#else
#define RZ_LOG_SILLY(fmtstr, ...)
#define RZ_LOG_DEBUG(fmtstr, ...)
#endif

Expand All @@ -66,14 +66,14 @@ extern "C" {

// Called by rz_core to set the configuration variables
RZ_API void rz_log_set_level(RzLogLevel level);
RZ_API void rz_log_set_file(const char *filename);
RZ_API void rz_log_set_srcinfo(bool show_info);
RZ_API bool rz_log_set_file(RZ_NULLABLE const char *filename);
RZ_API void rz_log_set_show_sources(bool show_sources);
RZ_API void rz_log_set_colors(bool show_colors);
RZ_API void rz_log_set_traplevel(RzLogLevel level);

// Functions for adding log callbacks
RZ_API void rz_log_add_callback(RzLogCallback cbfunc);
RZ_API void rz_log_del_callback(RzLogCallback cbfunc);
RZ_API void rz_log_add_callback(RZ_NULLABLE RzLogCallback cbfunc);
RZ_API void rz_log_del_callback(RZ_NULLABLE RzLogCallback cbfunc);
// TODO: rz_log_get_callbacks()

/* Define rz_log as weak so it can be 'overwritten' externally
Expand Down
Loading

0 comments on commit c1f571e

Please sign in to comment.