Entry getters return error code 99/222299/7
authorMichal Bloch <m.bloch@samsung.com>
Mon, 13 Jan 2020 09:28:04 +0000 (10:28 +0100)
committerMichal Bloch <m.bloch@samsung.com>
Thu, 16 Jan 2020 13:01:25 +0000 (14:01 +0100)
And return the actual thing through a parameter.
This is the Tizen API style.

Change-Id: Ibf4056a3d3a70c4c27de0b9bf5534c4bb575628c

15 files changed:
Makefile.am
include/logprint.h
include/queued_entry_timestamp.h
src/shared/fd_info.c
src/shared/log_file.c
src/shared/logprint.c
src/shared/logretrieve.c
src/shared/queued_entry_timestamp.c
src/shared/sort_vector.c
src/tests/fd_info.c
src/tests/kmsg_parser.c
src/tests/log_file.c
src/tests/logprint.c
src/tests/queued_entry.c
src/tests/syslog_parser.c

index 5d93671..ffd8128 100644 (file)
@@ -257,7 +257,7 @@ src_tests_sort_vector_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=log_config_get_int,--wr
 
 src_tests_fd_info_SOURCES = src/tests/fd_info.c src/shared/fd_info.c
 src_tests_fd_info_CFLAGS = $(check_CFLAGS)
-src_tests_fd_info_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=sort_vector_push,--wrap=malloc,--wrap=free,--wrap=close,--wrap=dlogutil_entry_get_ts,--wrap=log_should_print_line
+src_tests_fd_info_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=sort_vector_push,--wrap=malloc,--wrap=free,--wrap=close,--wrap=dlogutil_entry_get_timestamp,--wrap=log_should_print_line
 
 src_tests_fdi_logger_SOURCES = src/tests/fdi_logger.c src/shared/fdi_logger.c src/shared/ptrs_list.c src/shared/logcommon.c
 src_tests_fdi_logger_CFLAGS = $(check_CFLAGS)
@@ -285,7 +285,7 @@ src_tests_libdlog_base_LDFLAGS = $(AM_LDFLAGS) -lpthread -Wl,--wrap=log_config_r
 
 src_tests_log_file_SOURCES = src/tests/log_file.c src/shared/log_file.c
 src_tests_log_file_CFLAGS = $(check_CFLAGS)
-src_tests_log_file_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=strdup,--wrap=free,--wrap=memcpy,--wrap=snprintf,--wrap=open,--wrap=open64,--wrap=fstat,--wrap=fstat64,--wrap=rename,--wrap=dlogutil_entry_get_ts,--wrap=log_print_log_line,--wrap=dlogutil_entry_get_tag,--wrap=isatty
+src_tests_log_file_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=strdup,--wrap=free,--wrap=memcpy,--wrap=snprintf,--wrap=open,--wrap=open64,--wrap=fstat,--wrap=fstat64,--wrap=rename,--wrap=dlogutil_entry_get_timestamp,--wrap=log_print_log_line,--wrap=dlogutil_entry_get_tag,--wrap=isatty
 
 src_tests_queued_entry_SOURCES = src/tests/queued_entry.c src/shared/queued_entry.c src/shared/queued_entry_timestamp.c src/shared/parsers.c src/shared/translate_syslog.c src/shared/logconfig.c src/shared/logcommon.c
 src_tests_queued_entry_CFLAGS = $(check_CFLAGS)
