Move some arguments into a common struct 51/213551/2
authorMateusz Majewski <m.majewski2@samsung.com>
Fri, 6 Sep 2019 11:50:51 +0000 (13:50 +0200)
committerMateusz Majewski <m.majewski2@samsung.com>
Fri, 6 Sep 2019 13:12:27 +0000 (15:12 +0200)
Change-Id: Iacb5461d42688128c52842e20c8d32317b3cd60a

src/logutil/logutil.c

index b6b2e0d..b47c582 100644 (file)
@@ -142,6 +142,22 @@ int put_logs_into_vector(struct fd_info **data_fds, int fd_count, int nfds, stru
        return 0;
 }
 
+struct additional_options {
+       list_head filter_list;
+       log_filter *filter_object;
+};
+
+int additional_options_init(struct additional_options *opt) {
+       opt->filter_list = NULL;
+       opt->filter_object = log_filter_new();
+       return opt->filter_object ? 0 : -ENOMEM;
+}
+
+void additional_options_cleanup(struct additional_options *opt) {
+       list_clear(&opt->filter_list);
+       log_filter_free(opt->filter_object);
+}
+
 /**
  * @brief Handle input
  * @details The main loop reading log data
@@ -151,7 +167,7 @@ int put_logs_into_vector(struct fd_info **data_fds, int fd_count, int nfds, stru
  * @param[in] l_file File output metadata
  * @return int 0 if successful, 1 otherwise
  */
