Add api for widget file log 28/176328/9
authorhyunho <hhstark.kang@samsung.com>
Wed, 18 Apr 2018 10:39:02 +0000 (19:39 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Mon, 23 Apr 2018 01:55:01 +0000 (01:55 +0000)
Change-Id: Icd22a34bb266598ed3eb74bde752db739ae6732f
Signed-off-by: hyunho <hhstark.kang@samsung.com>
include/aul_widget.h
src/widget.c

index ec45fa1..2b0dd0d 100755 (executable)
@@ -138,6 +138,16 @@ int aul_widget_info_get_app_path(aul_widget_info_h info, char **app_path);
  */
 int aul_widget_instance_change_status(const char *widget_id, const char *status);
 
+/**
+ * @par Description:
+ *      Writes file log.
+ * @param[in]   tag            The log tag
+ * @param[in]   format         The log foramt
+ * @return      @c 0 on success,
+ *              otherwise a negative error value
+ */
+int aul_widget_write_log(const char *tag, const char *format, ...);
+
 #ifdef __cplusplus
 }
 #endif
index 857c032..4ee8b53 100644 (file)
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 #include <ctype.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <glib.h>
 #include <bundle.h>
 #include <bundle_internal.h>
@@ -46,6 +50,89 @@ struct widget_cb_info {
        void *user_data;
 };
 
+#define WIDGET_LOG_BUFFER_SIZE 10000
+#define WIDGET_LOG_BUFFER_STRING_SIZE 128
+
+static int __log_index;
+static int __log_fd;
+static bool __log_init = false;
+
+static int __init_log(void)
+{
+       int offset;
+       char buffer[256] = {0, };
+       char caller[255] = {0, };
+
+       aul_app_get_appid_bypid(getpid(), caller, sizeof(caller));
+       snprintf(buffer, sizeof(buffer),
+                       "/run/aul/log/widget/%d/widget_%s.log", getuid(), caller);
+       __log_fd = open(buffer, O_CREAT | O_WRONLY, 0644);
+       if (__log_fd < 0) {
+               _E("Failed to open %s - %d", buffer, errno);
+               return -1;
+       }
+
+       offset = lseek(__log_fd, 0, SEEK_END);
+       if (offset != 0) {
+               __log_index = (int)(offset / WIDGET_LOG_BUFFER_STRING_SIZE);
+               if (__log_index >= WIDGET_LOG_BUFFER_SIZE) {
+                       __log_index = 0;
+                       lseek(__log_fd, 0, SEEK_SET);
+               }
+       }
+       __log_init = true;
+
+       return 0;
+}
+
+API int aul_widget_write_log(const char *tag, const char *format, ...)
+{
+       int ret;
+       int offset;
+       time_t now;
+       char time_buf[32] = {0,};
+       char format_buffer[WIDGET_LOG_BUFFER_STRING_SIZE];
+       char buffer[WIDGET_LOG_BUFFER_STRING_SIZE];
+       va_list ap;
+
+       if (!__log_init)
+               __init_log();
+
+       if (__log_fd < 0) {
+               _E("Invalid file descriptor");
+               return -1;
+       }
+
+       time(&now);
+       ctime_r(&now, time_buf);
+       if (__log_index != 0)
+               offset = lseek(__log_fd, 0, SEEK_CUR);
+       else
+               offset = lseek(__log_fd, 0, SEEK_SET);
+
+       if (offset == -1)
+               _E("error in lseek: %d", errno);
+
+
+       va_start(ap, format);
+       vsnprintf(format_buffer, sizeof(format_buffer), format, ap);
+       va_end(ap);
+
+       snprintf(buffer, sizeof(buffer), "[%-6d][%-5d] %-15s %-50s %s",
+                       getpid(), __log_index, tag, format_buffer, time_buf);
+
+       ret = write(__log_fd, buffer, strlen(buffer));
+       if (ret < 0) {
+               _E("Cannot write the amd log: %d", ret);
+               return -1;
+       }
+
+       if (++__log_index >= WIDGET_LOG_BUFFER_SIZE)
+               __log_index = 0;
+
+       return 0;
+}
+
 static const char *__to_appid(const char *widget_id)
 {
        const char *appid;