Blame src/debug.c

Packit Service aee942
/*
Packit Service aee942
    debug.c: log (or not) messages
Packit Service aee942
    Copyright (C) 2003-2011   Ludovic Rousseau
Packit Service aee942
Packit Service aee942
    This library is free software; you can redistribute it and/or
Packit Service aee942
    modify it under the terms of the GNU Lesser General Public
Packit Service aee942
    License as published by the Free Software Foundation; either
Packit Service aee942
    version 2.1 of the License, or (at your option) any later version.
Packit Service aee942
Packit Service aee942
    This library is distributed in the hope that it will be useful,
Packit Service aee942
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service aee942
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service aee942
    Lesser General Public License for more details.
Packit Service aee942
Packit Service aee942
	You should have received a copy of the GNU Lesser General Public License
Packit Service aee942
	along with this library; if not, write to the Free Software Foundation,
Packit Service aee942
	Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit Service aee942
*/
Packit Service aee942
Packit Service aee942
#include <config.h>
Packit Service aee942
#include "misc.h"
Packit Service aee942
#include "debug.h"
Packit Service aee942
Packit Service aee942
#include <stdarg.h>
Packit Service aee942
#include <stdio.h>
Packit Service aee942
#include <string.h>
Packit Service aee942
#include <sys/time.h>
Packit Service aee942
#include <stdlib.h>
Packit Service aee942
Packit Service aee942
#ifdef USE_SYSLOG
Packit Service aee942
#include <syslog.h>
Packit Service aee942
#endif
Packit Service aee942
Packit Service aee942
#include "strlcpycat.h"
Packit Service aee942
Packit Service aee942
#undef LOG_TO_STDERR
Packit Service aee942
Packit Service aee942
#ifdef LOG_TO_STDERR
Packit Service aee942
#define LOG_STREAM stderr
Packit Service aee942
#else
Packit Service aee942
#define LOG_STREAM stdout
Packit Service aee942
#endif
Packit Service aee942
Packit Service aee942
void log_msg(const int priority, const char *fmt, ...)
Packit Service aee942
{
Packit Service aee942
	char debug_buffer[3 * 80]; /* up to 3 lines of 80 characters */
Packit Service aee942
	va_list argptr;
Packit Service aee942
	static struct timeval last_time = { 0, 0 };
Packit Service aee942
	struct timeval new_time = { 0, 0 };
Packit Service aee942
	struct timeval tmp;
Packit Service aee942
	int delta;
Packit Service aee942
#ifdef USE_SYSLOG
Packit Service aee942
	int syslog_level;
Packit Service aee942
Packit Service aee942
	switch(priority)
Packit Service aee942
	{
Packit Service aee942
		case PCSC_LOG_CRITICAL:
Packit Service aee942
			syslog_level = LOG_CRIT;
Packit Service aee942
			break;
Packit Service aee942
		case PCSC_LOG_ERROR:
Packit Service aee942
			syslog_level = LOG_ERR;
Packit Service aee942
			break;
Packit Service aee942
		case PCSC_LOG_INFO:
Packit Service aee942
			syslog_level = LOG_INFO;
Packit Service aee942
			break;
Packit Service aee942
		default:
Packit Service aee942
			syslog_level = LOG_DEBUG;
Packit Service aee942
	}
Packit Service aee942
#else
Packit Service aee942
	const char *color_pfx = "", *color_sfx = "";
Packit Service aee942
	const char *time_pfx = "", *time_sfx = "";
Packit Service aee942
	static int initialized = 0;
Packit Service aee942
	static int LogDoColor = 0;
Packit Service aee942
Packit Service aee942
	if (!initialized)
Packit Service aee942
	{
Packit Service aee942
		char *term;
Packit Service aee942
Packit Service aee942
		initialized = 1;
Packit Service aee942
		term = getenv("TERM");
Packit Service aee942
		if (term)
Packit Service aee942
		{
Packit Service aee942
			const char *terms[] = { "linux", "xterm", "xterm-color", "Eterm", "rxvt", "rxvt-unicode", "xterm-256color" };
Packit Service aee942
			unsigned int i;
Packit Service aee942
Packit Service aee942
			/* for each known color terminal */
Packit Service aee942
			for (i = 0; i < COUNT_OF(terms); i++)
Packit Service aee942
			{
Packit Service aee942
				/* we found a supported term? */
Packit Service aee942
				if (0 == strcmp(terms[i], term))
Packit Service aee942
				{
Packit Service aee942
					LogDoColor = 1;
Packit Service aee942
					break;
Packit Service aee942
				}
Packit Service aee942
			}
Packit Service aee942
		}
Packit Service aee942
	}
Packit Service aee942
Packit Service aee942
	if (LogDoColor)
Packit Service aee942
	{
Packit Service aee942
		color_sfx = "\33[0m";
Packit Service aee942
		time_sfx = color_sfx;
Packit Service aee942
		time_pfx = "\33[36m"; /* Cyan */
Packit Service aee942
Packit Service aee942
		switch (priority)
Packit Service aee942
		{
Packit Service aee942
			case PCSC_LOG_CRITICAL:
Packit Service aee942
				color_pfx = "\33[01;31m"; /* bright + Red */
Packit Service aee942
				break;
Packit Service aee942
Packit Service aee942
			case PCSC_LOG_ERROR:
Packit Service aee942
				color_pfx = "\33[35m"; /* Magenta */
Packit Service aee942
				break;
Packit Service aee942
Packit Service aee942
			case PCSC_LOG_INFO:
Packit Service aee942
				color_pfx = "\33[34m"; /* Blue */
Packit Service aee942
				break;
Packit Service aee942
Packit Service aee942
			case PCSC_LOG_DEBUG:
Packit Service aee942
				color_pfx = ""; /* normal (black) */
Packit Service aee942
				color_sfx = "";
Packit Service aee942
				break;
Packit Service aee942
		}
Packit Service aee942
	}
Packit Service aee942
#endif
Packit Service aee942
Packit Service aee942
	gettimeofday(&new_time, NULL);
Packit Service aee942
	if (0 == last_time.tv_sec)
Packit Service aee942
		last_time = new_time;
Packit Service aee942
Packit Service aee942
	tmp.tv_sec = new_time.tv_sec - last_time.tv_sec;
Packit Service aee942
	tmp.tv_usec = new_time.tv_usec - last_time.tv_usec;
Packit Service aee942
	if (tmp.tv_usec < 0)
Packit Service aee942
	{
Packit Service aee942
		tmp.tv_sec--;
Packit Service aee942
		tmp.tv_usec += 1000000;
Packit Service aee942
	}
Packit Service aee942
	if (tmp.tv_sec < 100)
Packit Service aee942
		delta = tmp.tv_sec * 1000000 + tmp.tv_usec;
Packit Service aee942
	else
Packit Service aee942
		delta = 99999999;
Packit Service aee942
Packit Service aee942
	last_time = new_time;
Packit Service aee942
Packit Service aee942
	va_start(argptr, fmt);
Packit Service aee942
	(void)vsnprintf(debug_buffer, sizeof debug_buffer, fmt, argptr);
Packit Service aee942
	va_end(argptr);
Packit Service aee942
Packit Service aee942
#ifdef USE_SYSLOG
Packit Service aee942
	syslog(syslog_level, "%.8d %s", delta, debug_buffer);
Packit Service aee942
#else
Packit Service aee942
	(void)fprintf(LOG_STREAM, "%s%.8d%s %s%s%s\n", time_pfx, delta, time_sfx,
Packit Service aee942
		color_pfx, debug_buffer, color_sfx);
Packit Service aee942
	fflush(LOG_STREAM);
Packit Service aee942
#endif
Packit Service aee942
} /* log_msg */
Packit Service aee942
Packit Service aee942
void log_xxd(const int priority, const char *msg, const unsigned char *buffer,
Packit Service aee942
	const int len)
Packit Service aee942
{
Packit Service aee942
	int i;
Packit Service aee942
	char *c, debug_buffer[len*3 + strlen(msg) +1];
Packit Service aee942
	size_t l;
Packit Service aee942
Packit Service aee942
	(void)priority;
Packit Service aee942
Packit Service aee942
	l = strlcpy(debug_buffer, msg, sizeof debug_buffer);
Packit Service aee942
	c = debug_buffer + l;
Packit Service aee942
Packit Service aee942
	for (i = 0; i < len; ++i)
Packit Service aee942
	{
Packit Service aee942
		/* 2 hex characters, 1 space, 1 NUL : total 4 characters */
Packit Service aee942
		(void)snprintf(c, 4, "%02X ", buffer[i]);
Packit Service aee942
		c += 3;
Packit Service aee942
	}
Packit Service aee942
Packit Service aee942
#ifdef USE_SYSLOG
Packit Service aee942
	syslog(LOG_DEBUG, "%s", debug_buffer);
Packit Service aee942
#else
Packit Service aee942
	(void)fprintf(LOG_STREAM, "%s\n", debug_buffer);
Packit Service aee942
	fflush(LOG_STREAM);
Packit Service aee942
#endif
Packit Service aee942
} /* log_xxd */