2 debug.c: log (or not) messages
3 Copyright (C) 2003-2011 Ludovic Rousseau
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this library; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include "strlcpycat.h"
39 #define LOG_STREAM stderr
41 #define LOG_STREAM stdout
46 void log_msg(const int priority, const char *fmt, ...)
48 char debug_buffer[3 * 80]; /* up to 3 lines of 80 characters */
54 case PCSC_LOG_CRITICAL:
55 os_log_type = OS_LOG_TYPE_FAULT;
58 os_log_type = OS_LOG_TYPE_ERROR;
61 os_log_type = OS_LOG_TYPE_INFO;
64 os_log_type = OS_LOG_TYPE_DEBUG;
67 va_start(argptr, fmt);
68 (void)vsnprintf(debug_buffer, sizeof debug_buffer, fmt, argptr);
71 os_log_with_type(OS_LOG_DEFAULT, os_log_type, "%s", debug_buffer);
74 void log_xxd(const int priority, const char *msg, const unsigned char *buffer,
78 char *c, debug_buffer[len*3 + strlen(msg) +1];
83 l = strlcpy(debug_buffer, msg, sizeof debug_buffer);
86 for (i = 0; i < len; ++i)
88 /* 2 hex characters, 1 space, 1 NUL : total 4 characters */
89 (void)snprintf(c, 4, "%02X ", buffer[i]);
93 os_log(OS_LOG_DEFAULT, "%s", debug_buffer);
98 void log_msg(const int priority, const char *fmt, ...)
100 char debug_buffer[3 * 80]; /* up to 3 lines of 80 characters */
102 static struct timeval last_time = { 0, 0 };
103 struct timeval new_time = { 0, 0 };
111 case PCSC_LOG_CRITICAL:
112 syslog_level = LOG_CRIT;
115 syslog_level = LOG_ERR;
118 syslog_level = LOG_INFO;
121 syslog_level = LOG_DEBUG;
124 const char *color_pfx = "", *color_sfx = "";
125 const char *time_pfx = "", *time_sfx = "";
126 static int initialized = 0;
127 static int LogDoColor = 0;
134 term = getenv("TERM");
137 const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode", "xterm-256color" };
140 /* for each known color terminal */
141 for (i = 0; i < COUNT_OF(terms); i++)
143 /* we found a supported term? */
144 if (0 == strcmp(terms[i], term))
155 color_sfx = "\33[0m";
156 time_sfx = color_sfx;
157 time_pfx = "\33[36m"; /* Cyan */
161 case PCSC_LOG_CRITICAL:
162 color_pfx = "\33[01;31m"; /* bright + Red */
166 color_pfx = "\33[35m"; /* Magenta */
170 color_pfx = "\33[34m"; /* Blue */
174 color_pfx = ""; /* normal (black) */
181 gettimeofday(&new_time, NULL);
182 if (0 == last_time.tv_sec)
183 last_time = new_time;
185 tmp.tv_sec = new_time.tv_sec - last_time.tv_sec;
186 tmp.tv_usec = new_time.tv_usec - last_time.tv_usec;
190 tmp.tv_usec += 1000000;
192 if (tmp.tv_sec < 100)
193 delta = tmp.tv_sec * 1000000 + tmp.tv_usec;
197 last_time = new_time;
199 va_start(argptr, fmt);
200 (void)vsnprintf(debug_buffer, sizeof debug_buffer, fmt, argptr);
204 syslog(syslog_level, "%.8d %s", delta, debug_buffer);
206 (void)fprintf(LOG_STREAM, "%s%.8d%s %s%s%s\n", time_pfx, delta, time_sfx,
207 color_pfx, debug_buffer, color_sfx);
212 void log_xxd(const int priority, const char *msg, const unsigned char *buffer,
216 char *c, debug_buffer[len*3 + strlen(msg) +1];
221 l = strlcpy(debug_buffer, msg, sizeof debug_buffer);
222 c = debug_buffer + l;
224 for (i = 0; i < len; ++i)
226 /* 2 hex characters, 1 space, 1 NUL : total 4 characters */
227 (void)snprintf(c, 4, "%02X ", buffer[i]);
232 syslog(LOG_DEBUG, "%s", debug_buffer);
234 (void)fprintf(LOG_STREAM, "%s\n", debug_buffer);