From: Hayato Nakamura Date: Thu, 19 Sep 2013 10:42:23 +0000 (+0900) Subject: Addition of the log output function that application is available. X-Git-Tag: accepted/tizen/20130919.181034^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d483fa4ef0f2610872744121f675ac185f8ab8a2;p=profile%2Fivi%2Fico-uxf-utilities.git Addition of the log output function that application is available. Change-Id: I24befb8e50b23af61b2f944a7ed304d13a67bcd5 Signed-off-by: Hayato Nakamura --- diff --git a/include/ico_log.h b/include/ico_log.h new file mode 100644 index 0000000..3b5e224 --- /dev/null +++ b/include/ico_log.h @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2013, TOYOTA MOTOR CORPORATION. + * + * This program is licensed under the terms and conditions of the + * Apache License, version 2.0. The full text of the Apache License is at + * http://www.apache.org/licenses/LICENSE-2.0 + * + */ + +/*========================================================================*/ +/** + * @file ico_log.h + * + * @brief debug log function + */ +/*========================================================================*/ +#ifndef __ICO_LOG_H__ +#define __ICO_LOG_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef ICO_LOG_STDOUT +#define ICO_LOG_STDOUT 0 + /**!< Log output to stdout(=1) or file(=0) */ +#endif /*ICO_APF_LOG_STDOUT*/ + +#define ICO_LOG_DIR "/var/log/ico" + /**!< Directory name of Log destination */ + +/* Maximum lines/files */ +#define ICO_LOG_MAXLINES 10000 + /**!< Maximum output lines of log file */ +#define ICO_LOG_MAXFILES 6 + /**!< Maximum number of the log file */ + +/* Log output level */ +#define ICO_LOG_DEBUG 0x0080 /**!< Debug write */ +#define ICO_LOG_INFO 0x0040 /**!< Information */ +#define ICO_LOG_WARNING 0x0010 /**!< Warning */ +#define ICO_LOG_CRITICAL 0x0008 /**!< Critical */ +#define ICO_LOG_ERROR 0x0004 /**!< Error */ + +/* Log output flush */ +#define ICO_LOG_FLUSH 0x4000 /**!< Log outout with log flush */ +#define ICO_LOG_NOFLUSH 0x2000 /**!< Log output without log flush */ + +#define ICO_TRA(fmt,...) \ +{ \ + ico_log_print(ICO_LOG_DEBUG, \ + "%s> " fmt " (%s,%s:%d)\n", \ + ico_get_str_cur_time("DBG"), \ + ##__VA_ARGS__, \ + __func__, \ + __FILE__, \ + __LINE__); \ +} + +#define ICO_DBG(fmt,...) \ +{ \ + ico_log_print(ICO_LOG_DEBUG, \ + "%s> " fmt " (%s,%s:%d)\n", \ + ico_get_str_cur_time("DBG"), \ + ##__VA_ARGS__, \ + __func__, \ + __FILE__, \ + __LINE__); \ +} + +#define ICO_INF(fmt,...) \ +{ \ + ico_log_print(ICO_LOG_INFO, \ + "%s> " fmt " (%s,%s:%d)\n", \ + ico_get_str_cur_time("INF"), \ + ##__VA_ARGS__, \ + __func__, \ + __FILE__, \ + __LINE__); \ +} + +#define ICO_WRN(fmt,...) \ +{ \ + ico_log_print(ICO_LOG_WARNING, \ + "%s> " fmt " (%s,%s:%d)\n", \ + ico_get_str_cur_time("WRN"), \ + ##__VA_ARGS__, \ + __func__, \ + __FILE__, \ + __LINE__); \ +} + +#define ICO_CRI(fmt,...) \ +{ \ + ico_log_print(ICO_LOG_CRITICAL, \ + "%s> " fmt " (%s,%s:%d)\n", \ + ico_get_str_cur_time("CRI"), \ + ##__VA_ARGS__, \ + __func__, \ + __FILE__, \ + __LINE__); \ +} + +#define ICO_ERR(fmt,...) \ +{ \ + ico_log_print(ICO_LOG_ERROR, \ + "%s> " fmt " (%s,%s:%d)\n", \ + ico_get_str_cur_time("ERR"), \ + ##__VA_ARGS__, \ + __func__, \ + __FILE__, \ + __LINE__); \ +} + + +void ico_log_print(int level, const char *fmt, ...); +void ico_log_open(const char *prog); +void ico_log_close(void); +void ico_log_flush(void); +char * ico_get_str_cur_time(const char *level); +void ico_log_set_level(int level); + +#ifdef __cplusplus +} +#endif + +#endif // __ICO__LOG_H__ +/* vim:set expandtab ts=4 sw=4: */ diff --git a/packaging/ico-uxf-utilities.changes b/packaging/ico-uxf-utilities.changes index fa509e9..f4cc13c 100644 --- a/packaging/ico-uxf-utilities.changes +++ b/packaging/ico-uxf-utilities.changes @@ -1,3 +1,8 @@ +* Thu Sep 19 2013 Shibata Makoto accepted/tizen/20130911.005417@250001b +- 0.2.04 release. +-- Addition of the log output function that application is available. +-- Changes the library name from libico-util-com to libico-util. + * Tue Aug 27 2013 Shibata Makoto submit/tizen/20130823.164921@fba3847 - 0.2.03 release. -- fix for - An error correction of the version number of the library. diff --git a/packaging/ico-uxf-utilities.spec b/packaging/ico-uxf-utilities.spec index 85ac241..faaaf4f 100644 --- a/packaging/ico-uxf-utilities.spec +++ b/packaging/ico-uxf-utilities.spec @@ -1,6 +1,6 @@ Name: ico-uxf-utilities Summary: Common utilities for ico uifw -Version: 0.2.03 +Version: 0.2.04 Release: 1.1 Group: Automotive/Libraries License: Apache-2.0 @@ -38,16 +38,21 @@ make %{?_smp_mflags} # include mkdir -p %{buildroot}/%{_includedir}/ico-util/ cp -f include/ico_uws.h %{buildroot}/%{_includedir}/ico-util/ +cp -f include/ico_log.h %{buildroot}/%{_includedir}/ico-util/ -%post -p /sbin/ldconfig +%post +/sbin/ldconfig +mkdir -p %{_localstatedir}/log/ico/ +chmod 0777 %{_localstatedir}/log/ico/ %postun -p /sbin/ldconfig %files %license LICENSE-2.0 -%{_libdir}/*.so.* +%{_libdir}/libico-util* %files devel %defattr(-,root,root,-) %{_includedir}/ico-util/ico_uws.h -%{_libdir}/*.so +%{_includedir}/ico-util/ico_log.h +%{_libdir}/libico-util* diff --git a/src/Makefile.am b/src/Makefile.am index 764769e..1d43c61 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,10 +1,11 @@ -lib_LTLIBRARIES = libico-util-com.la +lib_LTLIBRARIES = libico-util.la -libico_util_com_la_CFLAGS = -I../include $(OPT_CFLAGS) -libico_util_com_la_LIBADD = $(OPT_LIBS) -lwebsockets -libico_util_com_la_LDFLAGS = -version-info 0:2:0 -libico_util_com_la_SOURCES = \ - ico_uws.c +libico_util_la_CFLAGS = -I../include $(OPT_CFLAGS) +libico_util_la_LIBADD = $(OPT_LIBS) -lwebsockets +libico_util_la_LDFLAGS = -version-info 0:2:0 +libico_util_la_SOURCES = \ + ico_uws.c \ + ico_log.c AM_CFLAGS = $(GCC_CFLAGS) AM_CPPFLAGS = $(GCC_CFLAGS) \ diff --git a/src/ico_log.c b/src/ico_log.c new file mode 100644 index 0000000..0ec65c0 --- /dev/null +++ b/src/ico_log.c @@ -0,0 +1,250 @@ +/* + * Copyright (c) 2013, TOYOTA MOTOR CORPORATION. + * + * This program is licensed under the terms and conditions of the + * Apache License, version 2.0. The full text of the Apache License is at + * http://www.apache.org/licenses/LICENSE-2.0 + * + */ + +/*------------------------------------------------------------------------*/ +/** + * @file ico_log.c + * + * @brief + */ +/*------------------------------------------------------------------------*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ico_log.h" + +/*============================================================================*/ +/* global API */ +/*============================================================================*/ +#if defined(__GNUC__) && __GNUC__ >= 4 +#define ICO_API __attribute__ ((visibility("default"))) +#else +#define ICO_API +#endif + +/*========================================================================*/ +/** + * static variable + */ +/*========================================================================*/ + +static int time_zone = 99*60*60; /*!< local time difference(sec) */ +static int log_level = 0x7FFFFFFF; /*!< output level debug log */ +static bool flush_mode = true; /*!< flush mode flag */ +static bool initialized = false; /*!< initialized flag */ +static FILE *log_fd = NULL; /*!< file descriptor of output debug log*/ +static int log_lines = 0; /*!< output lines */ +static char log_prog[32] = {0,}; /*!< name of output source module */ + + +/*------------------------------------------------------------------------*/ +/** + * @brief printout log message + * + * @param [in] level log output level + * @param [in] fmt message format(same as printf) + * @param [in] ... arguments if need + */ +/*------------------------------------------------------------------------*/ +ICO_API void +ico_log_print(int level, const char *fmt, ...) +{ + if (log_level < level) { + return; + } + va_list list; + + if (NULL == log_fd) { + ico_log_open(NULL); + } +#if ICO_APF_LOG_STDOUT == 0 + else if (log_lines >= (ICO_LOG_MAXLINES-2)) { + if (log_lines >= ICO_LOG_MAXLINES) { + ico_log_close(); + ico_log_open(log_prog); + } + else { + fflush(log_fd); + } + } +#endif /*ICO_APF_LOG_STDOUT*/ + if (NULL != log_fd) { + va_start(list, fmt); + vfprintf(log_fd, fmt, list); + va_end(list); + if (flush_mode) { + fflush(log_fd); + } + } + if (log_fd != stdout) { + log_lines ++; + } +} + +/*------------------------------------------------------------------------*/ +/** + * @brief open log file + * + * @param [in] prog program name + */ +/*------------------------------------------------------------------------*/ +ICO_API void +ico_log_open(const char *prog) +{ +#if ICO_LOG_STDOUT == 0 + int idx; + char sPath[128]; + char sPath2[128]; +#endif /*ICO_LOG_STDOUT*/ + + if (NULL != log_fd) { + fflush(log_fd); + if (log_fd != stdout) { + fclose(log_fd); + } + } + + log_lines = 0; + + if ((! prog) || (*prog == 0)) { + log_fd = stdout; + log_prog[0] = 0; + return; + } + else { + strncpy(log_prog, prog, sizeof(log_prog)-1); + log_prog[sizeof(log_prog)-1] = 0; + } +#if ICO_LOG_STDOUT > 0 + log_fd = stdout; +#else /*ICO_LOG_STDOUT*/ + snprintf(sPath, sizeof(sPath)-1, "%s/%s.log%d", + ICO_LOG_DIR, log_prog, ICO_LOG_MAXFILES-1); + (void)remove(sPath); + + for (idx = (ICO_LOG_MAXFILES-1); idx > 0; idx--) { + strcpy(sPath2, sPath); + if (idx > 1) { + snprintf(sPath, sizeof(sPath)-1, "%s/%s.log%d", + ICO_LOG_DIR, log_prog, idx-1); + } + else { + snprintf(sPath, sizeof(sPath)-1, "%s/%s.log", + ICO_LOG_DIR, log_prog); + } + (void)rename(sPath, sPath2); + } + + log_fd = fopen(sPath, "w"); + if (NULL == log_fd) { + log_fd = stdout; + } + else if ((initialized == false) && + (log_fd != stdout) && (log_fd != stderr)) { + initialized = true; + fflush(stdout); + fflush(stderr); + stdout = log_fd; + stderr = log_fd; + } +#endif /*ICO_LOG_STDOUT*/ +} + +/*------------------------------------------------------------------------*/ +/** + * @brief close log file + */ +/*------------------------------------------------------------------------*/ +ICO_API void +ico_log_close(void) +{ +#if ICO_LOG_STDOUT == 0 + if (NULL != log_fd) { + fflush(log_fd); + if (log_fd != stdout) { + fclose(log_fd); + } + log_fd = (FILE *)NULL; + } +#endif /*ICO_LOG_STDOUT*/ +} + +/*------------------------------------------------------------------------*/ +/** + * @brief flush log file + */ +/*------------------------------------------------------------------------*/ +ICO_API void +ico_log_flush(void) +{ + if ((NULL != log_fd) && (false == flush_mode)) { + fflush(log_fd); + } +} + +/*------------------------------------------------------------------------*/ +/** + * @brief get current time string + * + * @param [in] level log level string(header of log message) + * @return current time string + */ +/*------------------------------------------------------------------------*/ +ICO_API char * +ico_get_str_cur_time(const char *level) +{ + struct timeval NowTime; + extern long timezone; + static char sBuf[28]; + + gettimeofday(&NowTime, (struct timezone *)0); + if (time_zone > (24*60*60)) { + tzset(); + time_zone = timezone; + } + NowTime.tv_sec -= time_zone; + + sprintf(sBuf, "%02d:%02d:%02d.%03d[%s]@%d", + (int)((NowTime.tv_sec/3600) % 24), + (int)((NowTime.tv_sec/60) % 60), + (int)(NowTime.tv_sec % 60), + (int)NowTime.tv_usec/1000, level, getpid()); + + return sBuf; +} + +/*------------------------------------------------------------------------*/ +/** + * @brief set log output level + * + * @param [in] level log output level + */ +/*------------------------------------------------------------------------*/ +ICO_API void +ico_log_set_level(int level) +{ + log_level = level & (~(ICO_LOG_FLUSH|ICO_LOG_NOFLUSH)); + + if (log_level & (ICO_LOG_FLUSH|ICO_LOG_NOFLUSH)) { + if (log_level & ICO_LOG_FLUSH) { + flush_mode = true; + } + else { + flush_mode = false; + } + } +} +/* vim:set expandtab ts=4 sw=4: */ diff --git a/test/Makefile.am b/test/Makefile.am index 45f88b6..9f99442 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -15,7 +15,7 @@ check_LTLIBRARIES = $(TESTS) check_PROGRAMS = tst_ico_uws_client tst_ico_uws_server tst_ico_uws_multi_client tst_ico_uws_multi_server test_common_lib = -lwebsockets -test_target_lib = ../src/.libs/libico-util-com.so +test_target_lib = ../src/.libs/libico-util.so tst_ico_uws_client_SOURCES = tst_ico_uws_client.c tst_ico_uws_client_CFLAGS = -I../include $(OPT_CFLAGS)