summaryrefslogtreecommitdiff
path: root/debug.c
blob: 890d7a5681be30e3c6ed12909540dfd408b0f195 (plain)
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            debug.c
 *
 *  Thu Nov  1 13:38:47 CET 2012
 *  Copyright 2012 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Debug Module.
 *
 *  Debug Module is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Debug Module is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Debug Module; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "debug.h"

#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#ifdef WITH_DBG_MUTEX
#include <pthread.h>
#endif

struct {
  unsigned int flags;
#ifdef WITH_DBG_MUTEX
  pthread_mutex_t mutex;
#endif
  int fd;
  int file_fd;
#ifdef WITH_DBG_SYSLOG
  const char* syslog_host;
  int syslog_port;
#endif
} dbg_config = { .flags = DBG_FLAG_DEFAULT, .fd = -1, .file_fd = -1 }; 

static void dbg_mutex_init()
{
#ifdef WITH_DBG_MUTEX
  if(dbg_config.flags & DBG_FLAG_USE_MUTEX) {
    pthread_mutex_init(&dbg_config.mutex, NULL);
  }
#endif
}

static void dbg_mutex_lock()
{
#ifdef WITH_DBG_MUTEX
  if(dbg_config.flags & DBG_FLAG_USE_MUTEX) {
    pthread_mutex_lock(&dbg_config.mutex);
  }
#endif
}

static void dbg_mutex_unlock()
{
#ifdef WITH_DBG_MUTEX
  if(dbg_config.flags & DBG_FLAG_USE_MUTEX) {
    pthread_mutex_unlock(&dbg_config.mutex);
  }
#endif
}

static void dbg_mutex_close()
{
#ifdef WITH_DBG_MUTEX
  if(dbg_config.flags & DBG_FLAG_USE_MUTEX) {
    // Make sure we don't destroy the mutex while another thread is using it.
    dbg_mutex_lock();
    dbg_mutex_unlock();
    pthread_mutex_destroy(&dbg_config.mutex);
  }
#endif
}

dbg_status_t dbg_init(unsigned int flags, ...)
{
  dbg_status_t status = DBG_STATUS_OK;

  dbg_config.flags = flags;

  int end = 0;

  va_list vl;
  va_start(vl, flags);
  while(!end) {
    int option = va_arg(vl, int);
    switch(option) {
    case DBG_OPTION_END:
      end = 1;
      break;
    case DBG_OPTION_FD:
      dbg_config.fd = va_arg(vl, int);
      break;
    case DBG_OPTION_FILENAME:
      if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FILE) {
        const char *filename = (const char*)va_arg(vl, char*);
        dbg_config.file_fd = open(filename, O_CREAT | O_RDWR, 0777);
      }
      break;
#ifdef WITH_DBG_SYSLOG
    case DBG_OPTION_SYSLOG_PORT:
      dbg_config.syslog_port = va_arg(vl, int);
      break;
    case DBG_OPTION_SYSLOG_HOST:
      dbg_config.syslog_host = (const char*)va_arg(vl, char*);
      break;
#endif
    default:
      status = DBG_STATUS_UNKNOWN_OPTION;
      printf("option: %x\n", option);
      goto err;
    }
  }

  dbg_mutex_init();

#ifdef WITH_DBG_SYSLOG
  if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_SYSLOG) {
    dbg_syslog_init(dbg_config.syslog_host, dbg_config.syslog_port);
  }
#endif

 err:
  va_end(vl);

  return status;
}

void dbg_close()
{
  if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FILE) {
    if(dbg_config.file_fd != -1) close(dbg_config.file_fd);
  }

#ifdef WITH_DBG_SYSLOG
  dbg_syslog_close();
#endif

  dbg_mutex_close();
}

static const char * const debug_class_str[] =
  { "fixme", "err", "warn", "info", "debug" };

static int dbg_create_header(char *hdr, size_t size)
{
  time_t rawtime = time(NULL);
  struct tm t;
  localtime_r(&rawtime, &t);

  return snprintf(hdr, size,
                  "%d-%02d-%02d %02d:%02d:%02d",
                  t.tm_year + 1900,
                  t.tm_mon + 1,
                  t.tm_mday,
                  t.tm_hour,
                  t.tm_min,
                  t.tm_sec);
}

static int dbg_output_fd(int fd, const char *msg)
{
  if(fd == -1) return 1;

  char hdr[32];
  dbg_create_header(hdr, sizeof(hdr));
  write(fd, hdr, strlen(hdr));
  write(fd, " ", 1);
  write(fd, msg, strlen(msg));
  if(msg[strlen(msg) - 1] != '\n') write(fd, "\n", 1);
  return 0;
}

