Log refactoring 89/195589/2
authorVyacheslav Cherkashin <v.cherkashin@samsung.com>
Tue, 11 Dec 2018 09:14:17 +0000 (12:14 +0300)
committerVyacheslav Cherkashin <v.cherkashin@samsung.com>
Fri, 28 Dec 2018 08:57:43 +0000 (11:57 +0300)
This commit implements the following:
  1. Separate the logging source code into separate files (log.h/log.c)
  2. Add vraw_log() to be able to create custom logging functions in
     submodules.
  3. Add new logging interface:
        - LOGI()
        - LOGW()
        - LOGE()

Change-Id: If428e25a03daa48271ff6a1407228781454b3b60
Signed-off-by: Vyacheslav Cherkashin <v.cherkashin@samsung.com>
Makefile
helper/common_probe_init.c
helper/libdaprobe.c
helper/log.c [new file with mode: 0644]
include/daprobe.h
include/log.h [new file with mode: 0644]
probe_event/gesture.c

index 04bc665..b4509d4 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -112,6 +112,7 @@ TIZEN_LDFLAGS = -lstdc++
 
 COMMON_SRCS = $(UTILITY_SRCS) $(PROBE_SRCS)
 UTILITY_SRCS =                         \
+       ./helper/log.c                  \
        ./helper/lsan_open.c            \
        ./helper/libdaprobe.c           \
        ./helper/dahelper.c             \
index 17d52f8..03d8390 100644 (file)
@@ -26,6 +26,7 @@
  *
  */
 
+#include <log.h>
 #include <unistd.h>
 #include "da_gles20.h"
 #include "binproto.h"
