logging: use NMStrBuf in _domains_to_string()

This commit is contained in:
Thomas Haller
2021-01-14 10:52:07 +01:00
parent 29e93b1738
commit 597ad6aa8a

View File

@@ -24,6 +24,7 @@
#include "libnm-glib-aux/nm-logging-base.h" #include "libnm-glib-aux/nm-logging-base.h"
#include "libnm-glib-aux/nm-time-utils.h" #include "libnm-glib-aux/nm-time-utils.h"
#include "libnm-glib-aux/nm-str-buf.h"
/*****************************************************************************/ /*****************************************************************************/
@@ -457,22 +458,22 @@ _domains_to_string(gboolean include_level_override,
const NMLogDomain log_state[static _LOGL_N_REAL]) const NMLogDomain log_state[static _LOGL_N_REAL])
{ {
const LogDesc *diter; const LogDesc *diter;
GString * str; NMStrBuf sbuf;
int i; int i;
/* We don't just return g_strdup() the logging domains that were set during /* We don't just return g_strdup() the logging domains that were set during
* nm_logging_setup(), because we want to expand "DEFAULT" and "ALL". * nm_logging_setup(), because we want to expand "DEFAULT" and "ALL".
*/ */
str = g_string_sized_new(75); nm_str_buf_init(&sbuf, NM_UTILS_GET_NEXT_REALLOC_SIZE_40, FALSE);
for (diter = &domain_desc[0]; diter->name; diter++) { for (diter = &domain_desc[0]; diter->name; diter++) {
/* If it's set for any lower level, it will also be set for LOGL_ERR */ /* If it's set for any lower level, it will also be set for LOGL_ERR */
if (!(diter->num & log_state[LOGL_ERR])) if (!(diter->num & log_state[LOGL_ERR]))
continue; continue;
if (str->len) nm_str_buf_append_required_delimiter(&sbuf, ',');
g_string_append_c(str, ','); nm_str_buf_append(&sbuf, diter->name);
g_string_append(str, diter->name);
if (!include_level_override) if (!include_level_override)
continue; continue;
@@ -480,7 +481,8 @@ _domains_to_string(gboolean include_level_override,
/* Check if it's logging at a lower level than the default. */ /* Check if it's logging at a lower level than the default. */
for (i = 0; i < log_level; i++) { for (i = 0; i < log_level; i++) {
if (diter->num & log_state[i]) { if (diter->num & log_state[i]) {
g_string_append_printf(str, ":%s", nm_log_level_desc[i].name); nm_str_buf_append_c(&sbuf, ':');
nm_str_buf_append(&sbuf, nm_log_level_desc[i].name);
break; break;
} }
} }
@@ -488,13 +490,14 @@ _domains_to_string(gboolean include_level_override,
if (!(diter->num & log_state[log_level])) { if (!(diter->num & log_state[log_level])) {
for (i = log_level + 1; i < _LOGL_N_REAL; i++) { for (i = log_level + 1; i < _LOGL_N_REAL; i++) {
if (diter->num & log_state[i]) { if (diter->num & log_state[i]) {
g_string_append_printf(str, ":%s", nm_log_level_desc[i].name); nm_str_buf_append_c(&sbuf, ':');
nm_str_buf_append(&sbuf, nm_log_level_desc[i].name);
break; break;
} }
} }
} }
} }
return g_string_free(str, FALSE); return nm_str_buf_finalize(&sbuf, NULL);
} }
static char _all_logging_domains_to_str[273]; static char _all_logging_domains_to_str[273];