4 * Copyright (c) 1998 Dirk Koopman G1TLH
19 #define LOGDIR "./dbg"
21 static char opened = 0;
22 unsigned long dbglevel = 0;
27 static time_t systime;
32 static int getdayno(time_t t)
40 static char *genlogname(char *dir, char *fn, int day, int readonly)
45 sprintf(buf, "%s/%s_%d", dir, fn, day);
47 /* look to see if this is out of date, if it is remove it */
48 if (!readonly && stat(buf, &s) >= 0) {
49 if (systime - s.st_mtime > (7*24*60*60))
55 static void rotate_log()
60 i = getdayno(systime);
77 thisday = getdayno(systime);
78 fn = genlogname(LOGDIR, "log", thisday, 0);
83 die("can't open %s (%d) for debug", fn, errno);
93 sprintf(buf, "%2.2s%3.3s%4.4s %8.8s", &ap[8], &ap[4], &ap[20], &ap[11]);
97 void dbginit(char *ident)
100 prog = strdup(ident);
102 mkdir(LOGDIR, 01777);
106 void dbgadd(unsigned long level)
111 void dbgsub(unsigned long level)
116 void dbgset(unsigned long level)
121 unsigned long dbgget()
126 void dbgread(char *s)
128 unsigned long level = strtoul(s, 0, 0);
132 void dbg(unsigned long level, char *format, ...)
134 if (f && DBGLEVEL(level)) {
144 sprintf(dbuf, "%s %s[%d,%04x] ", dbgtime(), prog, pid, level);
145 va_start(ap, format);
146 vsprintf(buf, format, ap);
148 if (i>1 && buf[i-1] == '\n')
158 sprintf(dbgbuf, "%s%s", dbuf, buf);
164 void dbgdump(unsigned long level, char *dir, unsigned char *s, int lth)
166 if (f && DBGLEVEL(level)) {
168 unsigned char *p2, *p1;
175 sprintf(buf, "%s %s[%d,%04x] %s Lth: %d", dbgtime(), prog, pid, level, dir, lth);
176 fprintf(f, "%s\n", buf);
181 /* calc how many blocks of 8 I can do */
185 for (p = buf, p2 = s; p2 < s + lth; p2 += c * 8, p = buf) {
187 sprintf(p, "%4d: ", p2 - s);
189 for (p1 = p2; p1 < s + lth && p1 < p2 + l; ++p1) {
190 sprintf(p, "%02X", *p1);
193 for ( ;p1 < p2 + l; ++p1) {
199 for (p1 = p2; p1 < s + lth && p1 < p2 + l; ++p1) {
200 sprintf(p, "%c", (*p1 >= ' ' && *p1 <= 0x7e) ? *p1 : '.');
203 fprintf(f, "%s\n", buf);