@@ -42,7 +43,7 @@ void probe_terminate_with_err(const char *msg, const char *func_name,
        snprintf(error_msg, sizeof(error_msg), "%s : [%s], %s\n", msg,
                 func_name, lib_name);
        perror(error_msg);
-       PRINTERR(error_msg);
+       LOGE("%s", error_msg);
        //wait for flush
        sleep(1);
        exit(0);
index 8b6d7ae..d21914d 100755 (executable)
@@ -646,7 +646,7 @@ const char *msg_code_to_srt(enum AppMessageType type)
 
 #define MINIMUM(X, Y) (((X) < (Y)) ? (X) : (Y))
 
-static bool do_send(int fd, void *data, size_t len, int flags)
+static bool do_send(int fd, const void *data, size_t len, int flags)
 {
        const size_t CHUNK_SIZE = 1024;
 
@@ -672,8 +672,8 @@ static bool do_send(int fd, void *data, size_t len, int flags)
        return !len;
 }
 
-static void do_msg_send(int fd, enum AppMessageType type, void *data,
-                       size_t len)
+static void do_msg_send(int fd, enum AppMessageType type,
+                       const void *data, size_t len)
 {
        struct msg_header {
                int type;
@@ -689,7 +689,7 @@ static void do_msg_send(int fd, enum AppMessageType type, void *data,
        do_send(fd, data, len, MSG_NOSIGNAL);
 }
 
-void msg_send(enum AppMessageType type, void *data, size_t len)
+void msg_send(enum AppMessageType type, const void *data, size_t len)
 {
        int fd = gTraceInfo.socket.daemonSock;
        pthread_mutex_t *mutex = &gTraceInfo.socket.sockMutex;
diff --git a/helper/log.c b/helper/log.c
new file mode 100644 (file)
index 0000000..aae2d65
--- /dev/null
@@ -0,0 +1,45 @@
+#include <log.h>
+#include <daprobe.h>
+#include <stdarg.h>
+
+
+static enum AppMessageType log_level2msg_type(enum log_level level)
+{
+       switch (level) {
+       case LL_INFO: return APP_MSG_MSG;
+       case LL_WARN: return APP_MSG_WARNING;
+       case LL_ERROR: return APP_MSG_ERROR;
+       }
+
+       /* If the log level is incorrect, returns the type of error message */
+       return APP_MSG_ERROR;
+}
+
+void vraw_log(enum log_level level, const char *format, va_list args)
+{
+       char buf[1024];
+       const size_t buf_size = sizeof(buf);
+
+       int n = vsnprintf(buf, buf_size, format, args);
+       if (n > 0) {
+               if ((size_t)n < buf_size) {
+                       /* Success */
+                       enum AppMessageType msg_type = log_level2msg_type(level);
+                       msg_send(msg_type, buf, n);
+               } else {
+                       const char error_msg[] = "Log message is very log";
+                       msg_send(APP_MSG_ERROR, error_msg, sizeof(error_msg));
+               }
+       } else {
+               const char error_msg[] = "Failed to create log message";
+               msg_send(APP_MSG_ERROR, error_msg, sizeof(error_msg));
+       }
+}
+
+void raw_log(enum log_level level, const char *format, ...)
+{
+       va_list args;
+       va_start(args, format);
+       vraw_log(level, format, args);
+       va_end(args);
+}
index 52f9376..7ce0551 100644 (file)
@@ -33,6 +33,7 @@
 #ifndef __DAPROBE_H__
 #define __DAPROBE_H__
 
+#include <log.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
@@ -99,7 +100,7 @@ bool print_log_fmt(int msgType, const char *func_name, int line,
                   const char *format, ...);
 bool print_log_str(int msgType, char *st);
 bool printLog(log_t* log, int msgType);
-void msg_send(enum AppMessageType type, void *data, size_t len);
+void msg_send(enum AppMessageType type, const void *data, size_t len);
 
 void *rtdl_next(const char *symname);
 void *rtld_default(const char *symname);
@@ -116,13 +117,11 @@ void *rtld_default(const char *symname);
                        symbol = rtld_default(sname);   \
        } while (0)
 
-// ========================= print log =====================================
-#define PRINTMSG(...)   \
-       print_log_fmt(APP_MSG_MSG, __FUNCTION__, __LINE__, __VA_ARGS__)
-#define PRINTWRN(...)   \
-       print_log_fmt(APP_MSG_WARNING, __FUNCTION__, __LINE__, __VA_ARGS__)
-#define PRINTERR(...)   \
-       print_log_fmt(APP_MSG_ERROR, __FUNCTION__, __LINE__, __VA_ARGS__)
+// ========================== legacy log format ===============================
+#define PRINTMSG(FORMAT, ...)  LOGI(FORMAT, ##__VA_ARGS__)
+#define PRINTWRN(FORMAT, ...)  LOGW(FORMAT, ##__VA_ARGS__)
+#define PRINTERR(FORMAT, ...)  LOGE(FORMAT, ##__VA_ARGS__)
+
 
 #define INIT_INFO                                              \
                info.host_ip = 0;                               \
diff --git a/include/log.h b/include/log.h
new file mode 100644 (file)
index 0000000..d20ad35
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef LOG_H
+#define LOG_H
+
+#include <stdarg.h>
+#include <dahelper.h>  // for _getpid(), _gettid()
+
+enum log_level {
+       LL_INFO,
+       LL_WARN,
+       LL_ERROR,
+};
+
+void vraw_log(enum log_level level, const char *format, va_list args);
+void raw_log(enum log_level level, const char *format, ...);
+
+#define DO_RAW_LOG(LEVEL, FORMAT, ...) \
+       raw_log(LEVEL, "[%05d:%05d]%s:%d)" FORMAT, _getpid(), _gettid(), __func__, __LINE__, ##__VA_ARGS__)
+
+#define LOGI(FORMAT, ...) DO_RAW_LOG(LL_INFO, FORMAT, ##__VA_ARGS__)
+#define LOGW(FORMAT, ...) DO_RAW_LOG(LL_WARN, FORMAT, ##__VA_ARGS__)
+#define LOGE(FORMAT, ...) DO_RAW_LOG(LL_ERROR, FORMAT, ##__VA_ARGS__)
+
+#endif // LOG_H
index 0559cbb..d6984c1 100755 (executable)
@@ -42,9 +42,6 @@
 #include "probe_event.h"
 
 
-
-#define LOGW PRINTWRN
-
 #define MAX_INFO1_LEN 64
 
 /* START - pack for gestures */