diff options
author | hyunho <hhstark.kang@samsung.com> | 2018-04-18 19:39:02 +0900 |
---|---|---|
committer | HwanKyu Jhun <h.jhun@samsung.com> | 2018-04-23 01:55:01 +0000 |
commit | ac06e4acbebfd05a03fbed3fad4995b2d8e1f48d (patch) | |
tree | 461ef1f806a964f4d215e62c03a11cf55ccd90e9 | |
parent | b7a5312a3b3916e510b41273f27eb38e289b3af8 (diff) | |
download | aul-1-ac06e4acbebfd05a03fbed3fad4995b2d8e1f48d.tar.gz aul-1-ac06e4acbebfd05a03fbed3fad4995b2d8e1f48d.tar.bz2 aul-1-ac06e4acbebfd05a03fbed3fad4995b2d8e1f48d.zip |
Add api for widget file log
Change-Id: Icd22a34bb266598ed3eb74bde752db739ae6732f
Signed-off-by: hyunho <hhstark.kang@samsung.com>
-rwxr-xr-x | include/aul_widget.h | 10 | ||||
-rw-r--r-- | src/widget.c | 87 |
2 files changed, 97 insertions, 0 deletions
diff --git a/include/aul_widget.h b/include/aul_widget.h index ec45fa15..2b0dd0da 100755 --- a/include/aul_widget.h +++ b/include/aul_widget.h @@ -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 diff --git a/src/widget.c b/src/widget.c index 857c032f..4ee8b539 100644 --- a/src/widget.c +++ b/src/widget.c @@ -17,8 +17,12 @@ #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; |