libdlogutil: extract op set check 33/291733/2
authorMichal Bloch <m.bloch@samsung.com>
Thu, 20 Apr 2023 13:57:10 +0000 (15:57 +0200)
committerMichal Bloch <m.bloch@samsung.com>
Fri, 21 Apr 2023 16:03:16 +0000 (18:03 +0200)
Change-Id: I3dfed3f6c298c594af2f41e98c95c347f99dd39c
Signed-off-by: Michal Bloch <m.bloch@samsung.com>
src/libdlogutil/logretrieve.c

index fc3d269..511e2c2 100644 (file)
 #include <assert.h>
 #include <limits.h>
 
-static const struct fd_ops *const backend_ops[] = {
-       [BACKEND_PIPE]           = &ops_pipe,
-       [BACKEND_ANDROID_LOGGER] = &ops_logger,
-       [BACKEND_ZERO_COPY]      = &ops_zero_copy,
-};
+static const struct fd_ops *get_ops_by_buffer(size_t buf_id, int enabled_buffers, backend_t backend, bool is_compressed_memory)
+{
+       if (!bit_test(enabled_buffers, buf_id))
+               return NULL;
+
+       /* These are implemented by the pipe daemon,
+        * regardless of the nominal backend. */
+       if (is_compressed_memory
+       || buf_id == LOG_ID_KMSG
+       || buf_id == LOG_ID_SYSLOG)
+               return &ops_pipe;
+
+       switch (backend) {
+       case BACKEND_PIPE          : return &ops_pipe;
+       case BACKEND_ANDROID_LOGGER: return &ops_logger;
+       case BACKEND_ZERO_COPY     : return &ops_zero_copy;
+       default                    : return NULL;
+       }
+}
 
 int create_initial_fdis(struct fd_info ***fdis, int enabled_buffers, backend_t backend, bool is_compressed_memory, const struct log_config *conf, log_id_t aliased[LOG_ID_MAX])
 {
@@ -50,6 +64,10 @@ int create_initial_fdis(struct fd_info ***fdis, int enabled_buffers, backend_t b
        int fdi_cnt = 0;
        int r;
 
+       const struct fd_ops *ops_by_buffer[LOG_ID_MAX];
+       for (size_t i = 0; i < NELEMS(ops_by_buffer); ++i)
+               ops_by_buffer[i] = get_ops_by_buffer(i, enabled_buffers, backend, is_compressed_memory);
+
        fdi_ptrs = calloc(bit_count(enabled_buffers) + 1 /* NULL terminator */, sizeof *fdi_ptrs);
        if (!fdi_ptrs)
                return TIZEN_ERROR_OUT_OF_MEMORY;
@@ -58,25 +76,11 @@ int create_initial_fdis(struct fd_info ***fdis, int enabled_buffers, backend_t b
 
        fdi_cnt = 0;
        for (int i = 0; i < LOG_ID_MAX; ++i) {
-               if (!bit_test(enabled_buffers, i))
+               const struct fd_ops *const ops = ops_by_buffer[i];
+               if (!ops)
                        continue;
 
-               struct fd_info *fdi;
-               if (is_compressed_memory) {
-                       /* Ignore `is_pipe`. This feature is done
-                        * via pipes always, even on other backends. */
-                       fdi = fdi_create(&ops_pipe, i);
-               } else {
-                       switch (i) {
-                       case LOG_ID_KMSG:
-                       case LOG_ID_SYSLOG:
-                               fdi = fdi_create(&ops_pipe, i);
-                               break;
-                       default:
-                               fdi = fdi_create(backend_ops[backend], i);
-                               break;
-                       }
-               }
+               struct fd_info *const fdi = fdi_create(ops, i);
                if (!fdi)
                        return -ENOMEM;