From 12a2beab7188e29b9941003c7f455a5e17d7a280 Mon Sep 17 00:00:00 2001 From: YoungHun Kim Date: Tue, 15 Nov 2016 15:29:22 +0900 Subject: [PATCH] Update muse log not to write all messages but cache just the latest message Change-Id: I815a7758028a140687085042afb1a0e8b15433b1 --- include/muse_core_internal.h | 2 ++ include/muse_core_log.h | 8 +++-- packaging/mused.spec | 2 +- src/muse_core.c | 3 ++ src/muse_core_ipc.c | 10 +----- src/muse_core_log.c | 77 +++++++++++++++++++++++++++++++++++++------- 6 files changed, 78 insertions(+), 24 deletions(-) diff --git a/include/muse_core_internal.h b/include/muse_core_internal.h index 7359eda..f9f2418 100644 --- a/include/muse_core_internal.h +++ b/include/muse_core_internal.h @@ -40,6 +40,7 @@ extern "C" { #include #include #include +#include #include #include #include @@ -79,6 +80,7 @@ extern "C" { #define PERSIST 0x10 #define MAX_ERROR_MSG_LEN 256 #define MUSE_MAX_PARAM_STRLEN 256 + #define SOCK_ERR -1 #define DISPATCHER "dispatcher" #define DISPATCHER_PTR "dispatcher_pointer" diff --git a/include/muse_core_log.h b/include/muse_core_log.h index d4edb80..c6e1551 100644 --- a/include/muse_core_log.h +++ b/include/muse_core_log.h @@ -29,13 +29,15 @@ extern "C" { #include "muse_core_msg_json.h" #ifndef MUSE_NO_LOG -#define MUSE_LOG_MSG_CACHE_MAX_LENGTH MUSE_MSG_MAX_LENGTH * 32 +#define MUSE_LOG_LEN 32 +#define MUSE_LOG_MSG_LEN 128 +#define MUSE_LOG_MSG_NUM 10 typedef struct muse_core_log { int type; char buf[MUSE_MSG_MAX_LENGTH + 1]; size_t size; - char cache[MUSE_LOG_MSG_CACHE_MAX_LENGTH + 1]; + char cache[MUSE_MSG_MAX_LENGTH + 1]; int log_fd; void (*log)(char *); void (*log_attr)(const char *, ...); @@ -46,6 +48,8 @@ typedef struct muse_core_log { void (*log_api_name) (gpointer); void (*free) (void); GMutex log_lock; + char latest_msgs[MUSE_LOG_MSG_NUM][MUSE_LOG_MSG_LEN]; + int currnt_index; } muse_core_log_t; /*muse_core_log_init must be called before muse_core_log_get_instance*/ diff --git a/packaging/mused.spec b/packaging/mused.spec index 732e882..c10ba6c 100644 --- a/packaging/mused.spec +++ b/packaging/mused.spec @@ -1,6 +1,6 @@ Name: mused Summary: A Multimedia Daemon in Tizen Native API -Version: 0.1.30 +Version: 0.1.31 Release: 0 Group: System/Libraries License: Apache-2.0 diff --git a/src/muse_core.c b/src/muse_core.c index f41b3a0..161c1ad 100644 --- a/src/muse_core.c +++ b/src/muse_core.c @@ -199,6 +199,9 @@ static int _muse_core_free(muse_core_t *server) muse_core_module_get_instance()->free(); muse_core_ipc_get_instance()->free(); muse_core_security_get_instance()->free(); +#ifndef MUSE_NO_LOG + muse_core_log_get_instance()->free(); +#endif LOGD("Leave"); return retval; } diff --git a/src/muse_core_ipc.c b/src/muse_core_ipc.c index 763a4b9..0587ebe 100644 --- a/src/muse_core_ipc.c +++ b/src/muse_core_ipc.c @@ -110,10 +110,6 @@ static gpointer _muse_core_ipc_dispatch_worker(gpointer data) api_module = 0; module->msg_offset = 0; -#ifndef MUSE_NO_LOG - muse_core_log_get_instance()->log(module->recvMsg); -#endif - while (module->msg_offset < len) { jobj = muse_core_msg_json_object_new(module->recvMsg+module->msg_offset, &parse_len, NULL); if (jobj == NULL) { @@ -588,10 +584,6 @@ int muse_core_ipc_send_fd_msg(int sock_fd, int *fds, const char *buf) g_return_val_if_fail(buf, MM_ERROR_INVALID_ARGUMENT); -#ifndef MUSE_NO_LOG - muse_core_log_get_instance()->log((char*)buf); -#endif - memset(&iov, 0, sizeof(iov)); iov.iov_base = (void *)buf; iov.iov_len = strlen(buf); @@ -626,7 +618,7 @@ int muse_core_ipc_send_fd_msg(int sock_fd, int *fds, const char *buf) if ((ret = sendmsg(sock_fd, &msg, 0)) == SOCK_ERR) { strerror_r(errno, err_msg, MAX_ERROR_MSG_LEN); - LOGE("fail to send msg (%s)", err_msg); + LOGE("fail to send msg (%s) %d", err_msg, sock_fd); } return ret; diff --git a/src/muse_core_log.c b/src/muse_core_log.c index 8ee1f77..adaeaae 100644 --- a/src/muse_core_log.c +++ b/src/muse_core_log.c @@ -48,6 +48,7 @@ static muse_core_log_t *g_muse_core_log = NULL; static gboolean _muse_core_log_pid_is_valid(pid_t pid); +static void _muse_core_log_latest_msgs(void); static void _muse_core_log_pid_info(pid_t pid); static void _muse_core_log_cmdline(pid_t pid); static void _muse_core_log_process_info(pid_t pid); @@ -64,6 +65,7 @@ static void _muse_core_log_init_instance(void (*log)(char *), void (*log_attr)(c static void _muse_core_log_write_buffer(const void *buf, size_t len); static void _muse_core_log_attributes(const char *msg, ...); static void _muse_core_log_monitor(char *msg); +static void _muse_core_log_cache_latest_msg(char *msg); static void _muse_core_log_fatal(char *msg); static void _muse_core_log_set_msg(char *msg); static char *_muse_core_log_get_msg(void); @@ -84,6 +86,34 @@ static gboolean _muse_core_log_pid_is_valid(pid_t pid) return TRUE; } +static void _muse_core_log_latest_msgs(void) +{ + g_return_if_fail(g_muse_core_log); + int idx, start_idx; + + g_mutex_lock(&g_muse_core_log->log_lock); + + if (muse_core_ipc_fd_is_valid(g_muse_core_log->log_fd)) { + start_idx = (g_muse_core_log->currnt_index + 1) % MUSE_LOG_MSG_NUM; + + for (idx = start_idx; idx < MUSE_LOG_MSG_NUM; idx++) { + if (write(g_muse_core_log->log_fd, g_muse_core_log->latest_msgs[idx], strlen(g_muse_core_log->latest_msgs[idx])) == WRITE_FAIL) + LOGE("fail to write sequentially the latest message by first step"); + else if (write(g_muse_core_log->log_fd, "\n", 1) == WRITE_FAIL) + LOGE("fail to write new line character"); + } + + for (idx = 0; idx < start_idx; idx++) { + if (write(g_muse_core_log->log_fd, g_muse_core_log->latest_msgs[idx], strlen(g_muse_core_log->latest_msgs[idx])) == WRITE_FAIL) + LOGE("fail to write sequentially the latest message by second step"); + else if (write(g_muse_core_log->log_fd, "\n", 1) == WRITE_FAIL) + LOGE("fail to write new line character"); + } + } + + g_mutex_unlock(&g_muse_core_log->log_lock); +} + static void _muse_core_log_pid_info(pid_t pid) { char client_buf[MAX_ERROR_MSG_LEN]; @@ -95,7 +125,7 @@ static void _muse_core_log_pid_info(pid_t pid) snprintf(client_buf, sizeof(client_buf), "[PID] %d", (int)pid); if (write(g_muse_core_log->log_fd, client_buf, strlen(client_buf)) == WRITE_FAIL) LOGE("There was an error writing client pid to logfile"); - else if (write(g_muse_core_log->log_fd, "\0", 1) != WRITE_FAIL) + else if (write(g_muse_core_log->log_fd, "\n", 1) != WRITE_FAIL) LOGE("%s", client_buf); } @@ -116,7 +146,7 @@ static void _muse_core_log_cmdline(pid_t pid) if (write(g_muse_core_log->log_fd, client_buf, strlen(client_buf)) == WRITE_FAIL) LOGE("There was an error writing client name to logfile"); - else if (write(g_muse_core_log->log_fd, "\0", 1) != WRITE_FAIL) + else if (write(g_muse_core_log->log_fd, "\n", 1) != WRITE_FAIL) LOGE("[Process Name] %s", client_buf); fclose(fp); @@ -137,15 +167,15 @@ static void _muse_core_log_process_info(pid_t pid) if (fp) { if (write(g_muse_core_log->log_fd, client_buf, strlen(client_buf)) == WRITE_FAIL) LOGE("muse-server %d writing command to logfile", (int)pid); - else if (write(g_muse_core_log->log_fd, "\0", 1) == WRITE_FAIL) - LOGE("Fail to write process command"); + else if (write(g_muse_core_log->log_fd, "\n", 1) == WRITE_FAIL) + LOGE("fail to write process command"); while (fgets(client_buf, MAX_ERROR_MSG_LEN, fp)) { if (write(g_muse_core_log->log_fd, client_buf, strlen(client_buf)) == WRITE_FAIL) - LOGE("Fail to write command info to logfile"); + LOGE("fail to write command info to logfile"); } if (pclose(fp) == -1) - LOGE("Fail to pclose"); + LOGE("fail to pclose"); } } @@ -158,7 +188,7 @@ static void _muse_core_log_api_name(gpointer ptr) memset(&info, 0, sizeof(info)); if (dladdr((const void *) ptr, &info) && info.dli_sname) - _muse_core_log_monitor((char *)info.dli_sname); + _muse_core_log_cache_latest_msg((char *)info.dli_sname); else LOGE("fail to log api name"); } @@ -180,7 +210,7 @@ static void _muse_core_log_api_info(void) snprintf(client_buf, sizeof(client_buf), "[The latest called api name] %s [The latest message] %s", (char *)info.dli_sname, _muse_core_log_get_msg()); if (write(g_muse_core_log->log_fd, client_buf, strlen(client_buf)) == WRITE_FAIL) LOGE("There was an error writing client's latest called api to logfile"); - else if (write(g_muse_core_log->log_fd, "\0", 1) != WRITE_FAIL) + else if (write(g_muse_core_log->log_fd, "\n", 1) != WRITE_FAIL) LOGE("%s", client_buf); } } @@ -222,6 +252,9 @@ static void _muse_core_log_sig_abort(int signo, pid_t pid) } if (g_muse_core_log) { + /* latest api */ + _muse_core_log_latest_msgs(); + /* pid */ _muse_core_log_pid_info(pid); @@ -395,7 +428,7 @@ static void _muse_core_log_init_instance(void (*log)(char *), void (*log_attr)(c g_return_if_fail(g_muse_core_log); memset(g_muse_core_log->buf, 0, MUSE_MSG_MAX_LENGTH + 1); g_muse_core_log->size = 0; - memset(g_muse_core_log->cache, 0, MUSE_LOG_MSG_CACHE_MAX_LENGTH + 1); + memset(g_muse_core_log->cache, 0, MUSE_MSG_MAX_LENGTH + 1); g_muse_core_log->log = log; g_muse_core_log->log_attr = log_attr; g_muse_core_log->fatal = fatal; @@ -405,6 +438,7 @@ static void _muse_core_log_init_instance(void (*log)(char *), void (*log_attr)(c g_muse_core_log->log_api_name = log_api_name; g_muse_core_log->free = free; g_mutex_init(&g_muse_core_log->log_lock); + g_muse_core_log->currnt_index = 0; } static void @@ -439,11 +473,11 @@ static void _muse_core_log_monitor(char *msg) g_mutex_lock(&g_muse_core_log->log_lock); if (muse_core_ipc_fd_is_valid(g_muse_core_log->log_fd)) { - if (strlen(g_muse_core_log->cache) + strlen(msg) < MUSE_LOG_MSG_CACHE_MAX_LENGTH) { + if (strlen(g_muse_core_log->cache) + strlen(msg) < MUSE_MSG_MAX_LENGTH) { _muse_core_log_write_buffer(msg, strlen(msg)); } else { if (write(g_muse_core_log->log_fd, g_muse_core_log->cache, strlen(g_muse_core_log->cache)) != WRITE_FAIL) { - memset(g_muse_core_log->cache, 0, MUSE_LOG_MSG_CACHE_MAX_LENGTH + 1); + memset(g_muse_core_log->cache, 0, MUSE_MSG_MAX_LENGTH + 1); _muse_core_log_write_buffer(msg, strlen(msg)); } } @@ -452,6 +486,25 @@ static void _muse_core_log_monitor(char *msg) g_mutex_unlock(&g_muse_core_log->log_lock); } +static void _muse_core_log_cache_latest_msg(char *msg) +{ + g_return_if_fail(msg); + g_return_if_fail(g_muse_core_log); + + struct timeval tv; + char time_buf[MUSE_LOG_LEN]; + + g_mutex_lock(&g_muse_core_log->log_lock); + + gettimeofday(&tv, NULL); + strftime(time_buf, sizeof(time_buf), "%m-%d %H:%M:%S", localtime(&(tv.tv_sec))); + snprintf(g_muse_core_log->latest_msgs[g_muse_core_log->currnt_index], MUSE_LOG_MSG_LEN, "%s.%03ld %s ", time_buf, tv.tv_usec / 1000, msg); + + g_muse_core_log->currnt_index = (g_muse_core_log->currnt_index + 1) % MUSE_LOG_MSG_NUM; + + g_mutex_unlock(&g_muse_core_log->log_lock); +} + static void _muse_core_log_fatal(char *msg) { if (!msg) { @@ -506,7 +559,7 @@ static void _muse_core_log_flush_msg(void) LOGE("There was an error writing to logfile [%d] %s", str_len, g_muse_core_log->cache); } - memset(g_muse_core_log->cache, 0, MUSE_LOG_MSG_CACHE_MAX_LENGTH + 1); + memset(g_muse_core_log->cache, 0, MUSE_MSG_MAX_LENGTH + 1); } muse_core_log_t *muse_core_log_get_instance(void) -- 2.7.4