libdlog: pass timestamp from a higher layer 30/238830/3
authorAgnieszka Baumann <a.baumann@samsung.com>
Tue, 14 Jul 2020 13:40:13 +0000 (15:40 +0200)
committerAgnieszka Baumann <a.baumann@samsung.com>
Thu, 23 Jul 2020 14:55:04 +0000 (16:55 +0200)
Will allow sharing its value with the deduplication module

Change-Id: Ic66f704080f0c136ecfdd28c8ec685a2147b0be7

include/libdlog.h
src/libdlog/log.c
src/libdlog/log_android.c
src/libdlog/log_pipe.c
src/tests/libdlog_android_neg.c
src/tests/libdlog_android_pos.c
src/tests/libdlog_android_wrapper.c
src/tests/libdlog_base_wrap.c
src/tests/libdlog_prio_filter_pos.c

index 2b1c117..9c47f9b 100644 (file)
@@ -11,7 +11,7 @@
 
 struct log_config;
 
-extern int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg);
+extern int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg, struct timespec *tp_mono);
 extern void (*destroy_backend)();
 
 extern pthread_rwlock_t log_limiter_lock;
index 8d563fe..7e56386 100644 (file)
@@ -37,7 +37,7 @@
  * @return Returns the number of bytes written on success and a negative error value on error.
  * @see __dlog_init_backend
  */
-int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg) = NULL;
+int (*write_to_log)(log_id_t log_id, log_priority prio, const char *tag, const char *msg, struct timespec *tp_mono) = NULL;
 void (*destroy_backend)();
 
 pthread_rwlock_t log_limiter_lock = PTHREAD_RWLOCK_INITIALIZER;
@@ -293,8 +293,12 @@ static int dlog_check_limiter(log_id_t log_id, int prio, const char *tag)
                if (!should_log) {
                        return DLOG_ERROR_NOT_PERMITTED;
                } else if (should_log < 0) {
+                       struct timespec tp;
+                       int result = clock_gettime(CLOCK_MONOTONIC, &tp);
+                       if (result < 0)
+                               return DLOG_ERROR_NOT_PERMITTED;
                        write_to_log(log_id, prio, tag,
-                                       "Your log has been blocked due to limit of log lines per minute.");
+                                       "Your log has been blocked due to limit of log lines per minute.", &tp);
                        return DLOG_ERROR_NOT_PERMITTED;
                }
        }
@@ -317,7 +321,13 @@ static int __write_to_log_critical_section(log_id_t log_id, int prio, const char
 
        char buf[LOG_MAX_PAYLOAD_SIZE];
        vsnprintf(buf, sizeof buf, fmt, ap);
-       return write_to_log(log_id, prio, tag, buf);
+
+       struct timespec tp;
+       int result = clock_gettime(CLOCK_MONOTONIC, &tp);
+       if (result < 0)
+               return DLOG_ERROR_NONE;
+
+       return write_to_log(log_id, prio, tag, buf, &tp);
 }
 
 static int __write_to_log(log_id_t log_id, int prio, const char *tag, const char *fmt, va_list ap, bool check_should_log, bool secure_log)
index 947f3b5..b61b312 100644 (file)
@@ -28,7 +28,7 @@ static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1, -1 };
  * @param[in] msg The contents of the message.
  * @return Number of bytes written, or negative errno
  */
