Dont store debug level DEBUG_DEBUG in the in-memory ringbuffer.
[tridge/ctdb.git] / lib / util / debug.c
1 /*
2    Unix SMB/CIFS implementation.
3    ctdb debug functions
4    Copyright (C) Volker Lendecke 2007
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "system/time.h"
22 #include <unistd.h>
23 #include <ctype.h>
24
25 static void _do_debug_v(const char *format, va_list ap)
26 {
27         struct timeval t;
28         char *s = NULL;
29         struct tm *tm;
30         char tbuf[100];
31         int ret;
32
33         ret = vasprintf(&s, format, ap);
34         if (ret == -1) {
35                 fprintf(stderr, "vasprintf failed in _do_debug_v, cannot print debug message.\n");
36                 fflush(stderr);
37                 return;
38         }
39
40         t = timeval_current();
41         tm = localtime(&t.tv_sec);
42
43         strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
44
45         fprintf(stderr, "%s.%06u [%5u]: %s", tbuf, (unsigned)t.tv_usec, (unsigned)getpid(), s);
46         fflush(stderr);
47         free(s);
48 }
49
50 /* default logging function */
51 void (*do_debug_v)(const char *, va_list ap) = _do_debug_v;
52
53 void do_debug(const char *format, ...)
54 {
55         va_list ap;
56
57         va_start(ap, format);
58         do_debug_v(format, ap);
59         va_end(ap);
60 }
61
62
63 static void _do_debug_add_v(const char *format, va_list ap)
64 {
65         char *s = NULL;
66         int ret;
67
68         ret = vasprintf(&s, format, ap);
69         if (ret == -1) {
70                 fprintf(stderr, "vasprintf failed in _do_debug_add_v, cannot print debug message.\n");
71                 fflush(stderr);
72                 return;
73         }
74
75         fprintf(stderr, "%s", s);
76         fflush(stderr);
77         free(s);
78 }
79
80 /* default logging function */
81 void (*do_debug_add_v)(const char *, va_list ap) = _do_debug_add_v;
82
83 void do_debug_add(const char *format, ...)
84 {
85         va_list ap;
86
87         va_start(ap, format);
88         do_debug_add_v(format, ap);
89         va_end(ap);
90 }
91
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)
95
96 static void print_asc(int level, const uint8_t *buf, size_t len)
97 {
98         int i;
99         for (i=0;i<len;i++) {
100                 DEBUGADD(level,("%c", isprint(buf[i])?buf[i]:'.'));
101         }
102 }
103
104 void dump_data(int level, const uint8_t *buf, size_t len)
105 {
106         int i=0;
107
108         if (len<=0) return;
109
110         if (!DEBUGLVL(level)) return;
111
112         DEBUG(level, (__location__ " dump data of size %i:\n", (int)len));
113         DEBUGADD(level,("[%03X] ",i));
114         for (i=0;i<len;) {
115                 DEBUGADD(level,("%02X ",(int)buf[i]));
116                 i++;
117                 if (i%8 == 0) DEBUGADD(level,(" "));
118                 if (i%16 == 0) {
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));
122                 }
123         }
124         if (i%16) {
125                 int n;
126                 n = 16 - (i%16);
127                 DEBUGADD(level,(" "));
128                 if (n>8) DEBUGADD(level,(" "));
129                 while (n--) DEBUGADD(level,("   "));
130                 n = MIN(8,i%16);
131                 print_asc(level,&buf[i-(i%16)],n); DEBUGADD(level,( " " ));
132                 n = (i%16) - n;
133                 if (n>0) print_asc(level,&buf[i-n],n);
134                 DEBUGADD(level,("\n"));
135         }
136         DEBUG(level, (__location__ " dump data of size %i finished\n", (int)len));
137 }
138