index 718ce0a..dd279a1 100644 (file)
@@ -161,14 +161,14 @@ bool log_should_print_line(
  *
  * Finds the beginning of a log tag in entry's payload and returns it
  */
-const char* dlogutil_entry_get_tag(const dlogutil_entry_s *entry);
+int dlogutil_entry_get_tag(const dlogutil_entry_s *entry, const char **tag);
 
 /**
  * Returns pointer to log entry message
  *
  * Finds the beginning of a log message in entry's payload and returns it
  */
-const char* dlogutil_entry_get_msg(const dlogutil_entry_s *entry);
+int dlogutil_entry_get_message(const dlogutil_entry_s *entry, const char **msg);
 
 /**
  * Formats a log message into a buffer
index f2acb71..3cb9216 100644 (file)
@@ -27,7 +27,7 @@
 void add_recv_timestamp(dlogutil_entry_s *le);
 void copy_recv_timestamp(dlogutil_entry_s *le);
 bool log_entry_is_earlier(const dlogutil_sorting_order_e sort_by, const dlogutil_entry_s *lhs, const dlogutil_entry_s *rhs);
-struct timespec dlogutil_entry_get_ts(const dlogutil_entry_s *le, dlogutil_sorting_order_e stamp_type);
+int dlogutil_entry_get_timestamp(const dlogutil_entry_s *entry, dlogutil_sorting_order_e order, struct timespec *ts);
 clockid_t get_proper_clock(dlogutil_sorting_order_e sort_by);
 
 #endif /* _QUEUED_ENTRY_TIMESTAMP_H */
index 5feb4e1..670e67e 100644 (file)
@@ -94,7 +94,10 @@ int fdi_push_log(struct fd_info *fdi, struct sort_vector *logs, dlogutil_entry_c
        int r;
 
        if (IS_VECTOR_SIZE_SORTABLE(logs->size)) {
-               struct timespec ts = dlogutil_entry_get_ts(temp, logs->sort_by);
+               struct timespec ts;
+               r = dlogutil_entry_get_timestamp(temp, logs->sort_by, &ts);
+               if (r != TIZEN_ERROR_NONE)
+                       return r;
 
                /* check if new entry is newer than util's start moment */
                if (ts.tv_sec > logs->start.tv_sec || (ts.tv_sec == logs->start.tv_sec && ts.tv_nsec > logs->start.tv_nsec))
index 572b956..92bb3ea 100644 (file)
@@ -223,12 +223,17 @@ int logfile_write_with_rotation(const dlogutil_entry_s *e, struct log_file *file
 
        int written_bytes = 0;
 
-       struct timespec ts = dlogutil_entry_get_ts(e, sort_by);
+       struct timespec ts;
+       if (dlogutil_entry_get_timestamp(e, sort_by, &ts) != TIZEN_ERROR_NONE)
+               return 1;
 
        if (ts.tv_sec < file->prev_sec || (ts.tv_sec == file->prev_sec && ts.tv_nsec < file->prev_nsec)) {
                struct dlogutil_entry_with_msg msg;
                memcpy(&msg, e, sizeof *e);
-               int r = snprintf(msg.msg, sizeof msg.msg, "%s%c%s", dlogutil_entry_get_tag(e), '\0', "INFO: Following log entry could not be sorted and is out of order.");
+               const char *tag;
+               if (dlogutil_entry_get_tag(e, &tag) != TIZEN_ERROR_NONE)
+                       return 1;
+               int r = snprintf(msg.msg, sizeof msg.msg, "%s%c%s", tag, '\0', "INFO: Following log entry could not be sorted and is out of order.");
 
                if (r < 0) {
                        ERR("unable to format out-of-order message %m");
index daaa796..66c29bd 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <tizen.h>
 
 #include <logprint.h>
 
@@ -211,9 +212,8 @@ bool log_should_print_line(dlogutil_filter_options_s *p_filter, const dlogutil_e
        const char *tag;
        log_priority prio;
 
-       tag = dlogutil_entry_get_tag(entry);
        /* mark empty-tagged messages and make it easy to catch an application that does that */
-       if (!tag || !strlen(tag))
+       if (dlogutil_entry_get_tag(entry, &tag) == TIZEN_ERROR_NO_DATA || !tag || !strlen(tag))
                tag = "DLOG_ERROR_NOTAG";
 
        prio = (log_priority)entry->priority;
@@ -538,36 +538,48 @@ int dlogutil_filter_options_tid(dlogutil_filter_options_s *p_filter, pid_t tid)
  * @brief Return pointer to log entry tag
  * @details Finds the beginning of a log tag in entry's payload and returns it
  * @param[in] entry The log entry
- * @return Pointer to log tag
+ * @param[out] tag The log tag
+ * @return An error code
+ * @retval TIZEN_ERROR_NONE Success
+ * @retval TIZEN_ERROR_NO_DATA No valid tag was found
  */
-const char* dlogutil_entry_get_tag(const dlogutil_entry_s *entry)
+int dlogutil_entry_get_tag(const dlogutil_entry_s *entry, const char **tag)
 {
        assert(entry);
+       assert(tag);
 
-       if (entry->len - (int) sizeof(dlogutil_entry_s) < 2)
-               return NULL;
-       if (((const char *)entry)[entry->len - 1] != '\0')
-               return NULL;
+       if (entry->len - (int) sizeof(dlogutil_entry_s) < 2
+       || ((const char *)entry)[entry->len - 1] != '\0') {
+               *tag = NULL;
+               return TIZEN_ERROR_NO_DATA;
+       }
 
-       return entry->msg;
+       *tag = entry->msg;
+       return TIZEN_ERROR_NONE;
 }
 
 /**
  * @brief Return pointer to log entry message
  * @details Finds the beginning of a log message in entry's payload and returns it
  * @param[in] entry The log entry
- * @return Pointer to log message
+ * @param[out] msg The log message
+ * @return An error code
+ * @retval TIZEN_ERROR_NONE Success
+ * @retval TIZEN_ERROR_NO_DATA No valid message was found
  */
-const char* dlogutil_entry_get_msg(const dlogutil_entry_s *entry)
+int dlogutil_entry_get_message(const dlogutil_entry_s *entry, const char **msg)
 {
        assert(entry);
+       assert(msg);
 
-       if (entry->len - (int) sizeof(dlogutil_entry_s) < 2)
-               return NULL;
-       if (((const char *)entry)[entry->len - 1] != '\0')
-               return NULL;
+       if (entry->len - (int) sizeof(dlogutil_entry_s) < 2
+       || ((const char *)entry)[entry->len - 1] != '\0') {
+               *msg = NULL;
+               return TIZEN_ERROR_NO_DATA;
+       }
 
-       return entry->msg + entry->tag_len + 1 /* NULL delimiter */;
+       *msg = entry->msg + entry->tag_len + 1 /* NULL delimiter */;
+       return TIZEN_ERROR_NONE;
 }
 
 dlogutil_sorting_order_e get_format_sorting(log_print_format format)
@@ -927,12 +939,12 @@ char *log_format_log_line(
 
        priChar = filter_pri_to_char((log_priority)entry->priority);
 
-       tag = dlogutil_entry_get_tag(entry);
        /* mark empty-tagged messages and make it easy to catch an application that does that */
-       if (!tag || !strlen(tag))
+       if (dlogutil_entry_get_tag(entry, &tag) == TIZEN_ERROR_NO_DATA || !tag || !strlen(tag))
                tag = "DLOG_ERROR_NOTAG";
 
-       msg = dlogutil_entry_get_msg(entry) ?: "DLOG_ERROR_NOMSG";
+       if (dlogutil_entry_get_message(entry, &msg) == TIZEN_ERROR_NO_DATA || !msg)
+               msg = "DLOG_ERROR_NOMSG";
 
        /*
         * JSON is formatted separately. This is because it's so vastly different
index dc98140..33bab7e 100644 (file)
@@ -113,7 +113,9 @@ struct fd_info *find_earliest_log(struct fd_info **data_fds, int fd_count, dlogu
                        continue;
 
                const dlogutil_entry_s *const le = fdi->ops->peek_entry(fdi);
-               struct timespec current_ts = dlogutil_entry_get_ts(le, sort_by);
+               struct timespec current_ts;
+               if (dlogutil_entry_get_timestamp(le, sort_by, &current_ts) != TIZEN_ERROR_NONE)
+                       return fdi; // the timestampless log is always the best choice to flush ASAP
 
                if (best_fdi && ((current_ts.tv_sec > best_ts.tv_sec) || (current_ts.tv_sec == best_ts.tv_sec && current_ts.tv_nsec > best_ts.tv_nsec)))
                        continue;
index 4b884bb..1b1daba 100644 (file)
@@ -95,9 +95,10 @@ bool log_entry_is_earlier(const dlogutil_sorting_order_e sort_by, const dlogutil
        return l_sec < r_sec || (l_sec == r_sec && l_nsec < r_nsec);
 }
 
-struct timespec dlogutil_entry_get_ts(const dlogutil_entry_s *le, dlogutil_sorting_order_e stamp_type)
+int dlogutil_entry_get_timestamp(const dlogutil_entry_s *le, dlogutil_sorting_order_e stamp_type, struct timespec *ts)
 {
        assert(le);
+       assert(ts);
 
        struct timespec ret;
 
@@ -122,7 +123,9 @@ struct timespec dlogutil_entry_get_ts(const dlogutil_entry_s *le, dlogutil_sorti
        default: assert(false); // LCOV_EXCL_LINE
        }
 
-       return ret;
+       *ts = ret;
+
+       return (ret.tv_sec == 0 && ret.tv_nsec == -1) ? TIZEN_ERROR_NO_DATA : TIZEN_ERROR_NONE;
 }
 
 clockid_t get_proper_clock(dlogutil_sorting_order_e sort_by)
index 3165f1f..a69d2ef 100644 (file)
@@ -198,7 +198,9 @@ long sort_vector_time_span(struct sort_vector *logs)
        if (sort_vector_empty(logs))
                return 0;
 
-       struct timespec now, oldest = dlogutil_entry_get_ts(sort_vector_back(logs), logs->sort_by);
+       struct timespec now, oldest;
+       if (dlogutil_entry_get_timestamp(sort_vector_back(logs), logs->sort_by, &oldest) != TIZEN_ERROR_NONE)
+               return -1; // report negative age to flush the invalid log immediately
        clock_gettime(get_proper_clock(logs->sort_by), &now);
 
        diff_s = now.tv_sec - oldest.tv_sec;
@@ -230,7 +232,9 @@ bool process_log(const dlogutil_entry_s *e, struct timespec reference_ts, dlogut
 
        if (timeout != -1) {
 
-               struct timespec log_ts = dlogutil_entry_get_ts(e, stamp_type);
+               struct timespec log_ts;
+               if (dlogutil_entry_get_timestamp(e, stamp_type, &log_ts) != TIZEN_ERROR_NONE)
+                       goto apply_callback; // flush timestampless logs immediately since waiting won't help
 
                long  s = reference_ts.tv_sec  - log_ts.tv_sec;
                long ns = reference_ts.tv_nsec - log_ts.tv_nsec;
@@ -243,6 +247,7 @@ bool process_log(const dlogutil_entry_s *e, struct timespec reference_ts, dlogut
                        return false;
        }
 
+apply_callback:
        *callback_ret = callback(e, userdata);
        return true;
 }
index 23a1788..c6af04e 100644 (file)
@@ -65,13 +65,14 @@ dlogutil_entry_s *test_extract(struct fd_info *fdi)
        return fail_extract ? NULL : (dlogutil_entry_s *) 0xBADFEEL;
 }
 
-struct timespec __wrap_dlogutil_entry_get_ts(const dlogutil_entry_s *le, dlogutil_sorting_order_e stamp_type)
+int __wrap_dlogutil_entry_get_timestamp(const dlogutil_entry_s *le, dlogutil_sorting_order_e stamp_type, struct timespec *ts)
 {
        assert(le == (dlogutil_entry_s *)0xBADFEEL);
-       return (struct timespec) {
+       *ts = (struct timespec) {
                .tv_sec = 1,
                .tv_nsec = 1,
        };
+       return 0;
 }
 
 static struct fd_info *expected_destroyee;
index 811a945..182031c 100644 (file)
@@ -24,10 +24,14 @@ void assert_entry(char const * msg, const struct dlogutil_entry_with_msg *expect
        assert(expected->header.pid == lem.header.pid);
        assert(expected->header.tid == lem.header.tid);
 
-       const char *const tag_expected = dlogutil_entry_get_tag(&expected->header);
-       const char *const tag_actual   = dlogutil_entry_get_tag(&lem.header);
-       const char *const msg_expected = dlogutil_entry_get_msg(&expected->header);
-       const char *const msg_actual   = dlogutil_entry_get_msg(&lem.header);
+       const char *tag_expected;
+       const char *tag_actual;
+       const char *msg_expected;
+       const char *msg_actual;
+       assert(!dlogutil_entry_get_tag(&expected->header, &tag_expected));
+       assert(!dlogutil_entry_get_tag(&lem.header, &tag_actual));
+       assert(!dlogutil_entry_get_message(&expected->header, &msg_expected));
+       assert(!dlogutil_entry_get_message(&lem.header, &msg_actual));
        assert(tag_expected);
        assert(tag_actual);
        assert(msg_expected);
index 9d280fb..56ee1b7 100644 (file)
@@ -34,20 +34,22 @@ int __wrap_rename(const char *oldpath, const char *newpath)
        return -1;
 }
 
-struct timespec __wrap_dlogutil_entry_get_ts(const dlogutil_entry_s *le, dlogutil_sorting_order_e stamp_type)
+int __wrap_dlogutil_entry_get_timestamp(const dlogutil_entry_s *le, dlogutil_sorting_order_e stamp_type, struct timespec *ts)
 {
        assert(stamp_type == DLOGUTIL_SORT_SENT_MONO);
        assert(le == (dlogutil_entry_s *) 0xBA5EBALL);
-       return (struct timespec) {
+       *ts = (struct timespec) {
                .tv_sec = 3333,
                .tv_nsec = 9999,
        };
+       return 0;
 }
 
-char *__wrap_dlogutil_entry_get_tag(const dlogutil_entry_s *entry)
+int __wrap_dlogutil_entry_get_tag(const dlogutil_entry_s *entry, const char **tag)
 {
        assert(entry == (dlogutil_entry_s *) 0xBA5EBALL);
-       return "";
+       *tag = "";
+       return 0;
 }
 
 static bool custom_memcpy;
index f7550b9..09598e8 100644 (file)
@@ -558,8 +558,13 @@ void check_syscall_failure_handling()
 void check_invalid_input()
 {
        const dlogutil_entry_s entry = { .len = 1 };
-       assert(!dlogutil_entry_get_msg(&entry));
-       assert(!dlogutil_entry_get_tag(&entry));
+       const char *data;
+
+       assert(dlogutil_entry_get_message(&entry, &data) == TIZEN_ERROR_NO_DATA);
+       assert(!data);
+
+       assert(dlogutil_entry_get_tag(&entry, &data) == TIZEN_ERROR_NO_DATA);
+       assert(!data);
 
        dlogutil_filter_options_s *const filter = log_filter_new();
        assert(filter);
index 1abe39b..4a4a783 100644 (file)
@@ -58,16 +58,16 @@ int main()
                 .sec_recv_real = 7,
                .nsec_recv_real = 8,
        };
-       ts = dlogutil_entry_get_ts(&le, DLOGUTIL_SORT_SENT_MONO);
+       assert(!dlogutil_entry_get_timestamp(&le, DLOGUTIL_SORT_SENT_MONO, &ts));
        assert(ts.tv_sec  == le. sec_sent_mono);
        assert(ts.tv_nsec == le.nsec_sent_mono);
-       ts = dlogutil_entry_get_ts(&le, DLOGUTIL_SORT_RECV_REAL);
+       assert(!dlogutil_entry_get_timestamp(&le, DLOGUTIL_SORT_RECV_REAL, &ts));
        assert(ts.tv_sec  == le. sec_recv_real);
        assert(ts.tv_nsec == le.nsec_recv_real);
-       ts = dlogutil_entry_get_ts(&le, DLOGUTIL_SORT_SENT_REAL);
+       assert(!dlogutil_entry_get_timestamp(&le, DLOGUTIL_SORT_SENT_REAL, &ts));
        assert(ts.tv_sec  == le. sec_sent_real);
        assert(ts.tv_nsec == le.nsec_sent_real);
-       ts = dlogutil_entry_get_ts(&le, DLOGUTIL_SORT_RECV_MONO);
+       assert(!dlogutil_entry_get_timestamp(&le, DLOGUTIL_SORT_RECV_MONO, &ts));
        assert(ts.tv_sec  == le. sec_recv_mono);
        assert(ts.tv_nsec == le.nsec_recv_mono);
 
index 2a92514..21ff56a 100644 (file)
@@ -67,8 +67,9 @@ int main()
 
        assert(sle->pid == 678);
        assert(sle->priority == DLOG_FATAL);
-       const char *tag = dlogutil_entry_get_tag(sle);
-       const char *msg = dlogutil_entry_get_msg(sle);
+       const char *tag, *msg;
+       assert(!dlogutil_entry_get_tag(sle, &tag));
+       assert(!dlogutil_entry_get_message(sle, &msg));
        assert(tag);
        assert(msg); // Can never be triggered, but SVACE complains without this check
        assert(!strcmp("SYSLOG_USER", tag));
@@ -80,8 +81,8 @@ int main()
 
        assert(sle->pid == 0);
        assert(sle->priority == DLOG_FATAL);
-       tag = dlogutil_entry_get_tag(sle);
-       msg = dlogutil_entry_get_msg(sle);
+       assert(!dlogutil_entry_get_tag(sle, &tag));
+       assert(!dlogutil_entry_get_message(sle, &msg));
        assert(tag);
        assert(msg); // Same as above
        assert(!strcmp("SYSLOG_USER", tag));