Skip to content

Commit d4de9e6

Browse files
irfanHaslandedmichalvasko
authored andcommitted
log OPTIMIZE skip no-op messages entirely
There are cases where logging is turned off, for example when finding the correct type of a union member. It is wasteful to format and print the log message, only to free it without using it at all. Learn whether a log message will be used before creating it.
1 parent cec4188 commit d4de9e6

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/log.c

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,38 @@ log_stderr_path_line(const char *data_path, const char *schema_path, uint64_t li
579579
fprintf(stderr, par ? ")\n" : "\n");
580580
}
581581

582+
/**
583+
* @brief Learn whether a log is a no-operation or must be produced, based on current ly_log_opts.
584+
*
585+
* @param[in] level Message log level to compare to enabled logging level.
586+
* @param[out] will_log Optionally learn whether the log will be printed.
587+
* @param[out] will_store Optionally learn whether the log will be stored.
588+
* @return 1 if the log is a no-operation, 0 otherwise.
589+
*/
590+
static ly_bool
591+
log_is_noop(LY_LOG_LEVEL level, ly_bool *will_log, ly_bool *will_store)
592+
{
593+
ly_bool lolog, lostore;
594+
595+
/* learn effective logger options */
596+
if (temp_ly_log_opts) {
597+
lolog = *temp_ly_log_opts & LY_LOLOG;
598+
lostore = *temp_ly_log_opts & LY_LOSTORE;
599+
} else {
600+
lolog = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG;
601+
lostore = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE;
602+
}
603+
604+
if (will_log) {
605+
*will_log = lolog;
606+
}
607+
if (will_store) {
608+
*will_store = lostore;
609+
}
610+
611+
return (level > ATOMIC_LOAD_RELAXED(ly_ll)) || (!lolog && !lostore);
612+
}
613+
582614
/**
583615
* @brief Log a message.
584616
*
@@ -601,16 +633,7 @@ log_vprintf(const struct ly_ctx *ctx, LY_LOG_LEVEL level, LY_ERR err, LY_VECODE
601633
const char *msg;
602634
ly_bool free_strs = 1, lolog, lostore;
603635

604-
/* learn effective logger options */
605-
if (temp_ly_log_opts) {
606-
lolog = *temp_ly_log_opts & LY_LOLOG;
607-
lostore = *temp_ly_log_opts & LY_LOSTORE;
608-
} else {
609-
lolog = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOLOG;
610-
lostore = ATOMIC_LOAD_RELAXED(ly_log_opts) & LY_LOSTORE;
611-
}
612-
613-
if (level > ATOMIC_LOAD_RELAXED(ly_ll)) {
636+
if (log_is_noop(level, &lolog, &lostore)) {
614637
/* do not print or store the message */
615638
goto cleanup;
616639
}
@@ -858,6 +881,10 @@ ly_vlog(const struct ly_ctx *ctx, const char *apptag, LY_VECODE code, const char
858881
char *data_path = NULL, *schema_path = NULL;
859882
uint64_t line = 0;
860883

884+
if (log_is_noop(LY_LLERR, NULL, NULL)) {
885+
return;
886+
}
887+
861888
if (ctx) {
862889
ly_vlog_build_path_line(ctx, &data_path, &schema_path, &line);
863890
}
@@ -887,9 +914,10 @@ ly_ext_log(const struct ly_ctx *ctx, const char *plugin_name, LY_LOG_LEVEL level
887914
{
888915
char *plugin_msg;
889916

890-
if (ATOMIC_LOAD_RELAXED(ly_ll) < level) {
917+
if (log_is_noop(level, NULL, NULL)) {
891918
return;
892919
}
920+
893921
if (asprintf(&plugin_msg, "Ext plugin \"%s\": %s", plugin_name, format) == -1) {
894922
LOGMEM(ctx);
895923
return;
@@ -987,6 +1015,10 @@ _ly_err_print(const struct ly_ctx *ctx, const struct ly_err_item *eitem, const c
9871015

9881016
LY_CHECK_ARG_RET(ctx, eitem, );
9891017

1018+
if (log_is_noop(eitem->level, NULL, NULL)) {
1019+
return;
1020+
}
1021+
9901022
if (eitem->data_path) {
9911023
data_path = strdup(eitem->data_path);
9921024
}

0 commit comments

Comments
 (0)