2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
4 * This program is licensed under the terms and conditions of the
5 * Apache License, version 2.0. The full text of the Apache License is at
6 * http://www.apache.org/licenses/LICENSE-2.0
10 /*------------------------------------------------------------------------*/
16 /*------------------------------------------------------------------------*/
25 #include <sys/types.h>
29 /*============================================================================*/
31 /*============================================================================*/
32 #if defined(__GNUC__) && __GNUC__ >= 4
33 #define ICO_API __attribute__ ((visibility("default")))
38 /*========================================================================*/
42 /*========================================================================*/
44 static int time_zone = 99*60*60; /*!< local time difference(sec) */
45 static int log_level = 0x7FFFFFFF; /*!< output level debug log */
46 static bool flush_mode = true; /*!< flush mode flag */
47 static bool initialized = false; /*!< initialized flag */
48 static FILE *log_fd = NULL; /*!< file descriptor of output debug log*/
49 static int log_stdout = 0; /*!< flag for log output to stdout */
50 static int log_lines = 0; /*!< output lines */
51 static char log_prog[32] = {0,}; /*!< name of output source module */
54 /*------------------------------------------------------------------------*/
56 * @brief printout log message
58 * @param [in] level log output level
59 * @param [in] fmt message format(same as printf)
60 * @param [in] ... arguments if need
62 /*------------------------------------------------------------------------*/
64 ico_log_print(int level, const char *fmt, ...)
66 if (log_level < level) {
74 #if ICO_APF_LOG_STDOUT == 0
75 else if (log_lines >= (ICO_LOG_MAXLINES-2)) {
76 if (log_lines >= ICO_LOG_MAXLINES) {
78 ico_log_open(log_prog);
84 #endif /*ICO_APF_LOG_STDOUT*/
87 vfprintf(log_fd, fmt, list);
93 if (log_stdout == 0) {
98 /*------------------------------------------------------------------------*/
100 * @brief open log file
102 * @param [in] prog program name
104 /*------------------------------------------------------------------------*/
106 ico_log_open(const char *prog)
108 #if ICO_LOG_STDOUT == 0
112 #endif /*ICO_LOG_STDOUT*/
114 if (NULL != log_fd) {
116 if (log_stdout == 0) {
123 if ((! prog) || (*prog == 0)) {
130 strncpy(log_prog, prog, sizeof(log_prog)-1);
131 log_prog[sizeof(log_prog)-1] = 0;
133 #if ICO_LOG_STDOUT > 0
136 #else /*ICO_LOG_STDOUT*/
137 snprintf(sPath, sizeof(sPath)-1, "%s/%s.log%d",
138 ICO_LOG_DIR, log_prog, ICO_LOG_MAXFILES-1);
141 for (idx = (ICO_LOG_MAXFILES-1); idx > 0; idx--) {
142 strcpy(sPath2, sPath);
144 snprintf(sPath, sizeof(sPath)-1, "%s/%s.log%d",
145 ICO_LOG_DIR, log_prog, idx-1);
148 snprintf(sPath, sizeof(sPath)-1, "%s/%s.log",
149 ICO_LOG_DIR, log_prog);
151 (void)rename(sPath, sPath2);
154 log_fd = fopen(sPath, "w");
155 if (NULL == log_fd) {
159 else if ((initialized == false) &&
160 (log_fd != stdout) && (log_fd != stderr)) {
167 #endif /*ICO_LOG_STDOUT*/
170 /*------------------------------------------------------------------------*/
172 * @brief close log file
174 /*------------------------------------------------------------------------*/
178 #if ICO_LOG_STDOUT == 0
179 if (NULL != log_fd) {
181 if (log_stdout == 0) {
184 log_fd = (FILE *)NULL;
186 #endif /*ICO_LOG_STDOUT*/
189 /*------------------------------------------------------------------------*/
191 * @brief flush log file
193 /*------------------------------------------------------------------------*/
197 if ((NULL != log_fd) && (false == flush_mode)) {
202 /*------------------------------------------------------------------------*/
204 * @brief get current time string
206 * @param [in] level log level string(header of log message)
207 * @return current time string
209 /*------------------------------------------------------------------------*/
211 ico_get_str_cur_time(const char *level)
213 struct timeval NowTime;
214 extern long timezone;
215 static char sBuf[28];
217 gettimeofday(&NowTime, (struct timezone *)0);
218 if (time_zone > (24*60*60)) {
220 time_zone = timezone;
222 NowTime.tv_sec -= time_zone;
224 sprintf(sBuf, "%02d:%02d:%02d.%03d[%s]@%d",
225 (int)((NowTime.tv_sec/3600) % 24),
226 (int)((NowTime.tv_sec/60) % 60),
227 (int)(NowTime.tv_sec % 60),
228 (int)NowTime.tv_usec/1000, level, getpid());
233 /*------------------------------------------------------------------------*/
235 * @brief set log output level
237 * @param [in] level log output level
239 /*------------------------------------------------------------------------*/
241 ico_log_set_level(int level)
243 log_level = level & (~(ICO_LOG_FLUSH|ICO_LOG_NOFLUSH));
245 if (log_level & (ICO_LOG_FLUSH|ICO_LOG_NOFLUSH)) {
246 if (log_level & ICO_LOG_FLUSH) {
254 /* vim:set expandtab ts=4 sw=4: */