-static int __write_to_log_android(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
+static int __write_to_log_android(log_id_t log_id, log_priority prio, const char *tag, const char *msg, struct timespec *tp_mono)
 {
        ssize_t ret;
        int log_fd;
@@ -49,14 +49,11 @@ static int __write_to_log_android(log_id_t log_id, log_priority prio, const char
 
 #ifdef USE_ANDROID_MONOTONIC
        struct iovec vec[4];
-
-       struct timespec ts;
        struct android_logger_footer alf;
 
-       clock_gettime(CLOCK_MONOTONIC, &ts);
        alf.magic = ANDROID_LOGGER_FOOTER_MAGIC;
-       alf.sec_sent_mono = ts.tv_sec;
-       alf.nsec_sent_mono = ts.tv_nsec;
+       alf.sec_sent_mono = tp_mono->tv_sec;
+       alf.nsec_sent_mono = tp_mono->tv_nsec;
 
        vec[3].iov_base = (void *) &alf;
        vec[3].iov_len = sizeof alf;
index a4cec19..0989c15 100644 (file)
@@ -19,7 +19,7 @@ static int pipe_fd[LOG_ID_MAX] = {-1, -1, -1, -1, -1, -1};
 static char log_pipe_path[LOG_ID_MAX][PATH_MAX];
 static int wait_pipe_ms = DEFAULT_WAIT_PIPE_MS;
 
-static int __write_to_log_pipe(log_id_t log_id, log_priority prio, const char *tag, const char *msg);
+static int __write_to_log_pipe(log_id_t log_id, log_priority prio, const char *tag, const char *msg, struct timespec *tp_mono);
 
 static pthread_rwlock_t log_pipe_lock = PTHREAD_RWLOCK_INITIALIZER;
 
@@ -159,7 +159,7 @@ static int _write_to_log_pipe_critical_section(log_id_t log_id, char *buf)
  * @param[in] msg The contents of the message.
  * @return Number of bytes written, or negative errno
  */
-static int __write_to_log_pipe(log_id_t log_id, log_priority prio, const char *tag, const char *msg)
+static int __write_to_log_pipe(log_id_t log_id, log_priority prio, const char *tag, const char *msg, struct timespec *tp_mono)
 {
        ssize_t ret = 0;
        char buf[LOG_MAX_PAYLOAD_SIZE + sizeof(struct pipe_logger_entry)];
@@ -176,7 +176,7 @@ static int __write_to_log_pipe(log_id_t log_id, log_priority prio, const char *t
                return DLOG_ERROR_INVALID_PARAMETER;
 
        create_pipe_message(buf, prio, tag, msg);
-       set_pipe_message_sent_timestamp((struct pipe_logger_entry *) buf, NULL, NULL);
+       set_pipe_message_sent_timestamp((struct pipe_logger_entry *) buf, tp_mono, NULL);
 
        if (!pthread_rwlock_rdlock(&log_pipe_lock)) {
                ret = _write_to_log_pipe_critical_section(log_id, buf);
index 776cf7c..c276bdf 100644 (file)
@@ -31,7 +31,12 @@ int main()
        assert(write_to_log);
        assert(destroy_backend);
 
-#define F(B, P, M) assert(write_to_log(B, P, "tag", M) == DLOG_ERROR_INVALID_PARAMETER)
+       struct timespec tp;
+       int result = clock_gettime(CLOCK_MONOTONIC, &tp);
+       if (result < 0)
+               return EXIT_FAILURE;
+
+#define F(B, P, M) assert(write_to_log(B, P, "tag", M, &tp) == DLOG_ERROR_INVALID_PARAMETER)
        F(LOG_ID_INVALID, DLOG_ERROR       , "xx");
        F(LOG_ID_MAX    , DLOG_ERROR       , "xx");
        F(LOG_ID_MAIN   , DLOG_UNKNOWN     , "xx");
@@ -47,7 +52,7 @@ int main()
                writev_called = false;
                writev_ret = -1;
                errno_to_set = i + 10;
-               assert(write_to_log(i, i + 3, "tag", LONG_TEXTS[i]) == -(i + 10));
+               assert(write_to_log(i, i + 3, "tag", LONG_TEXTS[i], &tp) == -(i + 10));
                assert(writev_called);
        }
 
index 0bf5252..db88f7e 100644 (file)
@@ -24,13 +24,17 @@ int main()
        assert(write_to_log);
        assert(destroy_backend);
 
+       struct timespec tp;
+       int result = clock_gettime(CLOCK_MONOTONIC, &tp);
+       if (result < 0)
+               return EXIT_FAILURE;
 
        used_tag = "tag";
        for (int i = 0; i < 4; ++i) {
                writev_called = false;
                writev_ret = i + 20;
                used_msg = LONG_TEXTS[i];
-               assert(write_to_log(i, i + 3, "tag", LONG_TEXTS[i]) == i + 20);
+               assert(write_to_log(i, i + 3, "tag", LONG_TEXTS[i], &tp) == i + 20);
                assert(writev_called);
        }
 
@@ -38,7 +42,7 @@ int main()
        used_tag = "";
        used_msg = LONG_TEXTS[0];
        writev_ret = 1234;
-       assert(write_to_log(0, 3, NULL, LONG_TEXTS[0]) == 1234);
+       assert(write_to_log(0, 3, NULL, LONG_TEXTS[0], &tp) == 1234);
 
        destroy_backend();
        assert(closed + 1 == LOG_ID_MAX);
index 714ab24..5e26742 100644 (file)
@@ -28,7 +28,7 @@
 #include <queued_entry.h>
 #endif /* USE_ANDROID_MONOTONIC */
 
-int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg) = NULL;
+int (*write_to_log)(log_id_t, log_priority, const char *tag, const char *msg, struct timespec *tp_mono) = NULL;
 void (*destroy_backend)() = NULL;
 extern void __dlog_init_android(const struct log_config *conf);
 
index 1edae07..f4ba0a9 100644 (file)
@@ -35,8 +35,8 @@ void destroy()
        destroyed = true;
 }
 
-int wtl_android(log_id_t buf_id, log_priority pri, const char *tag, const char *msg) { return 45835705; }
-int wtl_pipe(log_id_t buf_id, log_priority pri, const char *tag, const char *msg) { return 0xDECC16; }
+int wtl_android(log_id_t buf_id, log_priority pri, const char *tag, const char *msg, struct timespec *tp_mono) { return 45835705; }
+int wtl_pipe(log_id_t buf_id, log_priority pri, const char *tag, const char *msg, struct timespec *tp_mono) { return 0xDECC16; }
 
 void __dlog_init_pipe(const struct log_config *conf) { write_to_log = wtl_pipe; }
 void __dlog_init_android(const struct log_config *conf)
index fa7cdc9..ad03858 100644 (file)
@@ -23,7 +23,7 @@
 #include <libdlog.h>
 #include <logconfig.h>
 
-int wtl(log_id_t buf_id, log_priority pri, const char *tag, const char *msg) { return 0xABCD; }
+int wtl(log_id_t buf_id, log_priority pri, const char *tag, const char *msg, struct timespec *tp_mono) { return 0xABCD; }
 void __dlog_init_pipe(const struct log_config *conf) { write_to_log = wtl; }
 
 int __wrap_log_config_read(struct log_config *config)