From: Jihee Choi Date: Wed, 4 Apr 2018 10:34:53 +0000 (+0900) Subject: Add log files X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4a74827dbdc2a3bea47e9adbe7729abecce7e1cd;p=apps%2Fnative%2Fttsd-worker-package.git Add log files --- diff --git a/CMakeLists.txt b/CMakeLists.txt index e51eadd..db2d41b 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie") INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/inc) SET(SRCS + src/log.c src/package-manager-worker.c ) ADD_EXECUTABLE(${PROJECT_NAME} ${SRCS}) diff --git a/inc/log.h b/inc/log.h new file mode 100644 index 0000000..d5a9d56 --- /dev/null +++ b/inc/log.h @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __PACKAGE_MANAGER_WORKER_LOG_H__ +#define __PACKAGE_MANAGER_WORKER_LOG_H__ + +#include + +#ifdef LOG_TAG +#undef LOG_TAG +#endif +#define LOG_TAG "PKG_MANAGER_WORKER" + +#if !defined(_V) +#define _V(fmt, arg...) log_print(DLOG_VERBOSE, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg) +#endif + +#if !defined(_D) +#define _D(fmt, arg...) log_print(DLOG_DEBUG, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg) +#endif + +#if !defined(_I) +#define _I(fmt, arg...) log_print(DLOG_INFO, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg) +#endif + +#if !defined(_W) +#define _W(fmt, arg...) log_print(DLOG_WARN, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg) +#endif + +#if !defined(_E) +#define _E(fmt, arg...) log_print(DLOG_ERROR, LOG_TAG, "[%s:%d] " fmt "\n", __func__, __LINE__, ##arg) +#endif + +#define retvm_if(expr, val, fmt, arg...) do { \ + if (expr) { \ + _E(fmt, ##arg); \ + _E("(%s) -> %s() return", #expr, __FUNCTION__); \ + return val; \ + } \ +} while (0) + +#define retv_if(expr, val) do { \ + if (expr) { \ + _E("(%s) -> %s() return", #expr, __FUNCTION__); \ + return (val); \ + } \ +} while (0) + +#define retm_if(expr, fmt, arg...) do { \ + if (expr) { \ + _E(fmt, ##arg); \ + _E("(%s) -> %s() return", #expr, __FUNCTION__); \ + return; \ + } \ +} while (0) + +#define ret_if(expr) do { \ + if (expr) { \ + _E("(%s) -> %s() return", #expr, __FUNCTION__); \ + return; \ + } \ +} while (0) + +#define goto_if(expr, val) do { \ + if (expr) { \ + _E("(%s) -> goto", #expr); \ + goto val; \ + } \ +} while (0) + +#define break_if(expr) { \ + if (expr) { \ + _E("(%s) -> break", #expr); \ + break; \ + } \ +} + +#define continue_if(expr) { \ + if (expr) { \ + _E("(%s) -> continue", #expr); \ + continue; \ + } \ +} + +typedef enum { + LOG_TYPE_DLOG = 0, + LOG_TYPE_FILE, + LOG_TYPE_ALL, +} log_type; + +int log_print(log_priority prio, const char *tag, const char *fmt, ...); +int log_type_set(log_type type); +void log_file_close(void); + +#endif /* __PACKAGE_MANAGER_WORKER_LOG_H__*/ diff --git a/inc/package-manager-worker.h b/inc/package-manager-worker.h index 878e965..5443ae2 100755 --- a/inc/package-manager-worker.h +++ b/inc/package-manager-worker.h @@ -1,12 +1,6 @@ #ifndef __PACKAGE_MANAGER_WORKER_H__ #define __PACKAGE_MANAGER_WORKER_H__ -#include - -#ifdef LOG_TAG -#undef LOG_TAG -#endif -#define LOG_TAG "package-manager-worker" #endif /* __PACKAGE_MANAGER_WORKER_H__ */ diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..0856a3b --- /dev/null +++ b/src/log.c @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. + * + * Licensed under the Flora License, Version 1.1 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://floralicense.org/license/ + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include "log.h" + +static FILE *log_fp = NULL; +static log_type ltype = LOG_TYPE_DLOG; + +static const char log_prio_name[][DLOG_PRIO_MAX-1] = { + "UNKNOWN", + "DEFAULT", /**< Default */ + "VERBOSE", /**< Verbose */ + "DEBUG", /**< Debug */ + "INFO", /**< Info */ + "WARN", /**< Warning */ + "ERROR", /**< Error */ + "FATAL", /**< Fatal */ + "SILENT" /**< Silent */ +}; + +static inline char* getFormattedTime(void) +{ + struct timespec time_s; + struct tm *ptm; + static char res_time[40] = {0, }; + + if (0 == clock_gettime(CLOCK_REALTIME, &time_s)) { + ptm = localtime((const time_t *)&time_s.tv_sec); + + // format : YY-MM-DD hh:mm:ss:uuuuuu + snprintf(res_time, sizeof(res_time), + "%04d-%02d-%02d %02d:%02d:%02d:%06ld" + , ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday + , ptm->tm_hour, ptm->tm_min, ptm->tm_sec + , time_s.tv_nsec); + } + + return res_time; +} + +static int __open_log_file(void) +{ + log_fp = fopen("/var/log/ttd.log", "a"); + /* TODO : splits log file, if log file size is exceeded specified max size */ + if (log_fp == NULL) { + dlog_print(DLOG_WARN, LOG_TAG, "%s\n", strerror(errno)); + goto error; + } + + return 0; + +error: + dlog_print(DLOG_WARN, LOG_TAG, + "error to use log file, so use dlog as log system\n"); + ltype = LOG_TYPE_DLOG; + + return -1; +} + +int log_type_set(log_type type) +{ + ltype = type; + dlog_print(DLOG_DEBUG, LOG_TAG, "setting log type : [%d]\n", type); + switch (type) { + case LOG_TYPE_FILE: + case LOG_TYPE_ALL: + __open_log_file(); + break; + case LOG_TYPE_DLOG: /* nothing to do */ + default: + ltype = LOG_TYPE_DLOG; + break; + } + return 0; +} + +void log_file_close(void) +{ + if (log_fp) { + fclose(log_fp); + log_fp = NULL; + dlog_print(DLOG_DEBUG, LOG_TAG, "close log file\n"); + } + + log_type_set(LOG_TYPE_DLOG); + + return; +} + +int log_print(log_priority prio, const char *tag, const char *fmt, ...) +{ + va_list ap; + + switch (ltype) { + case LOG_TYPE_FILE: + if (log_fp) { + va_start(ap, fmt); + fprintf(log_fp, "[%s] [%s]", getFormattedTime(), log_prio_name[prio]); + vfprintf(log_fp, fmt, ap); + va_end(ap); + fflush(log_fp); + } + break; + case LOG_TYPE_ALL: + va_start(ap, fmt); + if (log_fp) { + fprintf(log_fp, "[%s] [%s]", getFormattedTime(), log_prio_name[prio]); + vfprintf(log_fp, fmt, ap); + } + dlog_vprint(prio, tag, fmt, ap); + va_end(ap); + + if (log_fp) + fflush(log_fp); + break; + case LOG_TYPE_DLOG: + default: + va_start(ap, fmt); + dlog_vprint(prio, tag, fmt, ap); + va_end(ap); + break; + } + return 0; +} diff --git a/src/package-manager-worker.c b/src/package-manager-worker.c index 682bb14..91600b5 100755 --- a/src/package-manager-worker.c +++ b/src/package-manager-worker.c @@ -1,10 +1,12 @@ #include #include #include "package-manager-worker.h" +#include "log.h" bool service_app_create(void *data) { // Todo: add your code here. + _D("Package manager worker created"); return true; }