Fix for SVACE defects
[platform/core/api/sound-pool.git] / test / logger / src / logger.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "logger.h"
18
19 #include <stdio.h>
20 #include <stdarg.h>
21 #include <string.h>
22 #include <pthread.h>
23 #include <dlog.h>
24
25 #define INFO_TAG "[ INFO ]"
26 #define WARNING_TAG "[ WARN ]"
27 #define ERROR_TAG "[ ERROR ]"
28
29 #ifndef COLORED_LOG
30         #define COLORED_LOG
31 #endif
32 #ifdef COLORED_LOG
33 #define FONT_COLOR_RESET    "\033[0m"
34 #define FONT_COLOR_RED      "\033[31m"
35 #define FONT_COLOR_GREEN    "\033[32m"
36 #define FONT_COLOR_YELLOW   "\033[33m"
37 #else
38 #define FONT_COLOR_RESET
39 #define FONT_COLOR_RED
40 #define FONT_COLOR_GREEN
41 #define FONT_COLOR_YELLOW
42 #endif
43
44 #ifdef LOG_TAG
45 #undef LOG_TAG
46 #endif
47
48 #define MAX_LOG_TAG_LEN 32
49 #define LOG_TAG "TIZEN_SOUND_POOL_TEST_SUITE"
50
51 static struct {
52         int inited;
53         int mode;
54         char fname[MAX_PATH_LEN];
55         pthread_mutex_t mutex;
56         time_t timer;
57 } __logger = { 0, LOG_MODE_NONE, {'\0'}, PTHREAD_MUTEX_INITIALIZER, 0 };
58 static char LOGGER_LOG_TAG[MAX_LOG_TAG_LEN] = LOG_TAG;
59
60 #define MAX_DATETIME_STR_LEN 26
61 static char str_time[MAX_DATETIME_STR_LEN];
62
63 void _logger_set_logging_mode(int mode)
64 {
65         pthread_mutex_lock(&__logger.mutex);
66         __logger.mode = mode;
67         pthread_mutex_unlock(&__logger.mutex);
68 }
69
70 int _logger_set_file(const char *fname)
71 {
72         if (!fname)
73                 return -1;
74
75         FILE *fd = fopen(fname, "w+");
76         if (!fd)
77                 return -1;
78
79         fclose(fd);
80
81         pthread_mutex_lock(&__logger.mutex);
82         strncpy(__logger.fname, fname, MAX_PATH_LEN - 1);
83         __logger.fname[MAX_PATH_LEN - 1] = '\0';
84         pthread_mutex_unlock(&__logger.mutex);
85
86         return 0;
87 }
88
89 int _logger_set_log_tag(const char *tag)
90 {
91         if (!tag)
92                 return -1;
93
94         pthread_mutex_lock(&__logger.mutex);
95         strncpy(LOGGER_LOG_TAG, tag, MAX_LOG_TAG_LEN - 1);
96         LOGGER_LOG_TAG[MAX_LOG_TAG_LEN - 1] = '\0';
97         pthread_mutex_unlock(&__logger.mutex);
98         return 0;
99 }
100
101 #   define __LOGGER_PREPARE()                                                  \
102                 do {                                                                   \
103                         struct tm timenew;                                                 \
104                         pthread_mutex_lock(&__logger.mutex);                               \
105                         time(&__logger.timer);                                             \
106                         localtime_r(&__logger.timer, &timenew);                            \
107                         strftime(str_time, MAX_DATETIME_STR_LEN, "%Y:%m:%d %H:%M:%S",      \
108                                          &timenew);                                                \
109                 } while (0)
110
111 #   define __LOGGER_UNPREPARE()                                                \
112                 do {                                                                   \
113                         pthread_mutex_unlock(&__logger.mutex);                             \
114                 } while (0)
115
116 int _logger_log_info(const char *fmt, ...)
117 {
118         int res = 0;
119         va_list args;
120         __LOGGER_PREPARE();
121
122         if ((__logger.mode & LOG_MODE_FILE) == LOG_MODE_FILE) {
123                 FILE *fd = fopen(__logger.fname, "a");
124                 if (!fd) {
125                         __LOGGER_UNPREPARE();
126                         return -1;
127                 }
128                 fprintf(fd, "[%s]", str_time);
129                 fprintf(fd, INFO_TAG);
130                 va_start(args, fmt);
131                 res = vfprintf(fd, fmt, args);
132                 fprintf(fd, "\n");
133                 fclose(fd);
134         }
135         if ((__logger.mode & LOG_MODE_STDERR) == LOG_MODE_STDERR) {
136                 fprintf(stderr, "[%s]", str_time);
137                 fprintf(stderr, FONT_COLOR_GREEN INFO_TAG);
138                 va_start(args, fmt);
139                 res = vfprintf(stderr, fmt, args);
140                 fprintf(stderr, FONT_COLOR_RESET);
141                 fprintf(stderr, "\n");
142         }
143         if ((__logger.mode & LOG_MODE_DLOG) == LOG_MODE_DLOG) {
144                 char msg[MAX_MSG_LEN] = { '\0' };
145                 snprintf(msg, MAX_MSG_LEN, "[%s]%s", str_time, fmt);
146                 va_start(args, fmt);
147                 dlog_vprint(DLOG_INFO, LOGGER_LOG_TAG, msg, args);
148         }
149
150         va_end(args);
151         __LOGGER_UNPREPARE();
152         return res;
153 }
154
155 int _logger_log_warn(const char *fmt, ...)
156 {
157         int res = 0;
158         va_list args;
159         __LOGGER_PREPARE();
160
161         if ((__logger.mode & LOG_MODE_FILE) == LOG_MODE_FILE) {
162                 FILE *fd = fopen(__logger.fname, "a");
163                 if (!fd) {
164                         __LOGGER_UNPREPARE();
165                         return -1;
166                 }
167                 fprintf(fd, "[%s]", str_time);
168                 fprintf(fd, WARNING_TAG);
169                 va_start(args, fmt);
170                 res = vfprintf(fd, fmt, args);
171                 fprintf(fd, "\n");
172                 fclose(fd);
173         }
174         if ((__logger.mode & LOG_MODE_STDERR) == LOG_MODE_STDERR) {
175                 fprintf(stderr, "[%s]", str_time);
176                 fprintf(stderr, FONT_COLOR_YELLOW WARNING_TAG);
177                 va_start(args, fmt);
178                 res = vfprintf(stderr, fmt, args);
179                 fprintf(stderr, FONT_COLOR_RESET);
180                 fprintf(stderr, "\n");
181         }
182         if ((__logger.mode & LOG_MODE_DLOG) == LOG_MODE_DLOG) {
183                 char msg[MAX_MSG_LEN] = { '\0' };
184                 snprintf(msg, MAX_MSG_LEN, "[%s]%s", str_time, fmt);
185                 va_start(args, fmt);
186                 dlog_vprint(DLOG_WARN, LOGGER_LOG_TAG, msg, args);
187         }
188
189         va_end(args);
190         __LOGGER_UNPREPARE();
191         return res;
192 }
193
194 int _logger_log_err(const char *fmt, ...)
195 {
196         int res = 0;
197         va_list args;
198         __LOGGER_PREPARE();
199
200         if ((__logger.mode & LOG_MODE_FILE) == LOG_MODE_FILE) {
201                 FILE *fd = fopen(__logger.fname, "a");
202                 if (!fd) {
203                         __LOGGER_UNPREPARE();
204                         return -1;
205                 }
206                 fprintf(fd, "[%s]", str_time);
207                 fprintf(fd, ERROR_TAG);
208                 va_start(args, fmt);
209                 res = vfprintf(fd, fmt, args);
210                 fprintf(fd, "\n");
211                 fclose(fd);
212         }
213         if ((__logger.mode & LOG_MODE_STDERR) == LOG_MODE_STDERR) {
214                 fprintf(stderr, "[%s]", str_time);
215                 fprintf(stderr, FONT_COLOR_RED ERROR_TAG);
216                 va_start(args, fmt);
217                 res = vfprintf(stderr, fmt, args);
218                 fprintf(stderr, FONT_COLOR_RESET);
219                 fprintf(stderr, "\n");
220         }
221         if ((__logger.mode & LOG_MODE_DLOG) == LOG_MODE_DLOG) {
222                 char msg[MAX_MSG_LEN] = { '\0' };
223                 snprintf(msg, MAX_MSG_LEN, "[%s]%s", str_time, fmt);
224                 va_start(args, fmt);
225                 dlog_vprint(DLOG_ERROR, LOGGER_LOG_TAG, msg, args);
226         }
227
228         va_end(args);
229         __LOGGER_UNPREPARE();
230         return res;
231 }
232
233 int _printf(_cmd_color_e color, const char *fmt, ...)
234 {
235         va_list args;
236         va_start(args, fmt);
237
238         switch (color) {
239         case CMD_COLOR_RED:
240                 vprintf(FONT_COLOR_RED, args);
241                 break;
242         case CMD_COLOR_YELLOW:
243                 vprintf(FONT_COLOR_YELLOW, args);
244                 break;
245         case CMD_COLOR_GREEN:
246                 vprintf(FONT_COLOR_GREEN, args);
247                 break;
248         default:
249                 break;
250         }
251
252         int res = vprintf(fmt, args);
253         printf(FONT_COLOR_RESET);
254
255         va_end(args);
256
257         return res;
258 }