2 Unix SMB/CIFS implementation.
4 Copyright (C) Volker Lendecke 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/time.h"
25 static void _do_debug_v(const char *format, va_list ap)
33 ret = vasprintf(&s, format, ap);
35 fprintf(stderr, "vasprintf failed in _do_debug_v, cannot print debug message.\n");
40 t = timeval_current();
41 tm = localtime(&t.tv_sec);
43 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
45 fprintf(stderr, "%s.%06u [%5u]: %s", tbuf, (unsigned)t.tv_usec, (unsigned)getpid(), s);
50 /* default logging function */
51 void (*do_debug_v)(const char *, va_list ap) = _do_debug_v;
53 void do_debug(const char *format, ...)
58 do_debug_v(format, ap);
63 static void _do_debug_add_v(const char *format, va_list ap)
68 ret = vasprintf(&s, format, ap);
70 fprintf(stderr, "vasprintf failed in _do_debug_add_v, cannot print debug message.\n");
75 fprintf(stderr, "%s", s);
80 /* default logging function */
81 void (*do_debug_add_v)(const char *, va_list ap) = _do_debug_add_v;
83 void do_debug_add(const char *format, ...)
88 do_debug_add_v(format, ap);
92 #define DEBUGLVL(lvl) ((lvl) <= LogLevel)
93 #define DEBUG(lvl, x) do { this_log_level = (lvl); if ((lvl) < DEBUG_DEBUG) { log_ringbuffer x; } if ((lvl) <= LogLevel) { do_debug x; }} while (0)
94 #define DEBUGADD(lvl, x) do { if ((lvl) <= LogLevel) { this_log_level = (lvl); do_debug_add x; }} while (0)
96 static void print_asc(int level, const uint8_t *buf, size_t len)
100 DEBUGADD(level,("%c", isprint(buf[i])?buf[i]:'.'));
104 void dump_data(int level, const uint8_t *buf, size_t len)
110 if (!DEBUGLVL(level)) return;
112 DEBUG(level, (__location__ " dump data of size %i:\n", (int)len));
113 DEBUGADD(level,("[%03X] ",i));
115 DEBUGADD(level,("%02X ",(int)buf[i]));
117 if (i%8 == 0) DEBUGADD(level,(" "));
119 print_asc(level,&buf[i-16],8); DEBUGADD(level,(" "));
120 print_asc(level,&buf[i-8],8); DEBUGADD(level,("\n"));
121 if (i<len) DEBUGADD(level,("[%03X] ",i));
127 DEBUGADD(level,(" "));
128 if (n>8) DEBUGADD(level,(" "));
129 while (n--) DEBUGADD(level,(" "));
131 print_asc(level,&buf[i-(i%16)],n); DEBUGADD(level,( " " ));
133 if (n>0) print_asc(level,&buf[i-n],n);
134 DEBUGADD(level,("\n"));
136 DEBUG(level, (__location__ " dump data of size %i finished\n", (int)len));