2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #define INFO_TAG "[ INFO ]"
26 #define WARNING_TAG "[ WARN ]"
27 #define ERROR_TAG "[ ERROR ]"
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"
38 #define FONT_COLOR_RESET
39 #define FONT_COLOR_RED
40 #define FONT_COLOR_GREEN
41 #define FONT_COLOR_YELLOW
48 #define MAX_LOG_TAG_LEN 32
49 #define LOG_TAG "TIZEN_SOUND_POOL_TEST_SUITE"
54 char fname[MAX_PATH_LEN];
55 pthread_mutex_t mutex;
57 } __logger = { 0, LOG_MODE_NONE, {'\0'}, PTHREAD_MUTEX_INITIALIZER, 0 };
58 static char LOGGER_LOG_TAG[MAX_LOG_TAG_LEN] = LOG_TAG;
60 #define MAX_DATETIME_STR_LEN 26
61 static char str_time[MAX_DATETIME_STR_LEN];
63 void _logger_set_logging_mode(int mode)
65 pthread_mutex_lock(&__logger.mutex);
67 pthread_mutex_unlock(&__logger.mutex);
70 int _logger_set_file(const char *fname)
75 FILE *fd = fopen(fname, "w+");
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);
89 int _logger_set_log_tag(const char *tag)
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);
101 # define __LOGGER_PREPARE() \
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", \
111 # define __LOGGER_UNPREPARE() \
113 pthread_mutex_unlock(&__logger.mutex); \
116 int _logger_log_info(const char *fmt, ...)
122 if ((__logger.mode & LOG_MODE_FILE) == LOG_MODE_FILE) {
123 FILE *fd = fopen(__logger.fname, "a");
125 __LOGGER_UNPREPARE();
128 fprintf(fd, "[%s]", str_time);
129 fprintf(fd, INFO_TAG);
131 res = vfprintf(fd, fmt, args);
135 if ((__logger.mode & LOG_MODE_STDERR) == LOG_MODE_STDERR) {
136 fprintf(stderr, "[%s]", str_time);
137 fprintf(stderr, FONT_COLOR_GREEN INFO_TAG);
139 res = vfprintf(stderr, fmt, args);
140 fprintf(stderr, FONT_COLOR_RESET);
141 fprintf(stderr, "\n");
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);
147 dlog_vprint(DLOG_INFO, LOGGER_LOG_TAG, msg, args);
151 __LOGGER_UNPREPARE();
155 int _logger_log_warn(const char *fmt, ...)
161 if ((__logger.mode & LOG_MODE_FILE) == LOG_MODE_FILE) {
162 FILE *fd = fopen(__logger.fname, "a");
164 __LOGGER_UNPREPARE();
167 fprintf(fd, "[%s]", str_time);
168 fprintf(fd, WARNING_TAG);
170 res = vfprintf(fd, fmt, args);
174 if ((__logger.mode & LOG_MODE_STDERR) == LOG_MODE_STDERR) {
175 fprintf(stderr, "[%s]", str_time);
176 fprintf(stderr, FONT_COLOR_YELLOW WARNING_TAG);
178 res = vfprintf(stderr, fmt, args);
179 fprintf(stderr, FONT_COLOR_RESET);
180 fprintf(stderr, "\n");
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);
186 dlog_vprint(DLOG_WARN, LOGGER_LOG_TAG, msg, args);
190 __LOGGER_UNPREPARE();
194 int _logger_log_err(const char *fmt, ...)
200 if ((__logger.mode & LOG_MODE_FILE) == LOG_MODE_FILE) {
201 FILE *fd = fopen(__logger.fname, "a");
203 __LOGGER_UNPREPARE();
206 fprintf(fd, "[%s]", str_time);
207 fprintf(fd, ERROR_TAG);
209 res = vfprintf(fd, fmt, args);
213 if ((__logger.mode & LOG_MODE_STDERR) == LOG_MODE_STDERR) {
214 fprintf(stderr, "[%s]", str_time);
215 fprintf(stderr, FONT_COLOR_RED ERROR_TAG);
217 res = vfprintf(stderr, fmt, args);
218 fprintf(stderr, FONT_COLOR_RESET);
219 fprintf(stderr, "\n");
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);
225 dlog_vprint(DLOG_ERROR, LOGGER_LOG_TAG, msg, args);
229 __LOGGER_UNPREPARE();
233 int _printf(_cmd_color_e color, const char *fmt, ...)
240 vprintf(FONT_COLOR_RED, args);
242 case CMD_COLOR_YELLOW:
243 vprintf(FONT_COLOR_YELLOW, args);
245 case CMD_COLOR_GREEN:
246 vprintf(FONT_COLOR_GREEN, args);
252 int res = vprintf(fmt, args);
253 printf(FONT_COLOR_RESET);