Bug fix TIVI-839 ,change the design and permit operation of the music player while...
[profile/ivi/ico-uxf-homescreen.git] / ico-app-framework / ico_apf_log.c
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
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
7  *
8  */
9 /**
10  * @brief   Application Framework debug log function
11  *
12  * @date    Feb-28-2013
13  */
14
15 #include    <stdio.h>
16 #include    <stdlib.h>
17 #include    <stdarg.h>
18 #include    <unistd.h>
19 #include    <string.h>
20 #include    <sys/time.h>
21 #include    <sys/types.h>
22 #include    <time.h>
23 #include    "ico_apf_log.h"
24 #include    "ico_apf_apimacro.h"
25
26 /* variabe & table                          */
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;
31
32 /* file descriptor of output debug log      */
33 static FILE     *sDbgFd = (FILE *)0;
34
35 /* name of output source module             */
36 static char     sDbgProg[32];
37
38 /* local time difference(sec)               */
39 static int      sTimeZone = (99*60*60);
40
41 /* output lines                             */
42 static int      sDbgLines = 0;
43
44
45 /*--------------------------------------------------------------------------*/
46 /**
47  * @brief   ico_apf_log_print: printout log message
48  *
49  * @param[in]   fmt     message format(same as printf)
50  * @param[in]   ...     arguments if need
51  * @return      nothing
52  */
53 /*--------------------------------------------------------------------------*/
54 ICO_APF_API void
55 ico_apf_log_print(const char *fmt, ...)
56 {
57     va_list     list;
58
59     if (! sDbgFd)   {
60         ico_apf_log_open(NULL);
61     }
62 #if ICO_APF_LOG_STDOUT == 0
63     else if (sDbgLines >= (ICO_APF_LOG_MAXLINES-2)) {
64         if (sDbgLines >= ICO_APF_LOG_MAXLINES)  {
65             ico_apf_log_close();
66             ico_apf_log_open(sDbgProg);
67         }
68         else    {
69             fflush(sDbgFd);
70         }
71     }
72 #endif /*ICO_APF_LOG_STDOUT*/
73     if (sDbgFd) {
74         va_start(list, fmt);
75         vfprintf(sDbgFd, fmt, list);
76         va_end(list);
77         if (ico_apf_log_flushmode)  {
78             fflush(sDbgFd);
79         }
80     }
81     if (sDbgFd != stdout)   {
82         sDbgLines ++;
83     }
84 }
85
86 /*--------------------------------------------------------------------------*/
87 /**
88  * @brief   ico_apf_log_open: open log file
89  *
90  * @param[in]   prog    program name
91  * @return      nothing
92  */
93 /*--------------------------------------------------------------------------*/
94 ICO_APF_API void
95 ico_apf_log_open(const char *prog)
96 {
97 #if ICO_APF_LOG_STDOUT == 0
98     int     idx;
99     char    sPath[128];
100     char    sPath2[128];
101 #endif /*ICO_APF_LOG_STDOUT*/
102
103     if (sDbgFd) {
104         fflush(sDbgFd);
105         if (sDbgFd != stdout)   {
106             fclose(sDbgFd);
107         }
108     }
109
110     sDbgLines = 0;
111
112     if ((! prog) || (*prog == 0))   {
113         sDbgFd = stdout;
114         sDbgProg[0] = 0;
115         return;
116     }
117     else    {
118         strncpy(sDbgProg, prog, sizeof(sDbgProg)-1);
119         sDbgProg[sizeof(sDbgProg)-1] = 0;
120     }
121 #if ICO_APF_LOG_STDOUT > 0
122     sDbgFd = stdout;
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);
127
128     for (idx = (ICO_APF_LOG_MAXFILES-1); idx > 0; idx--)    {
129         strcpy(sPath2, sPath);
130         if (idx > 1)    {
131             snprintf(sPath, sizeof(sPath)-1, "%s/%s.log%d",
132                      ICO_APF_LOG_DIR, sDbgProg, idx-1);
133         }
134         else    {
135             snprintf(sPath, sizeof(sPath)-1, "%s/%s.log",
136                      ICO_APF_LOG_DIR, sDbgProg);
137         }
138         (void) rename(sPath, sPath2);
139     }
140     sDbgFd = fopen(sPath, "w");
141     if (! sDbgFd)   {
142         sDbgFd = stdout;
143     }
144     else if ((ico_apf_log_initialized == 0) &&
145              (sDbgFd != stdout) && (sDbgFd != stderr))  {
146         ico_apf_log_initialized = 1;
147         fflush(stdout);
148         fflush(stderr);
149         stdout = sDbgFd;
150         stderr = sDbgFd;
151     }
152 #endif /*ICO_APF_LOG_STDOUT*/
153 }
154
155 /*--------------------------------------------------------------------------*/
156 /**
157  * @brief   ico_apf_log_close: close log file
158  *
159  * @param       nothing
160  * @return      nothing
161  */
162 /*--------------------------------------------------------------------------*/
163 ICO_APF_API void
164 ico_apf_log_close(void)
165 {
166 #if ICO_APF_LOG_STDOUT == 0
167     if (sDbgFd) {
168         fflush(sDbgFd);
169         if (sDbgFd != stdout)   {
170             fclose(sDbgFd);
171         }
172         sDbgFd = (FILE *)0;
173     }
174 #endif /*ICO_APF_LOG_STDOUT*/
175 }
176
177 /*--------------------------------------------------------------------------*/
178 /**
179  * @brief   ico_apf_log_flush: flush log file
180  *
181  * @param       nothing
182  * @return      nothing
183  */
184 /*--------------------------------------------------------------------------*/
185 ICO_APF_API void
186 ico_apf_log_flush(void)
187 {
188     if ((sDbgFd != NULL) && (ico_apf_log_flushmode == 0))   {
189         fflush(sDbgFd);
190     }
191 }
192
193 /*--------------------------------------------------------------------------*/
194 /**
195  * @brief   ico_apf_log_curtime: create current time string
196  *
197  * @param[in]   level   log level string(header of log message)
198  * @return      current time string
199  */
200 /*--------------------------------------------------------------------------*/
201 ICO_APF_API char *
202 ico_apf_log_curtime(const char *level)
203 {
204     struct timeval  NowTime;
205     extern long     timezone;
206     static char     sBuf[28];
207
208     gettimeofday(&NowTime, (struct timezone *)0);
209     if (sTimeZone > (24*60*60)) {
210         tzset();
211         sTimeZone = timezone;
212     }
213     NowTime.tv_sec -= sTimeZone;
214
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());
220
221     return sBuf;
222 }
223
224 /*--------------------------------------------------------------------------*/
225 /**
226  * @brief   ico_apf_log_setlevel: set log output level
227  *
228  * @param[in]   loglevel    log output level
229  * @return      nothing
230  */
231 /*--------------------------------------------------------------------------*/
232 ICO_APF_API void
233 ico_apf_log_setlevel(const int loglevel)
234 {
235     ico_apf_log_level = loglevel & (~(ICO_APF_LOG_FLUSH|ICO_APF_LOG_NOFLUSH));
236
237     if (loglevel & (ICO_APF_LOG_FLUSH|ICO_APF_LOG_NOFLUSH))    {
238         if (loglevel & ICO_APF_LOG_FLUSH)  {
239             ico_apf_log_flushmode = 1;
240         }
241         else    {
242             ico_apf_log_flushmode = 0;
243         }
244     }
245 }
246