[Pcsclite-cvs-commit] r6644 - /trunk/Drivers/ccid/src/debug.c
rousseau at users.alioth.debian.org
rousseau at users.alioth.debian.org
Mon Jun 3 08:08:33 UTC 2013
Author: rousseau
Date: Mon Jun 3 08:08:33 2013
New Revision: 6644
URL: http://svn.debian.org/wsvn/pcsclite/?sc=1&rev=6644
Log:
Add colorization of logs
This log API is used when pcsc-lite do not provide it i.e. on Mac OS X.
The colorization should happen only when the logs are displayed on a
terminal and not when sent in a log file.
The main use is with the handler_test application.
Modified:
trunk/Drivers/ccid/src/debug.c
Modified: trunk/Drivers/ccid/src/debug.c
URL: http://svn.debian.org/wsvn/pcsclite/trunk/Drivers/ccid/src/debug.c?rev=6644&op=diff
==============================================================================
--- trunk/Drivers/ccid/src/debug.c (original)
+++ trunk/Drivers/ccid/src/debug.c Mon Jun 3 08:08:33 2013
@@ -28,6 +28,9 @@
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
+#include <sys/time.h>
+#include <stdlib.h>
+
#include "strlcpycat.h"
#undef LOG_TO_STDERR
@@ -42,14 +45,83 @@
{
char debug_buffer[160]; /* up to 2 lines of 80 characters */
va_list argptr;
+ static struct timeval last_time = { 0, 0 };
+ struct timeval new_time = { 0, 0 };
+ struct timeval tmp;
+ int delta;
+ const char *color_pfx = "", *color_sfx = "\33[0m";
+ const char *time_pfx = "\33[36m", *time_sfx = color_sfx;
+ static int initialized = 0;
+ static int LogDoColor = 0;
- (void)priority;
+ if (!initialized)
+ {
+ char *term;
+
+ initialized = 1;
+ term = getenv("TERM");
+ if (term)
+ {
+ const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode", "xterm-256color" };
+ unsigned int i;
+
+ /* for each known color terminal */
+ for (i = 0; i < sizeof(terms) / sizeof(terms[0]); i++)
+ {
+ /* we found a supported term? */
+ if (0 == strcmp(terms[i], term))
+ {
+ LogDoColor = 1;
+ break;
+ }
+ }
+ }
+ }
+
+ switch (priority)
+ {
+ case PCSC_LOG_CRITICAL:
+ color_pfx = "\33[01;31m"; /* bright + Red */
+ break;
+
+ case PCSC_LOG_ERROR:
+ color_pfx = "\33[35m"; /* Magenta */
+ break;
+
+ case PCSC_LOG_INFO:
+ color_pfx = "\33[34m"; /* Blue */
+ break;
+
+ case PCSC_LOG_DEBUG:
+ color_pfx = ""; /* normal (black) */
+ color_sfx = "";
+ break;
+ }
+
+ gettimeofday(&new_time, NULL);
+ if (0 == last_time.tv_sec)
+ last_time = new_time;
+
+ tmp.tv_sec = new_time.tv_sec - last_time.tv_sec;
+ tmp.tv_usec = new_time.tv_usec - last_time.tv_usec;
+ if (tmp.tv_usec < 0)
+ {
+ tmp.tv_sec--;
+ tmp.tv_usec += 1000000;
+ }
+ if (tmp.tv_sec < 100)
+ delta = tmp.tv_sec * 1000000 + tmp.tv_usec;
+ else
+ delta = 99999999;
+
+ last_time = new_time;
va_start(argptr, fmt);
(void)vsnprintf(debug_buffer, sizeof debug_buffer, fmt, argptr);
va_end(argptr);
- (void)fprintf(LOG_STREAM, "%s\n", debug_buffer);
+ (void)fprintf(LOG_STREAM, "%s%.8d%s %s%s%s\n", time_pfx, delta, time_sfx,
+ color_pfx, debug_buffer, color_sfx);
fflush(LOG_STREAM);
} /* log_msg */
More information about the Pcsclite-cvs-commit
mailing list