libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

debug.c (1821B)


      1 #include <string.h>
      2 #include <fcntl.h>
      3 #include <unistd.h>
      4 #include <stdarg.h>
      5 
      6 #include <llog.h>
      7 #include <rerr.h>
      8 
      9 #include "lq/mem.h"
     10 #include "debug.h"
     11 
     12 static int default_fd = 2;
     13 static char nl = 0x0a;
     14 
     15 
     16 int debug_fd(int fd) {
     17 	if (fcntl(fd, F_GETFD) < 0) {
     18 		return 1;
     19 	};
     20 	default_fd = fd;
     21 	return 0;
     22 }
     23 
     24 
     25 static void debug_write(int fd, const char *s) {
     26 	size_t r;
     27 	size_t l;
     28 	size_t c;
     29 	char *p;
     30 	
     31 	l = strlen(s);
     32 	c = 0;
     33 	p = (char*)s;
     34 	while (c < l) {
     35 		r = write(fd, p, l - c);
     36 		p += r;
     37 		c += r;
     38 	}
     39 	write(fd, &nl, 1);
     40 }
     41 
     42 void llog_out(const char *s) {
     43 	debug_write(default_fd, s);
     44 }
     45 
     46 static void debug_out(enum lloglvl_e lvl, const char *ns, const char *msg) {
     47 	char *p;
     48 
     49 	p = llog_new_ns(lvl, (char*)msg, (char*)ns);
     50 	llog_out(p);
     51 }
     52 
     53 void debug(enum lloglvl_e lvl, const char *ns, const char *msg) {
     54 	debug_out(lvl, (char*)ns, (char*)msg);
     55 }
     56 
     57 void debug_x(enum lloglvl_e lvl, const char *ns, const char *msg, int argc, ...) {
     58 	int i;
     59 	long long l;
     60 	char *k;
     61 	char *p;
     62 	enum debug_typ_e typ;
     63 	void *v;	
     64 	va_list vv;
     65 
     66 	va_start(vv, argc);
     67 
     68 	p = llog_new_ns(lvl, (char*)msg, (char*)ns);
     69 
     70 	for (i = 0; i < argc; i++) {
     71 		typ = va_arg(vv, enum debug_typ_e);
     72 		l = va_arg(vv, int);
     73 		k = va_arg(vv, char*);
     74 		switch (typ) {
     75 			case MORGEL_TYP_BIN:
     76 				v = va_arg(vv, char*);
     77 				llog_add_b(k, v, l);
     78 				break;
     79 			case MORGEL_TYP_STR:
     80 				v = va_arg(vv, char*);
     81 				llog_add_s(k, v);
     82 				break;
     83 			case MORGEL_TYP_NUM:
     84 				l = va_arg(vv, long long);
     85 				llog_add_n(k, l);
     86 				break;
     87 		}
     88 	}
     89 	llog_out(p);
     90 	va_end(vv);
     91 }
     92 
     93 int debug_logerr(enum lloglvl_e lvl, int err, char *msg) {
     94 	char *e;
     95 	char *s;
     96 
     97 	if (msg == 0) {
     98 		msg = "(debug logerr)";
     99 	}
    100 	s = (char*)rerrpfx(err);
    101 	e = llog_new_ns(lvl, msg, s);
    102 	e = llog_add_x("errcode", err);
    103 	s = rerrstrv(err);
    104 	e = llog_add_s("err", s);
    105 	llog_out(e);
    106 	return err;
    107 }