-const char errlog_rcs[] = "$Id: errlog.c,v 1.99 2009/12/26 11:34:01 fabiankeil Exp $";
+const char errlog_rcs[] = "$Id: errlog.c,v 1.104 2010/07/26 11:20:17 fabiankeil Exp $";
/*********************************************************************
*
* File : $Source: /cvsroot/ijbswa/current/errlog.c,v $
* Purpose : Log errors to a designated destination in an elegant,
* printf-like fashion.
*
- * Copyright : Written by and Copyright (C) 2001-2009 the SourceForge
+ * Copyright : Written by and Copyright (C) 2001-2010 the
* Privoxy team. http://www.privoxy.org/
*
* Based on the Internet Junkbuster originally written
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
+#include <ctype.h>
#include "config.h"
#include "miscutil.h"
* Currently we reopen it every time the config file
* has been reloaded, but actually we only have to
* reopen it if the file name changed or if the
- * configuration reloas was caused by a SIGHUP.
+ * configuration reload was caused by a SIGHUP.
*/
log_error(LOG_LEVEL_INFO, "Failed to reopen logfile: \'%s\'. "
"Retrying after closing the old file descriptor first. If that "
break;
case 'N':
/*
- * Non-standard: Print a counted unterminated string.
+ * Non-standard: Print a counted unterminated string,
+ * replacing unprintable bytes with their hex value.
* Takes 2 parameters: int length, const char * string.
*/
ival = va_arg(ap, int);
format_string = "[counted string lenght < 0]";
}
}
- else if ((size_t)ival >= sizeof(tempbuf))
+ while ((ival-- > 0) && (length < log_buffer_size - 6))
{
- /*
- * String is too long, copy as much as possible.
- * It will be further truncated later.
- */
- memcpy(tempbuf, sval, sizeof(tempbuf)-1);
- tempbuf[sizeof(tempbuf)-1] = '\0';
- }
- else
- {
- memcpy(tempbuf, sval, (size_t) ival);
- tempbuf[ival] = '\0';
+ if (isprint((int)*sval) && (*sval != '\\'))
+ {
+ outbuf[length++] = *sval;
+ outbuf[length] = '\0';
+ }
+ else
+ {
+ snprintf(outbuf + length, log_buffer_size - length - 2,
+ "\\x%.2x", (int)*sval);
+ length += 4;
+ }
+ sval++;
}
+ /*
+ * XXX: In case of printable characters at the end of
+ * the %N string, we're not using the whole buffer.
+ */
+ format_string = (length < log_buffer_size - 6) ? "" : "[too long]";
break;
case 'E':
/* Non-standard: Print error code from errno */
snprintf(tempbuf, sizeof(tempbuf), "Bad format string: \"%s\"", fmt);
loglevel = LOG_LEVEL_FATAL;
break;
- } /* switch( p ) */
+ }
assert(length < log_buffer_size);
length += strlcpy(outbuf + length, format_string, log_buffer_size - length);
if (length >= log_buffer_size-2)
{
- static char warning[] = "... [too long, truncated]";
+ static const char warning[] = "... [too long, truncated]";
length = log_buffer_size - sizeof(warning) - 1;
length += strlcpy(outbuf + length, warning, log_buffer_size - length);
break;
}
- } /* for( p ... ) */
+ }
/* done with var. args */
va_end(ap);
* visible to all files that include errlog.h.
*
* Parameters :
- * 1 : error = a valid jb_err code
+ * 1 : jb_error = a valid jb_err code
*
* Returns : A string with the jb_err translation
*
*********************************************************************/
-const char *jb_err_to_string(int error)
+const char *jb_err_to_string(int jb_error)
{
- switch (error)
+ switch (jb_error)
{
case JB_ERR_OK:
return "Success, no error";