1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/* bjnp-debug.c
*
* (c) 2008, 2009 Louis Lagendijk
* (c) 2018 Markus Heinz
*
* This software is licensed under the terms of the GPL.
* For details see file COPYING.
*/
#include "config.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h> /* usleep */
#include <stdint.h>
#include <errno.h>
#include "bjnp.h"
#ifdef __GNUC__
# define UNUSED(v) (void) v
#else
# define UNUSED(v)
#endif
typedef struct
{
bjnp_loglevel_t level;
char string[10];
} logtable_entry_t;
static logtable_entry_t logtable[] = {
{LOG_NONE, "NONE"},
{LOG_EMERG, "EMERG"},
{LOG_ALERT, "ALERT"},
{LOG_CRIT, "CRIT"},
{LOG_ERROR, "ERROR"},
{LOG_WARN, "WARNING"},
{LOG_NOTICE, "NOTICE"},
{LOG_INFO, "INFO"},
{LOG_DEBUG, "DEBUG"},
{LOG_DEBUG2, "DEBUG2"},
{LOG_END, ""}
};
/*
* static data
*/
static bjnp_loglevel_t debug_level = LOG_ERROR;
static int to_cups = 0;
static FILE *debug_file = NULL;
static time_t start_sec = 0;
static suseconds_t start_msec;
/*
* local functions
*/
void bjnp_get_time (time_t * sec, uint32_t * usec);
bjnp_loglevel_t str2level (char *level);
char * level2str (bjnp_loglevel_t level);
#ifndef NDEBUG
static void
u8tohex (uint8_t x, char *str)
{
static const char hdigit[16] =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f'
};
str[0] = hdigit[(x >> 4) & 0xf];
str[1] = hdigit[x & 0xf];
str[2] = '\0';
}
static void
u8tochar (uint8_t x, char *str)
{
if ((x > 0x20) && (x < 0x7f))
str[0] = (char) x;
else
str[0] = (char) '.';
}
static void
u32tohex (uint32_t x, char *str)
{
u8tohex (x >> 24, str);
u8tohex (x >> 16, str + 2);
u8tohex (x >> 8, str + 4);
u8tohex (x, str + 6);
}
char *
level2str (bjnp_loglevel_t level)
{
int i;
for (i = 0; logtable[i].level != LOG_END; i++)
{
if (logtable[i].level == level)
return logtable[i].string;
}
return "UNDEF";
}
bjnp_loglevel_t
str2level (char *level)
{
int i;
for (i = 0; strlen (logtable[i].string) != 0; i++)
{
if (strncasecmp (level, logtable[i].string, 10) == 0)
return logtable[i].level;
}
return LOG_END;
}
void
bjnp_hexdump (bjnp_loglevel_t level, char *header, const void *d_,
unsigned len)
{
const uint8_t *d = (const uint8_t *) (d_);
unsigned ofs, c;
char line[100]; /* actually only 1+8+1+8*3+1+8*3+1+4+16 =
80 bytes needed */
if (level > debug_level)
return;
bjnp_debug (level, "%s\n", header);
ofs = 0;
while (ofs < len)
{
char *p;
memset (line, ' ', sizeof (line));
line[0] = ' ';
u32tohex (ofs, line + 1);
line[9] = ':';
p = line + 10;
for (c = 0; c != 16 && (ofs + c) < len; c++)
{
u8tohex (d[ofs + c], p);
p[2] = ' ';
p += 3;
if (c == 7)
{
p[0] = ' ';
p++;
}
}
p[0] = p[1] = p[2] = ' ';
p = line + 61;
for (c = 0; c != 16 && (ofs + c) < len; c++)
{
u8tochar (d[ofs + c], p);
p++;
if (c == 7)
{
p[0] = ' ';
p++;
}
}
p[0] = '\0';
bjnp_debug (level, "%s\n", line);
ofs += c;
}
bjnp_debug (level, "\n\n");
}
#endif /* NDEBUG */
void
bjnp_debug (bjnp_loglevel_t level, const char *fmt, ...)
{
va_list ap;
char printbuf[256];
struct timeval tp;
int sec;
int msec;
/* print received data into a string */
va_start (ap, fmt);
vsnprintf (printbuf, sizeof (printbuf), fmt, ap);
va_end (ap);
/* we only send real errors & warnings to stderr, unless explicitly asked */
if ((level <= LOG_WARN) || to_cups)
fprintf (stderr, "%s: %s", level2str (level), printbuf);
/* other log messages may go to the own logfile */
if ((level <= debug_level) && debug_file)
{
gettimeofday(&tp, NULL);
if ((msec = tp.tv_usec - start_msec) < 0) {
msec += 1000;
tp.tv_sec -= 1;
}
sec = tp.tv_sec - start_sec;
fprintf (debug_file, "%s: %03d.%03d %s", level2str (level), sec, msec,
printbuf);
}
}
void
bjnp_set_debug_level (char *level)
{
/*
* set debug level to level (string)
*/
struct timeval tp;
char loglevel[16];
char *separator;
gettimeofday(&tp, NULL);
start_sec = tp.tv_sec;
start_msec = tp.tv_usec;
/*
* Split string into loglevel and optional cupslog string
*/
separator = strchr (level, '_');
to_cups = 0;
if (separator)
{
*separator = '\0';
separator++;
if (strlen (separator) > 0)
to_cups = 1;
}
/*
* Set log level
*/
if (level == NULL)
debug_level = LOG_ERROR;
else
{
strncpy (loglevel, level, 15);
level[15] = '\0';
debug_level = str2level (level);
}
if ((debug_file = fopen (LOGFILE, "w")) == NULL)
bjnp_debug (LOG_WARN, "Can not open logfile: %s - %s\n",
LOGFILE, strerror (errno));
bjnp_debug (LOG_INFO, "BJNP debug level = %s\n", level2str (debug_level));
}
|