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 * @brief Application Framework debug log function
21 #include <sys/types.h>
23 #include "ico_apf_log.h"
24 #include "ico_apf_apimacro.h"
27 /* output level debug log */
28 ICO_APF_API int ico_apf_log_level = 0x7fffffff;
29 static int ico_apf_log_flushmode = 1;
30 static int ico_apf_log_initialized = 0;
32 /* file descriptor of output debug log */
33 static FILE *sDbgFd = (FILE *)0;
35 /* name of output source module */
36 static char sDbgProg[32];
38 /* local time difference(sec) */
39 static int sTimeZone = (99*60*60);
42 static int sDbgLines = 0;
45 /*--------------------------------------------------------------------------*/
47 * @brief ico_apf_log_print: printout log message
49 * @param[in] fmt message format(same as printf)
50 * @param[in] ... arguments if need
53 /*--------------------------------------------------------------------------*/
55 ico_apf_log_print(const char *fmt, ...)
60 ico_apf_log_open(NULL);
62 #if ICO_APF_LOG_STDOUT == 0
63 else if (sDbgLines >= (ICO_APF_LOG_MAXLINES-2)) {
64 if (sDbgLines >= ICO_APF_LOG_MAXLINES) {
66 ico_apf_log_open(sDbgProg);
72 #endif /*ICO_APF_LOG_STDOUT*/
75 vfprintf(sDbgFd, fmt, list);
77 if (ico_apf_log_flushmode) {
81 if (sDbgFd != stdout) {
86 /*--------------------------------------------------------------------------*/
88 * @brief ico_apf_log_open: open log file
90 * @param[in] prog program name
93 /*--------------------------------------------------------------------------*/
95 ico_apf_log_open(const char *prog)
97 #if ICO_APF_LOG_STDOUT == 0
101 #endif /*ICO_APF_LOG_STDOUT*/
105 if (sDbgFd != stdout) {
112 if ((! prog) || (*prog == 0)) {
118 strncpy(sDbgProg, prog, sizeof(sDbgProg)-1);
119 sDbgProg[sizeof(sDbgProg)-1] = 0;
121 #if ICO_APF_LOG_STDOUT > 0
123 #else /*ICO_APF_LOG_STDOUT*/
124 snprintf(sPath, sizeof(sPath)-1, "%s/%s.log%d",
125 ICO_APF_LOG_DIR, sDbgProg, ICO_APF_LOG_MAXFILES-1);
126 (void) remove(sPath);
128 for (idx = (ICO_APF_LOG_MAXFILES-1); idx > 0; idx--) {
129 strcpy(sPath2, sPath);
131 snprintf(sPath, sizeof(sPath)-1, "%s/%s.log%d",
132 ICO_APF_LOG_DIR, sDbgProg, idx-1);
135 snprintf(sPath, sizeof(sPath)-1, "%s/%s.log",
136 ICO_APF_LOG_DIR, sDbgProg);
138 (void) rename(sPath, sPath2);
140 sDbgFd = fopen(sPath, "w");
144 else if ((ico_apf_log_initialized == 0) &&
145 (sDbgFd != stdout) && (sDbgFd != stderr)) {
146 ico_apf_log_initialized = 1;
152 #endif /*ICO_APF_LOG_STDOUT*/
155 /*--------------------------------------------------------------------------*/
157 * @brief ico_apf_log_close: close log file
162 /*--------------------------------------------------------------------------*/
164 ico_apf_log_close(void)
166 #if ICO_APF_LOG_STDOUT == 0
169 if (sDbgFd != stdout) {
174 #endif /*ICO_APF_LOG_STDOUT*/
177 /*--------------------------------------------------------------------------*/
179 * @brief ico_apf_log_flush: flush log file
184 /*--------------------------------------------------------------------------*/
186 ico_apf_log_flush(void)
188 if ((sDbgFd != NULL) && (ico_apf_log_flushmode == 0)) {
193 /*--------------------------------------------------------------------------*/
195 * @brief ico_apf_log_curtime: create current time string
197 * @param[in] level log level string(header of log message)
198 * @return current time string
200 /*--------------------------------------------------------------------------*/
202 ico_apf_log_curtime(const char *level)
204 struct timeval NowTime;
205 extern long timezone;
206 static char sBuf[28];
208 gettimeofday(&NowTime, (struct timezone *)0);
209 if (sTimeZone > (24*60*60)) {
211 sTimeZone = timezone;
213 NowTime.tv_sec -= sTimeZone;
215 sprintf(sBuf, "%02d:%02d:%02d.%03d[%s]@%d",
216 (int)((NowTime.tv_sec/3600) % 24),
217 (int)((NowTime.tv_sec/60) % 60),
218 (int)(NowTime.tv_sec % 60),
219 (int)NowTime.tv_usec/1000, level, getpid());
224 /*--------------------------------------------------------------------------*/
226 * @brief ico_apf_log_setlevel: set log output level
228 * @param[in] loglevel log output level
231 /*--------------------------------------------------------------------------*/
233 ico_apf_log_setlevel(const int loglevel)
235 ico_apf_log_level = loglevel & (~(ICO_APF_LOG_FLUSH|ICO_APF_LOG_NOFLUSH));
237 if (loglevel & (ICO_APF_LOG_FLUSH|ICO_APF_LOG_NOFLUSH)) {
238 if (loglevel & ICO_APF_LOG_FLUSH) {
239 ico_apf_log_flushmode = 1;
242 ico_apf_log_flushmode = 0;