-static int do_print(struct fd_info **data_fds, int fd_count, struct sort_vector *logs, write_callback callback, void *userdata, list_head filter_list, log_filter *filter_object)
+static int do_print(struct fd_info **data_fds, int fd_count, struct sort_vector *logs, write_callback callback, void *userdata, struct additional_options *opt)
 {
        assert(data_fds);
        assert(logs);
@@ -165,7 +181,7 @@ static int do_print(struct fd_info **data_fds, int fd_count, struct sort_vector
        }
 
        for (int nfds = 0; nfds < fd_count; ++nfds) {
-               int r = data_fds[nfds]->ops->prepare_print(data_fds[nfds], logs->dump, filter_list, filter_object);
+               int r = data_fds[nfds]->ops->prepare_print(data_fds[nfds], logs->dump, opt->filter_list, opt->filter_object);
                if (r > 0) {
                        // everything went fine, but we're not printing this one
                        fdi_free(data_fds[nfds]);
@@ -216,7 +232,7 @@ static int do_print(struct fd_info **data_fds, int fd_count, struct sort_vector
                        }
                }
 
-               if (put_logs_into_vector(data_fds, fd_count, nfds, logs, callback, userdata, filter_object) < 0)
+               if (put_logs_into_vector(data_fds, fd_count, nfds, logs, callback, userdata, opt->filter_object) < 0)
                        return 1;
 
                /* The oldest log can be so fresh as to be from the future
@@ -232,7 +248,7 @@ static int do_print(struct fd_info **data_fds, int fd_count, struct sort_vector
                        flush_logs(logs, callback, userdata, is_from_future);
        }
 
-       if (put_logs_into_vector(data_fds, fd_count, 0, logs, callback, userdata, filter_object) < 0)
+       if (put_logs_into_vector(data_fds, fd_count, 0, logs, callback, userdata, opt->filter_object) < 0)
                return 1;
 
        if (logs->dump != DUMP_CONTINUOUS && logs->dump != DUMP_INFINITE)
@@ -249,14 +265,14 @@ static void show_version(const char *name)
        printf("%s version: %s-%s\n", name, __DLOG_VERSION, __DLOG_RELEASE);
 }
 
-int parse_options(int argc, char **argv, struct log_file *l_file, log_filter *filter_object, struct sort_vector *logs, int *enabled_buffers, action_e *action, list_head *filter_list)
+int parse_options(int argc, char **argv, struct log_file *l_file, struct sort_vector *logs, int *enabled_buffers, action_e *action, struct additional_options *opt)
 {
        assert(argv);
        assert(logs);
        assert(l_file);
        assert(enabled_buffers);
        assert(action);
-       assert(filter_list);
+       assert(opt);
 
        int silence = 0;
 
@@ -280,7 +296,7 @@ int parse_options(int argc, char **argv, struct log_file *l_file, log_filter *fi
                        int tid;
                        if (sscanf(optarg, "%d", &tid) != 1)
                                err_arg_nondigit = 1;
-                       else if (log_add_filter_tid(filter_object, tid))
+                       else if (log_add_filter_tid(opt->filter_object, tid))
                                goto enomem;
                        break;
                }
@@ -288,7 +304,7 @@ int parse_options(int argc, char **argv, struct log_file *l_file, log_filter *fi
                        int pid;
                        if (sscanf(optarg, "%d", &pid) != 1)
                                err_arg_nondigit = 1;
-                       else if (log_add_filter_pid(filter_object, pid))
+                       else if (log_add_filter_pid(opt->filter_object, pid))
                                goto enomem;
                        break;
                }
@@ -350,7 +366,7 @@ int parse_options(int argc, char **argv, struct log_file *l_file, log_filter *fi
                }
                case 's':
                        silence = 1;
-                       list_add(filter_list, "*:s");
+                       list_add(&opt->filter_list, "*:s");
                        break;
                case 'r':
                        if (sscanf(optarg, "%zu", &l_file->rotate_size_kbytes) != 1)
@@ -376,9 +392,9 @@ int parse_options(int argc, char **argv, struct log_file *l_file, log_filter *fi
 
        if (optind < argc)
                while (optind < argc)
-                       list_add(filter_list, argv[optind++]);
+                       list_add(&opt->filter_list, argv[optind++]);
        else if (!silence)
-               list_add(filter_list, "*:D");
+               list_add(&opt->filter_list, "*:D");
 
        return 0;
 
@@ -462,24 +478,21 @@ int validate_buffers(int *enabled_buffers)
 }
 
 #ifndef UNIT_TEST
-void log_filter_cleanup(log_filter **filter)
-{
-       log_filter_free(*filter);
-}
-
 int main(int argc, char **argv)
 {
        int enabled_buffers = 0; // bitset
-       __attribute__ ((cleanup(list_clear))) list_head filter_list = NULL;
        __attribute__ ((cleanup(log_config_free))) struct log_config conf = {.begin = NULL, .last = NULL};
        action_e action = ACTION_PRINT;
        __attribute__ ((cleanup(fdi_array_free))) struct fd_info **fdi_ptrs = NULL;
        int fdi_cnt = 0;
        int i;
        __attribute__ ((cleanup(logfile_free))) struct log_file l_file;
-       __attribute__ ((cleanup(log_filter_cleanup))) log_filter *filter_object = log_filter_new();
        __attribute__ ((cleanup(sort_vector_free))) struct sort_vector logs;
        int r;
+       __attribute__ ((cleanup(additional_options_cleanup))) struct additional_options opt;
+
+       if (additional_options_init(&opt))
+               goto err_nomem;
 
        sort_vector_init(&logs);
 
@@ -492,7 +505,7 @@ int main(int argc, char **argv)
        else
                sort_vector_apply_config(&logs, &conf);
 
-       r = parse_options(argc, argv, &l_file, filter_object, &logs, &enabled_buffers, &action, &filter_list);
+       r = parse_options(argc, argv, &l_file, &logs, &enabled_buffers, &action, &opt);
        if (r)
                return r < 0 ? 1 : 0;
 
@@ -581,7 +594,7 @@ int main(int argc, char **argv)
                return do_print(fdi_ptrs, fdi_cnt, &logs, logfile_callback, &(struct logfile_callback_data) {
                        .file = &l_file,
                        .sort_by = proper_sort_by,
-               }, filter_list, filter_object);
+               }, &opt);
 
        return 0;