Review at  https://gerrit.osmocom.org/5488

cosmetic: logging: simplify logging level VTY cmd doc composition

In log_vty_command_description(), which composes a VTY command doc string, use
talloc_asprintf_append() to compose the doc string instead of the strangely
convoluted way the function worked before this patch.

Looking at LOGGING_STR, I came across this function and "by accident" started
to refactor it, to understand what it is doing. I considered dropping the patch
but since it is already here I might as well submit it.

Change-Id: Ib818e2d524c750f07bea2aa585011f40e3ee82e8
---
M src/logging.c
1 file changed, 15 insertions(+), 40 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/88/5488/1

diff --git a/src/logging.c b/src/logging.c
index 20ec443..8e65208 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -904,59 +904,34 @@
 {
        struct log_info *info = osmo_log_info;
        char *str;
-       int i, ret, len = 0, offset = 0, rem;
-       unsigned int size =
-               strlen(LOGGING_STR
-                      "Set the log level for a specified category\n") + 1;
+       int i;
 
        assert_loginfo();
 
-       for (i = 0; i < info->num_cat; i++) {
-               if (info->cat[i].name == NULL)
-                       continue;
-               size += strlen(info->cat[i].description) + 1;
-       }
-
-       for (i = 0; i < LOGLEVEL_DEFS; i++)
-               size += strlen(loglevel_descriptions[i]) + 1;
-
-       size += strlen("Global setting for all subsystems") + 1;
-       rem = size;
-       str = talloc_zero_size(tall_log_ctx, size);
+       str = talloc_zero_size(tall_log_ctx, 4096);
        if (!str)
                return NULL;
 
-       ret = snprintf(str + offset, rem, LOGGING_STR
-                       "Set the log level for a specified category\n");
-       if (ret < 0)
-               goto err;
-       OSMO_SNPRINTF_RET(ret, rem, offset, len);
+#define SPRINTF(fmt, args...) \
+       do {\
+               str = talloc_asprintf_append(str, fmt, ## args); \
+               if (!str) \
+                       return NULL; \
+       } while(0)
 
-       ret = snprintf(str + offset, rem,
-                       "Global setting for all subsystems\n");
-       if (ret < 0)
-               goto err;
-       OSMO_SNPRINTF_RET(ret, rem, offset, len);
+       SPRINTF(LOGGING_STR "Set the log level for a specified category\n");
+       SPRINTF("Global setting for all subsystems\n");
 
        for (i = 0; i < info->num_cat; i++) {
                if (info->cat[i].name == NULL)
                        continue;
-               ret = snprintf(str + offset, rem, "%s\n",
-                               info->cat[i].description);
-               if (ret < 0)
-                       goto err;
-               OSMO_SNPRINTF_RET(ret, rem, offset, len);
+               SPRINTF("%s\n", info->cat[i].description);
        }
-       for (i = 0; i < LOGLEVEL_DEFS; i++) {
-               ret = snprintf(str + offset, rem, "%s\n",
-                               loglevel_descriptions[i]);
-               if (ret < 0)
-                       goto err;
-               OSMO_SNPRINTF_RET(ret, rem, offset, len);
-       }
-err:
-       str[size-1] = '\0';
+       for (i = 0; i < LOGLEVEL_DEFS; i++)
+               SPRINTF("%s\n", loglevel_descriptions[i]);
+
        return str;
+#undef SPRINTF
 }
 
 /*! Initialize the Osmocom logging core

-- 
To view, visit https://gerrit.osmocom.org/5488
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib818e2d524c750f07bea2aa585011f40e3ee82e8
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofm...@sysmocom.de>

Reply via email to