int __debug(const char *func, const int line,
            const enum __debug_class cl,
            const char *ch, const char *fmt, ...)
{
  int result = 0;

  dbg_mutex_lock();

  //
  // Generate message
  //

  char buf[1024];

  int sz = snprintf(buf, sizeof(buf),
                    "%s:%s:%s:%d ",
                    debug_class_str[(unsigned)cl], ch, func, line);
  va_list va;
  va_start(va, fmt);
  sz += vsnprintf(buf + sz, sizeof(buf) - sz, fmt, va);
  va_end(va);

  //
  // Send message to output
  //

  if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_STDOUT) {
    dbg_output_fd(STDOUT_FILENO, buf);
  }

  if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_STDERR) {
    dbg_output_fd(STDERR_FILENO, buf);
  }

  if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FD) {
    dbg_output_fd(dbg_config.fd, buf);
  }

  if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FILE) {
    dbg_output_fd(dbg_config.file_fd, buf);
  }

  if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_SYSLOG) {
    dbg_syslog_output(buf);
  }

 done:
  dbg_mutex_unlock();

  return result;
}

#if 0

#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
//#include <pthread.h>
#include <string>
#include <time.h>

//#include "mutex.h"

//static Mutex mutex;

static unsigned int gettid()
{
  return 0;//(unsigned int)pthread_self();
}

static FILE *logfp = stderr;

#define NELEM(x)	(sizeof(x)/sizeof((x)[0]))
struct __debug_channel
{
	char name[32];
	unsigned flags;
};

static const char * const debug_class_str[] =
  { "fixme", "err", "warn", "info", "debug" };

#define __DEBUG_CHANNEL_MAX 256

static struct __debug_channel debug_channel[__DEBUG_CHANNEL_MAX];
static unsigned n_debug_channel = 0;
static unsigned debug_flags = (1 << __class_err) | (1 << __class_fixme);

static int __debug_enabled(const enum __debug_class cl, const char *ch)
{
	unsigned i;
	for(i = 0; i < n_debug_channel; i++) {
		if(!strcmp(ch, debug_channel[i].name)) {
			return (debug_channel[i].flags & (1 << cl)) != 0;
		}
	}
	return debug_flags & (1 << cl);
}

int __debug(const char *func, const int line,
            const enum __debug_class cl,
            const char *ch, const char *fmt, ...)
{
  time_t rawtime = time(NULL);
  struct tm *t = localtime(&rawtime);

  //  MutexAutolock m(mutex);
	int ret = 0;
	if(__debug_enabled(cl, ch)) {
		if((unsigned)cl < NELEM(debug_class_str))
			ret += fprintf(logfp, "%d-%02d-%02d %02d:%02d:%02d %u %s:%s:%s:%d ",
                     t->tm_year + 1900,
                     t->tm_mon + 1,
                     t->tm_mday,
                     t->tm_hour,
                     t->tm_min,
                     t->tm_sec,
                     gettid(),
                     debug_class_str[(unsigned)cl], ch, func, line);
		if(fmt) {
			va_list va;
			va_start(va, fmt);
			ret += vfprintf(logfp, fmt, va);
			va_end(va);
		}
	}
	if(ret){
		fflush(logfp);
  }
	return ret;
}

void debug_init(FILE *fp)
{
  //  mutex.name = "debug";
  //  MutexAutolock m(mutex);
  logfp = fp;
}

/*
 * fmt := [set[,set]*]*
 * set := [+-]channel
 *     |  class[+-]channel
 *     |  [+-]all
 */
void debug_parse(const char *fmt)
{
  //  MutexAutolock m(mutex);
	char *s;
	char *next;
	char *opt;

	if(!(s = strdup(fmt))) return;

	for(opt = s; opt; opt = next) {
		int set = 0;
		int clr = 0;
		unsigned i;
		if((next = strchr(opt, ','))) *next++ = '\0';
		char *p = opt + strcspn(opt, "+-");
		if(!*p) p = opt;	// All chars -> a channel name
		if(p > opt) {
			// we have a class
			for(i = 0; i < NELEM(debug_class_str); i++) {
				int n = strlen(debug_class_str[i]);
				if(n != (p - opt)) continue;
				if(!memcmp(opt, debug_class_str[i], n)) {
					// Found the class
					if(*p == '+')
						set = 1 << i;
					else
						clr = 1 << i;
					break;
				}
			}
			if(i == NELEM(debug_class_str)) continue;
		} else {
			if(*p == '-')
				clr = ~0;
			else
				set = ~0;
		}
		if(*p == '+' || *p == '-') p++;
		if(!*p) continue;
		if(!strcmp("all", p)) {
			debug_flags = (debug_flags & ~clr) | set;
		} else {
			if(strlen(p) >= sizeof(debug_channel[0].name)) continue;
			for(i = 0; i < n_debug_channel; i++) {
				if(!strcmp(p, debug_channel[i].name)) {
					debug_channel[i].flags = (debug_channel[i].flags & ~clr) | set;
					break;
				}
			}
			if(i == n_debug_channel && n_debug_channel < __DEBUG_CHANNEL_MAX) {
				strcpy(debug_channel[i].name, p);
				debug_channel[i].flags = (debug_flags & ~clr) | set;
				n_debug_channel++;
			}
		}
	}
	free(s);
}


#endif