Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rz_log and rz_core_notify functions usable with not-formatted strings. #4448

Merged
merged 5 commits into from
Apr 21, 2024
Merged
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
30 changes: 30 additions & 0 deletions librz/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,36 @@ RZ_API void rz_core_notify_error(RZ_NONNULL RzCore *core, RZ_NONNULL const char
va_end(args);
}

/**
* \brief Prints a message definining the beginning of a task
*
* \param core The RzCore to use
* \param text The message to notify
*/
RZ_API void rz_core_notify_begin_str(RZ_NONNULL RzCore *core, RZ_NONNULL const char *text) {
Rot127 marked this conversation as resolved.
Show resolved Hide resolved
rz_core_notify_begin(core, "%s", text);
}

/**
* \brief Prints a message definining the end of a task which succeeded
*
* \param core The RzCore to use
* \param text The message to notify
*/
RZ_API void rz_core_notify_done_str(RZ_NONNULL RzCore *core, RZ_NONNULL const char *text) {
rz_core_notify_done(core, "%s", text);
}

/**
* \brief Prints a message definining the end of a task which errored
*
* \param core The RzCore to use
* \param text The message to notify
*/
RZ_API void rz_core_notify_error_str(RZ_NONNULL RzCore *core, RZ_NONNULL const char *text) {
rz_core_notify_error(core, "%s", text);
}

static int on_fcn_new(RzAnalysis *_analysis, void *_user, RzAnalysisFunction *fcn) {
RzCore *core = (RzCore *)_user;
const char *cmd = rz_config_get(core->config, "cmd.fcn.new");
Expand Down
4 changes: 4 additions & 0 deletions librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ RZ_API void rz_core_notify_begin(RZ_NONNULL RzCore *core, RZ_NONNULL const char
RZ_API void rz_core_notify_done(RZ_NONNULL RzCore *core, RZ_NONNULL const char *format, ...) RZ_PRINTF_CHECK(2, 3);
RZ_API void rz_core_notify_error(RZ_NONNULL RzCore *core, RZ_NONNULL const char *format, ...) RZ_PRINTF_CHECK(2, 3);

RZ_API void rz_core_notify_begin_str(RZ_NONNULL RzCore *core, RZ_NONNULL const char *text);
RZ_API void rz_core_notify_done_str(RZ_NONNULL RzCore *core, RZ_NONNULL const char *text);
RZ_API void rz_core_notify_error_str(RZ_NONNULL RzCore *core, RZ_NONNULL const char *text);

/**
* \brief APIs to handle Visual Gadgets
*/
Expand Down
3 changes: 3 additions & 0 deletions librz/include/rz_util/rz_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ RZ_API void rz_log(const char *funcname, const char *filename,
RZ_API void rz_vlog(const char *funcname, const char *filename,
ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr, va_list args);

RZ_API void rz_log_str(const char *funcname, const char *filename,
ut32 lineno, RzLogLevel level, const char *tag, const char *msg);

#ifdef __cplusplus
}
#endif
Expand Down
16 changes: 16 additions & 0 deletions librz/util/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ RZ_API void rz_vlog(const char *funcname, const char *filename,
* \param filename Contains the filename that funcname is defined in
* \param lineno The line number that this log call is being made from in filename
* \param lvl Logging level for output
* \param tag A string placed at the beginning of the log message. If NULL, the log level is printed.
* \param fmtstr A printf like string

This function is used by the RZ_LOG_* preprocessor macros for logging
Expand All @@ -272,3 +273,18 @@ RZ_API void rz_log(const char *funcname, const char *filename,
rz_vlog(funcname, filename, lineno, level, tag, fmtstr, args);
va_end(args);
}

/**
* \brief Logging function for simple strings.
*
* \param funcname Contains the function name of the calling function
* \param filename Contains the filename that \p funcname is defined in
* \param lineno The line number that this log call is being made from in filename
* \param lvl Logging level for output
* \param tag A string placed at the beginning of the log message. If NULL, the log level is printed.
* \param msg The log message.
*/
RZ_API void rz_log_str(const char *funcname, const char *filename,
ut32 lineno, RzLogLevel level, const char *tag, const char *msg) {
rz_log(funcname, filename, lineno, level, tag, "%s", msg);
}
17 changes: 17 additions & 0 deletions test/unit/test_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ static int small_check(const char *output, const char *funcname, const char *fil
return 0;
}

static int bind_check(const char *output, const char *funcname, const char *filename,
ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr, ...) {
mu_assert_streq(output, "ERROR: 3", "bind check msg should be correct");
return 0;
}

static int large_check(const char *output, const char *funcname, const char *filename,
ut32 lineno, RzLogLevel level, const char *tag, const char *fmtstr, ...) {
char *exp_msg = RZ_NEWS0(char, 2000);
Expand All @@ -24,13 +30,23 @@ static int large_check(const char *output, const char *funcname, const char *fil

bool test_log_small(void) {
rz_log_del_callback((RzLogCallback)large_check);
rz_log_del_callback((RzLogCallback)bind_check);
rz_log_add_callback((RzLogCallback)small_check);
rz_log("func", "file", 1, RZ_LOGLVL_ERROR, NULL, "%d", 3);
mu_end;
}

bool test_log_binding(void) {
rz_log_del_callback((RzLogCallback)large_check);
rz_log_del_callback((RzLogCallback)small_check);
rz_log_add_callback((RzLogCallback)bind_check);
rz_log_str("func", "file", 1, RZ_LOGLVL_ERROR, NULL, "3");
mu_end;
}

bool test_log_large(void) {
rz_log_del_callback((RzLogCallback)small_check);
rz_log_del_callback((RzLogCallback)bind_check);
rz_log_add_callback((RzLogCallback)large_check);
char *buf = RZ_NEWS(char, 1000);
memset(buf, 0x41, 999);
Expand All @@ -42,6 +58,7 @@ bool test_log_large(void) {

bool all_tests() {
mu_run_test(test_log_small);
mu_run_test(test_log_binding);
mu_run_test(test_log_large);
return tests_passed != tests_run;
}
Expand Down
Loading