From a32e833c161a0dbcb94e939e75465ee302a8e0bb Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Thu, 1 Nov 2012 14:28:05 +0100 Subject: Simple basic implementation. --- add_file | 110 +++++++++++++++++++++++++++++++++++++++++ debug.cc | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ debug.h | 59 ++++++++++++++++++++++ 3 files changed, 336 insertions(+) create mode 100755 add_file diff --git a/add_file b/add_file new file mode 100755 index 0000000..c5d5b29 --- /dev/null +++ b/add_file @@ -0,0 +1,110 @@ +#!/bin/bash +PROJECT="Debug Module" + +function allfile() { + WHO="`whoami`" + + echo "/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */" > $1; + echo "/* vim: set et sw=2 ts=2: */" >> $1; + echo "/***************************************************************************" >> $1; + echo " * $1" >> $1; + echo " *" >> $1 ; + echo " * `date`" >> $1; + echo -n " * Copyright " >> $1 + echo -n `date +%Y | xargs` >> $1 + if [ "$WHO" == "nemo" ]; + then + echo " Jonas Suhr Christensen" >> $1; + echo " * jsc@umbraculum.org" >> $1; + fi + if [ "$WHO" == "deva" ]; + then + echo " Bent Bisballe Nyeng" >> $1; + echo " * deva@aasimon.org" >> $1; + fi + if [ "$WHO" == "senator" ]; + then + echo " Lars Bisballe Jensen" >> $1; + echo " * elsenator@gmail.com" >> $1; + fi + if [ "$WHO" == "piparum" ]; + then + echo " Peter Skaarup" >> $1; + echo " * and piparum@piparum.dk" >> $1; + fi + echo " ****************************************************************************/" >> $1; + echo "" >> $1; + echo "/*" >> $1; + echo " * This file is part of $PROJECT." >> $1; + echo " *" >> $1; + echo " * $PROJECT is free software; you can redistribute it and/or modify" >> $1; + echo " * it under the terms of the GNU General Public License as published by" >> $1; + echo " * the Free Software Foundation; either version 2 of the License, or" >> $1; + echo " * (at your option) any later version." >> $1; + echo " *" >> $1; + echo " * $PROJECT is distributed in the hope that it will be useful," >> $1; + echo " * but WITHOUT ANY WARRANTY; without even the implied warranty of" >> $1; + echo " * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the" >> $1; + echo " * GNU General Public License for more details." >> $1; + echo " *" >> $1; + echo " * You should have received a copy of the GNU General Public License" >> $1; + echo " * along with $PROJECT; if not, write to the Free Software" >> $1; + echo " * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA." >> $1; + echo " */" >> $1; +} + +function ccfile() { + local hf=`echo -n $1 | cut -d'.' -f1`.h; + hfile $hf; + + allfile $1; + echo -n '#include "' >> $1; + echo -n $hf >> $1; + echo '"' >> $1; + echo '' >> $1; + + local hn=`echo $1 | cut -d'.' -f1 | tr 'a-z.' 'A-Z_'` + echo "#ifdef TEST_${hn}" >> $1; + echo "//Additional dependency files" >> $1; + echo "//deps:" >> $1; + echo "//Required cflags (autoconf vars may be used)" >> $1; + echo "//cflags:" >> $1; + echo "//Required link options (autoconf vars may be used)" >> $1; + echo "//libs:" >> $1; + echo "#include \"test.h\"" >> $1; + echo "" >> $1; + echo "TEST_BEGIN;" >> $1; + echo "" >> $1; + echo "// TODO: Put some testcode here (see test.h for usable macros)." >> $1; + echo "TEST_TRUE(false, \"No tests yet!\");" >> $1; + echo "" >> $1; + echo "TEST_END;" >> $1; + echo "" >> $1; + echo "#endif/*TEST_${hn}*/" >> $1; +} + +function hfile() { + allfile $1; + local hn=`echo $1 | tr 'a-z.' 'A-Z_'` + local pr=`echo $PROJECT | tr 'a-z. ' 'A-Z__'` + echo "#ifndef __${pr}_${hn}__" >> $1; + echo "#define __${pr}_${hn}__" >> $1; + echo "#endif/*__${pr}_${hn}__*/" >> $1; +} + +if [ "$#" = "1" ]; then +if [ "CC" = `echo $1 | cut -d'.' -f2 | tr 'a-z' 'A-Z'` ]; then + ccfile $1; +fi; +if [ "H" = `echo $1 | cut -d'.' -f2 | tr 'a-z' 'A-Z'` ]; then + hfile $1; +fi; +else + echo "Usage: $0 filename"; + echo + echo "Examples:"; + echo "$0 myclass.cc Which will produce both myclass.cc and myclass.h"; + echo "$0 myinterface.h Which will only produce myinterface.h"; + echo + echo "NOTE: The files will be created in the current directory!"; +fi; diff --git a/debug.cc b/debug.cc index e69de29..d3c1dce 100644 --- a/debug.cc +++ b/debug.cc @@ -0,0 +1,167 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * debug.cc + * + * 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 +#include +#include +//#include +#include + +//#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, ...) +{ + // MutexAutolock m(mutex); + int ret = 0; + if(__debug_enabled(cl, ch)) { + if((unsigned)cl < NELEM(debug_class_str)) + ret += fprintf(logfp, "%u %s:%s:%s:%d ", + 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); +} diff --git a/debug.h b/debug.h index e69de29..0c5e3a5 100644 --- a/debug.h +++ b/debug.h @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * debug.h + * + * 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. + */ +#ifndef __DEBUG_MODULE_DEBUG_H__ +#define __DEBUG_MODULE_DEBUG_H__ + +#include + +void debug_init(FILE *fp); +void debug_parse(const char *fmt); + +enum __debug_class +{ + __class_fixme = 0, + __class_err = 1, + __class_warn = 2, + __class_info = 3, + __class_debug = 4 +}; + +int __debug(const char *func, const int line, enum __debug_class cl, + const char *ch, const char *fmt, ...) + __attribute__((format (printf,5,6))); + +#define __DEBUG_PRINT(cl, ch, fmt...) \ + do { __debug(__func__, __LINE__, cl, ch, fmt); } while(0) +#define __DEBUG(cl, ch, fmt...) \ + __DEBUG_PRINT(__class##cl, #ch, fmt) + +#define ERR(ch, fmt...) __DEBUG(_err, ch, fmt) +#define WARN(ch, fmt...) __DEBUG(_warn, ch, fmt) +#define INFO(ch, fmt...) __DEBUG(_info, ch, fmt) +#define DEBUG(ch, fmt...) __DEBUG(_debug, ch, fmt) + +#endif/*__DEBUG_MODULE_DEBUG_H__*/ -- cgit v1.2.3