From 76f4ad0d529623ecd36ce8b0a4f3e73c0240e60c Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Fri, 4 Jan 2013 11:01:15 +0100 Subject: Rename project from 'Debug Module' to 'Hugin' --- debug.c | 332 --------------------------------------------------------- debug.h | 169 ----------------------------- debug.hpp | 39 ------- debug_filter.c | 131 ----------------------- debug_filter.h | 36 ------- debug_syslog.c | 274 ----------------------------------------------- debug_syslog.h | 37 ------- debug_util.h | 39 ------- hugin.c | 332 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hugin.h | 169 +++++++++++++++++++++++++++++ hugin.hpp | 39 +++++++ hugin_filter.c | 131 +++++++++++++++++++++++ hugin_filter.h | 36 +++++++ hugin_syslog.c | 274 +++++++++++++++++++++++++++++++++++++++++++++++ hugin_syslog.h | 37 +++++++ hugin_util.h | 39 +++++++ 16 files changed, 1057 insertions(+), 1057 deletions(-) delete mode 100644 debug.c delete mode 100644 debug.h delete mode 100644 debug.hpp delete mode 100644 debug_filter.c delete mode 100644 debug_filter.h delete mode 100644 debug_syslog.c delete mode 100644 debug_syslog.h delete mode 100644 debug_util.h create mode 100644 hugin.c create mode 100644 hugin.h create mode 100644 hugin.hpp create mode 100644 hugin_filter.c create mode 100644 hugin_filter.h create mode 100644 hugin_syslog.c create mode 100644 hugin_syslog.h create mode 100644 hugin_util.h diff --git a/debug.c b/debug.c deleted file mode 100644 index 70a4df1..0000000 --- a/debug.c +++ /dev/null @@ -1,332 +0,0 @@ -/* -*- 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 -#include -#include -#include -#include - -#include -#include -#include - -#ifdef WITH_DBG_MUTEX -#include -#endif - -#ifdef WITH_DBG_FILTER -#include "debug_filter.h" -#endif - -#ifdef WITH_DBG_SYSLOG -#include "debug_syslog.h" -#endif - -struct dbg_config_t { - unsigned int flags; - int fd; - int file_fd; - int stdout_no_date; -#ifdef WITH_DBG_MUTEX - pthread_mutex_t mutex; -#endif -#ifdef WITH_DBG_SYSLOG - const char* syslog_host; - int syslog_port; -#endif -} dbg_config = { - DBG_FLAG_DEFAULT, // flags - -1, // fd - -1, // file_fd - 0, //stdout_no_date -#ifdef WITH_DBG_MUTEX - {}, // mutex; -#endif -#ifdef WITH_DBG_SYSLOG - "", // syslog_host; - -1, // syslog_port; -#endif - /* // This doesn't work in C++ - .flags = DBG_FLAG_DEFAULT, - .fd = -1, - .file_fd = -1, - .stdout_no_date = 0 - */ -}; - -#ifdef WITH_DBG_MUTEX - pthread_mutex_t localtime_mutex; -#endif - -struct tm *dbg_localtime(const time_t *timep, struct tm *result) -{ - struct tm *res = NULL; -#ifdef WITH_DBG_MUTEX - pthread_mutex_lock(&localtime_mutex); -#endif - - if(timep && result) { - memcpy(result,localtime(timep),sizeof(*result)); - res = result; - } - -#ifdef WITH_DBG_MUTEX - pthread_mutex_unlock(&localtime_mutex); -#endif - - return res; -} - - -static void dbg_mutex_init() -{ -#ifdef WITH_DBG_MUTEX - if(dbg_config.flags & DBG_FLAG_USE_MUTEX) { - pthread_mutex_init(&dbg_config.mutex, NULL); - } - - pthread_mutex_init(&localtime_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); - } - - pthread_mutex_destroy(&localtime_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_STDOUT_NO_DATE: - dbg_config.stdout_no_date = 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 -#ifdef WITH_DBG_FILTER - case DBG_OPTION_FILTER: - dbg_filter_parse((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 unsigned int gettid() -{ - return (unsigned int)pthread_self(); -} -*/ - -static int dbg_create_header(char *hdr, size_t size) -{ - time_t rawtime = time(NULL); - struct tm t; - dbg_localtime(&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, int withdate) -{ - int s; - - if(fd == -1) return 1; - - if(withdate) { - char hdr[32]; - dbg_create_header(hdr, sizeof(hdr)); - - s = write(fd, hdr, strlen(hdr)); - s = write(fd, " ", 1); - } - - s = write(fd, msg, strlen(msg)); - if(msg[strlen(msg) - 1] != '\n') { - s = 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; - int sz; - - // NOTE: This must be identical to the debug_class_str in debug_filter.c - const char * const debug_class_str[] = - { "fixme", "err", "warn", "info", "debug" }; - - dbg_mutex_lock(); - -#ifdef WITH_DBG_FILTER - if(!dbg_filter_enabled(cl, ch)) goto done; -#endif - - // - // Generate message - // - - char buf[1024]; - 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, dbg_config.stdout_no_date == 0); - } - - if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_STDERR) { - dbg_output_fd(STDERR_FILENO, buf, 1); - } - - if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FD) { - dbg_output_fd(dbg_config.fd, buf, 1); - } - - if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FILE) { - dbg_output_fd(dbg_config.file_fd, buf, 1); - } - -#ifdef WITH_DBG_SYSLOG - if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_SYSLOG) { - dbg_syslog_output(buf); - } -#endif - -#ifdef WITH_DBG_FILTER -done: -#endif - dbg_mutex_unlock(); - - return result; -} diff --git a/debug.h b/debug.h deleted file mode 100644 index 3f3b753..0000000 --- a/debug.h +++ /dev/null @@ -1,169 +0,0 @@ -/* -*- 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__ - -enum DBG_FLAG { - // Features -#ifdef WITH_DBG_THREAD - DBG_FLAG_USE_THREAD = 0x00000001, -#endif -#ifdef WITH_DBG_MUTEX - DBG_FLAG_USE_MUTEX = 0x00000002, -#endif -#ifdef WITH_DBG_FILTER - DBG_FLAG_USE_FILTER = 0x00000004, -#endif - - // Outputs - DBG_FLAG_OUTPUT_TO_STDOUT = 0x00010000, - DBG_FLAG_OUTPUT_TO_STDERR = 0x00020000, - DBG_FLAG_OUTPUT_TO_FD = 0x00040000, - DBG_FLAG_OUTPUT_TO_FILE = 0x00080000, -#ifdef WITH_DBG_SYSLOG - DBG_FLAG_OUTPUT_TO_SYSLOG = 0x00100000, -#endif - - // Default value of flags - DBG_FLAG_DEFAULT = DBG_FLAG_OUTPUT_TO_STDOUT, // Output to stdout -}; - -enum DBG_OPTION { - /** - * No more options / last option. This is used - * to terminate the VARARGs list. - */ - DBG_OPTION_END, - - /** - * const char* argument containing a filename which will be used for log - * output. To be used with the DBG_FLAG_OUTPUT_TO_FILE flag. - */ - DBG_OPTION_FILENAME, - - /** - * Integer argument describing a file descriptor which will be used for log - * output. To be used with the DBG_FLAG_OUTPUT_TO_FD flag. - */ - DBG_OPTION_FD, - - /** - * Set this option to make the stdout output to be printed without any date - * information in the header. - * Parameter is an integer. - * Values: - * 0 := use date - * 1 := do not use date. - */ - DBG_OPTION_STDOUT_NO_DATE, - - /** - * Host and port to use when logging on an external server. - * Host is a const char* argument, port is an integer. - * To be used with the DBG_FLAG_USE_SYSLOG flag. - * Linux: If DBG_OPTION_SYSLOG_HOST is not supplied, the local syslog will be - * used. - * Windows: If DBG_OPTION_SYSLOG_HOST is not supplied an error will be - * returned by debug_init. - * If DBG_OPTION_SYSLOG_PORT is not supplied, the default syslogd port will - * be used (port 514). - */ -#ifdef WITH_DBG_SYSLOG - DBG_OPTION_SYSLOG_HOST, - DBG_OPTION_SYSLOG_PORT, -#endif - - /** - * Filter option. Argument is a const char *. - * fmt := [set[,set]*]* - * set := [+-]channel - * | class[+-]channel - * | [+-]all - */ -#ifdef WITH_DBG_FILTER - DBG_OPTION_FILTER, -#endif -}; - -typedef enum { - DBG_STATUS_OK = 0, - DBG_STATUS_UNKNOWN_OPTION, - DBG_STATUS_ERROR, -} dbg_status_t; - -/** - * @param flags combination of DBG_FLAG values - * @param ... list of options (type-value pairs, - * terminated with DBG_OPTION_END). - * @return 0 on success, 1 on error. - */ -dbg_status_t dbg_init(unsigned int flags, ...); -void dbg_close(); - -/** - * Example of usage (use mutex protected calls, send output to file): - * - * dbg_status_t status; - * status = debug_init(DBG_FLAG_OUTPUT_TO_FILE | DBG_FLAG_USE_MUTEX, - * DBG_OPTION_FILENAME, "/tmp/my.log", - DBG_OPTION_END); - * if(status != DBG_STATUS_OK) exit(1); - * INFO(example, "We are up and running\n"); - * dbg_close(); - */ - -/** - * Example of usage (simply outputs to stdout): - * - * INFO(example, "We are up and running\n"); - */ - -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__*/ diff --git a/debug.hpp b/debug.hpp deleted file mode 100644 index fb82878..0000000 --- a/debug.hpp +++ /dev/null @@ -1,39 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set et sw=2 ts=2: */ -/*************************************************************************** - * debug.hpp - * - * 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_HPP__ -#define __DEBUG_MODULE_DEBUG_HPP__ - -#ifdef __cplusplus -extern "C" { -#endif -#include "debug.h" -#ifdef __cplusplus -} -#endif - -#endif/*__DEBUG_MODULE_DEBUG_HPP__*/ diff --git a/debug_filter.c b/debug_filter.c deleted file mode 100644 index 23e83dd..0000000 --- a/debug_filter.c +++ /dev/null @@ -1,131 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set et sw=2 ts=2: */ -/*************************************************************************** - * debug_filter.c - * - * Fri Dec 7 14:24:27 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_filter.h" - -#include -#include - -#ifndef WITH_DBG_FILTER -#warning debug_filter.c compiled but WITH_DBG_FILTER not defined -#endif - -#define NELEM(x) (sizeof(x)/sizeof((x)[0])) -struct __debug_channel -{ - char name[32]; - unsigned flags; -}; - -#define __DEBUG_CHANNEL_MAX 256 - -static struct __debug_channel debug_channel[__DEBUG_CHANNEL_MAX]; -static unsigned n_debug_channel = 0; - -// Default is to enable everything... -static unsigned debug_flags = 0xffffffff;//(1<<__class_err)|(1<<__class_fixme); - -int dbg_filter_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); -} - -/* - * fmt := [set[,set]*]* - * set := [+-]channel - * | class[+-]channel - * | [+-]all - */ -int dbg_filter_parse(const char *filter) -{ - char *s; - char *next; - char *opt; - - // NOTE: This must be identical to the debug_class_str in debug.c - const char * const debug_class_str[] = - { "fixme", "err", "warn", "info", "debug" }; - - if(!(s = strdup(filter))) return 1; - - 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); - - return 0; -} diff --git a/debug_filter.h b/debug_filter.h deleted file mode 100644 index d312d56..0000000 --- a/debug_filter.h +++ /dev/null @@ -1,36 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set et sw=2 ts=2: */ -/*************************************************************************** - * debug_filter.h - * - * Fri Dec 7 14:24:27 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_FILTER_H__ -#define __DEBUG_MODULE_DEBUG_FILTER_H__ - -#include "debug.h" - -int dbg_filter_enabled(const enum __debug_class cl, const char *ch); -int dbg_filter_parse(const char *filter); - -#endif/*__DEBUG_MODULE_DEBUG_FILTER_H__*/ diff --git a/debug_syslog.c b/debug_syslog.c deleted file mode 100644 index 2a8abb3..0000000 --- a/debug_syslog.c +++ /dev/null @@ -1,274 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set et sw=2 ts=2: */ -/*************************************************************************** - * debug_syslog.c - * - * Fri Dec 7 14:24:54 CET 2012 - * Copyright 2012 Jonas Suhr Christensen - * jsc@umbraculum.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_syslog.h" - -#include -#include -#include - -#ifdef WIN32 -#include -#include -typedef SOCKET socket_t; -#else -typedef int socket_t; -#include -#include -#include -#include -#endif - -#include -#include - -#include "debug_util.h" - -#ifndef WITH_DBG_SYSLOG -#warning debug_syslog.c compiled but WITH_DBG_SYSLOG not defined -#endif - -#define SYSLOG_MSGLEN 1024 -#define SYSLOG_PRILEN 5 -#define SYSLOG_TIMELEN 32 -#define SYSLOG_TAGLEN 32 -#define SYSLOG_CONTENTLEN SYSLOG_MSGLEN - SYSLOG_PRILEN - SYSLOG_TIMELEN - SYSLOG_TAGLEN -//#define SYSLOG_CONTENTLEN SYSLOG_MSGLEN - SYSLOG_PRILEN - SYSLOG_TAGLEN - SYSLOG_HEADERLEN -1 - -#define SYSLOG_LENOFEXECNAME 256 - -static int dbg_syslog_sock; -static struct sockaddr_in dbg_syslog_sockaddr; -static pid_t pid; -static char execname[SYSLOG_LENOFEXECNAME]; - -#ifdef WIN32 -static void wsastartup() -{ - WORD wVersionRequested = MAKEWORD(2, 0); - WSADATA wsaData; - - int ret = WSAStartup(wVersionRequested, &wsaData); - if(ret != 0) { - fprintf(stderr, "WSAStartup failed.\n"); - } -} -#endif - -void dbg_syslog_init(const char* host, int port) -{ - -#ifdef WIN32 - wsastartup(); -#endif - - printf("Initializing syslog module remote %s:%d\n", host, port); - if ( (dbg_syslog_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { - fprintf(stderr, "Failed to create socket\n"); - return; - } - memset(&dbg_syslog_sockaddr, 0, sizeof(dbg_syslog_sockaddr)); - dbg_syslog_sockaddr.sin_family = AF_INET; -// dbg_syslog_sockaddr.sin_addr.s_addr = inet_addr(host); - struct hostent *hp = gethostbyname(host); - memcpy(&(dbg_syslog_sockaddr.sin_addr),*(hp->h_addr_list),sizeof(struct in_addr)); - dbg_syslog_sockaddr.sin_port = htons(port); - - // This implementation has all kind of possible errors: - // * It is Linux only - // * Max length of execname (including path, avoid this by cmdline file in reverse order) - // is set to SYSLOG_LENOFEXECNAME - // * If not found execname is "" - pid = getpid(); - char buf[SYSLOG_LENOFEXECNAME]; - FILE* f = fopen("/proc/self/cmdline", "r"); - if(f) { - char* s = NULL; - s = fgets(buf, SYSLOG_LENOFEXECNAME, f); - fclose(f); - } - - char* bufptr = strrchr(buf, '/'); - if(bufptr) { - strncpy(execname, bufptr+1, SYSLOG_LENOFEXECNAME-1); - } - else { - strcpy(execname, "\0"); - } - - printf("\tRunning as %s with pid %d\n", execname, pid); -} - -/* -void dbg_syslog_createheader() { - const time_t rawtime = time(NULL); - struct tm time; - localtime_r(&rawtime, &time); - char timebuf[256]; - strftime(timebuf, 256, "%b %e %H:%M:%S ", &time); - - char bufpri[SYSLOG_PRILEN] = "<20>"; - char buftag[SYSLOG_TAGLEN] = "PROGRAM[PID]: "; - -} -*/ - -/* - * Syslog message specification (based on rfc3164). - * Modified after reading syslog v2. documentation - * - * MSG: "<%d%d>%s %s %s%c%s", FACILITY, SEVERITY, TIME, CLIENT, TAG, DELIM, CONTENT - * - * Example: - * <34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8 - * - * - * NOTE! Length of MSG must not exceed 1024 characters! - * - * FACILITY: - * 0 kernel messages - * 1 user-level messages - * 2 mail system - * 3 system daemons - * 4 security/authorization messages (note 1) - * 5 messages generated internally by syslogd - * 6 line printer subsystem - * 7 network news subsystem - * 8 UUCP subsystem - * 9 clock daemon (note 2) - * 10 security/authorization messages (note 1) - * 11 FTP daemon - * 12 NTP subsystem - * 13 log audit (note 1) - * 14 log alert (note 1) - * 15 clock daemon (note 2) - * 16 local use 0 (local0) - * 17 local use 1 (local1) - * 18 local use 2 (local2) - * 19 local use 3 (local3) - * 20 local use 4 (local4) - * 21 local use 5 (local5) - * 22 local use 6 (local6) - * 23 local use 7 (local7) - * - * SEVERITY: - * 0 Emergency: system is unusable - * 1 Alert: action must be taken immediately - * 2 Critical: critical conditions - * 3 Error: error conditions - * 4 Warning: warning conditions - * 5 Notice: normal but significant condition - * 6 Informational: informational messages - * 7 Debug: debug-level messages - * - * TIME: - * Time of message formatted like: Mmm dd hh:mm:ss - * - * CLIENT: - * Hostname or - * IPv4 address or - * IPv6 address - * - * TAG: format: PROGRAM[PID] - * Program or process information - * PROGRAM is Alphanumeric - * PID is numeric - * Must not exceed 32 characters - * - * DELIM: - * A nonn-alphanumeric character (eg. ':', '[' ...) - * In implementation DELIM is the first character of CONTENT - * - * CONTENT: - * The message to log. - * String of printable characters - * - * */ - -void dbg_syslog_output(char* msg) -{ - if(dbg_syslog_sock < 0) return; - - const time_t rawtime = time(NULL); - struct tm time; - dbg_localtime(&rawtime, &time); - char buftime[SYSLOG_TIMELEN]; - strftime(buftime, SYSLOG_TIMELEN, "%b %e %H:%M:%S ", &time); - - // Currently everything is mapped to local facility 4 as debug - char bufpri[SYSLOG_PRILEN]; - strncpy(bufpri, "<167>", SYSLOG_PRILEN); - - char buftag[SYSLOG_TAGLEN]; - snprintf(buftag, SYSLOG_TAGLEN, "%s[%d]: ", execname, pid); - - char buf[SYSLOG_MSGLEN]; - memset(buf, 0, sizeof(buf)); - strncat(buf, bufpri, SYSLOG_PRILEN); - strncat(buf, buftime, SYSLOG_TIMELEN); - strncat(buf, buftag, SYSLOG_TAGLEN); - strncat(buf, msg, SYSLOG_CONTENTLEN); - strcat(buf, "\n"); - - -// printf("Sending to syslog: %s\n", buf); - int buf_len = strlen(buf); - if((sendto(dbg_syslog_sock, buf, buf_len, 0, (struct sockaddr *) &dbg_syslog_sockaddr, - sizeof(dbg_syslog_sockaddr))) != buf_len) { - fprintf(stderr, "Failed to send message successfully: %s\n", strerror(errno)); - } -} - -void dbg_syslog_close() { - printf("Closing syslog module\n"); - if(dbg_syslog_sock < 0) return; - -#ifdef WIN32 - closesocket(dbg_syslog_sock); - WSACleanup(); -#else - close(dbg_syslog_sock); -#endif -} - -#ifdef TEST_DEBUG_SYSLOG -//Additional dependency files -//deps: -//Required cflags (autoconf vars may be used) -//cflags: -//Required link options (autoconf vars may be used) -//libs: -#include "test.h" - -TEST_BEGIN; - -// TODO: Put some testcode here (see test.h for usable macros). -TEST_TRUE(false, "No tests yet!"); - -TEST_END; - -#endif/*TEST_DEBUG_SYSLOG*/ diff --git a/debug_syslog.h b/debug_syslog.h deleted file mode 100644 index ab26b6c..0000000 --- a/debug_syslog.h +++ /dev/null @@ -1,37 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set et sw=2 ts=2: */ -/*************************************************************************** - * debug_syslog.h - * - * Fri Dec 7 14:24:54 CET 2012 - * Copyright 2012 Jonas Suhr Christensen - * jsc@umbraculum.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_SYSLOG_H__ -#define __DEBUG_MODULE_DEBUG_SYSLOG_H__ - -void dbg_syslog_init(const char* host, int port); - -void dbg_syslog_output(char* msg); - -void dbg_syslog_close(); - -#endif/*__DEBUG_MODULE_DEBUG_SYSLOG_H__*/ diff --git a/debug_util.h b/debug_util.h deleted file mode 100644 index 589f223..0000000 --- a/debug_util.h +++ /dev/null @@ -1,39 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set et sw=2 ts=2: */ -/*************************************************************************** - * debug_util.h - * - * Fri Jan 4 10:10:03 CET 2013 - * Copyright 2013 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_UTIL_H__ -#define __DEBUG_MODULE_DEBUG_UTIL_H__ - -#include - -/** - * Threadsafe version of localtime (if compiled with mutex support). - * See man page for the posix localtime_r function. - */ -struct tm *dbg_localtime(const time_t *timep, struct tm *result); - -#endif/*__DEBUG_MODULE_DEBUG_UTIL_H__*/ diff --git a/hugin.c b/hugin.c new file mode 100644 index 0000000..70a4df1 --- /dev/null +++ b/hugin.c @@ -0,0 +1,332 @@ +/* -*- 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 +#include +#include +#include +#include + +#include +#include +#include + +#ifdef WITH_DBG_MUTEX +#include +#endif + +#ifdef WITH_DBG_FILTER +#include "debug_filter.h" +#endif + +#ifdef WITH_DBG_SYSLOG +#include "debug_syslog.h" +#endif + +struct dbg_config_t { + unsigned int flags; + int fd; + int file_fd; + int stdout_no_date; +#ifdef WITH_DBG_MUTEX + pthread_mutex_t mutex; +#endif +#ifdef WITH_DBG_SYSLOG + const char* syslog_host; + int syslog_port; +#endif +} dbg_config = { + DBG_FLAG_DEFAULT, // flags + -1, // fd + -1, // file_fd + 0, //stdout_no_date +#ifdef WITH_DBG_MUTEX + {}, // mutex; +#endif +#ifdef WITH_DBG_SYSLOG + "", // syslog_host; + -1, // syslog_port; +#endif + /* // This doesn't work in C++ + .flags = DBG_FLAG_DEFAULT, + .fd = -1, + .file_fd = -1, + .stdout_no_date = 0 + */ +}; + +#ifdef WITH_DBG_MUTEX + pthread_mutex_t localtime_mutex; +#endif + +struct tm *dbg_localtime(const time_t *timep, struct tm *result) +{ + struct tm *res = NULL; +#ifdef WITH_DBG_MUTEX + pthread_mutex_lock(&localtime_mutex); +#endif + + if(timep && result) { + memcpy(result,localtime(timep),sizeof(*result)); + res = result; + } + +#ifdef WITH_DBG_MUTEX + pthread_mutex_unlock(&localtime_mutex); +#endif + + return res; +} + + +static void dbg_mutex_init() +{ +#ifdef WITH_DBG_MUTEX + if(dbg_config.flags & DBG_FLAG_USE_MUTEX) { + pthread_mutex_init(&dbg_config.mutex, NULL); + } + + pthread_mutex_init(&localtime_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); + } + + pthread_mutex_destroy(&localtime_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_STDOUT_NO_DATE: + dbg_config.stdout_no_date = 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 +#ifdef WITH_DBG_FILTER + case DBG_OPTION_FILTER: + dbg_filter_parse((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 unsigned int gettid() +{ + return (unsigned int)pthread_self(); +} +*/ + +static int dbg_create_header(char *hdr, size_t size) +{ + time_t rawtime = time(NULL); + struct tm t; + dbg_localtime(&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, int withdate) +{ + int s; + + if(fd == -1) return 1; + + if(withdate) { + char hdr[32]; + dbg_create_header(hdr, sizeof(hdr)); + + s = write(fd, hdr, strlen(hdr)); + s = write(fd, " ", 1); + } + + s = write(fd, msg, strlen(msg)); + if(msg[strlen(msg) - 1] != '\n') { + s = 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; + int sz; + + // NOTE: This must be identical to the debug_class_str in debug_filter.c + const char * const debug_class_str[] = + { "fixme", "err", "warn", "info", "debug" }; + + dbg_mutex_lock(); + +#ifdef WITH_DBG_FILTER + if(!dbg_filter_enabled(cl, ch)) goto done; +#endif + + // + // Generate message + // + + char buf[1024]; + 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, dbg_config.stdout_no_date == 0); + } + + if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_STDERR) { + dbg_output_fd(STDERR_FILENO, buf, 1); + } + + if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FD) { + dbg_output_fd(dbg_config.fd, buf, 1); + } + + if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_FILE) { + dbg_output_fd(dbg_config.file_fd, buf, 1); + } + +#ifdef WITH_DBG_SYSLOG + if(dbg_config.flags & DBG_FLAG_OUTPUT_TO_SYSLOG) { + dbg_syslog_output(buf); + } +#endif + +#ifdef WITH_DBG_FILTER +done: +#endif + dbg_mutex_unlock(); + + return result; +} diff --git a/hugin.h b/hugin.h new file mode 100644 index 0000000..3f3b753 --- /dev/null +++ b/hugin.h @@ -0,0 +1,169 @@ +/* -*- 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__ + +enum DBG_FLAG { + // Features +#ifdef WITH_DBG_THREAD + DBG_FLAG_USE_THREAD = 0x00000001, +#endif +#ifdef WITH_DBG_MUTEX + DBG_FLAG_USE_MUTEX = 0x00000002, +#endif +#ifdef WITH_DBG_FILTER + DBG_FLAG_USE_FILTER = 0x00000004, +#endif + + // Outputs + DBG_FLAG_OUTPUT_TO_STDOUT = 0x00010000, + DBG_FLAG_OUTPUT_TO_STDERR = 0x00020000, + DBG_FLAG_OUTPUT_TO_FD = 0x00040000, + DBG_FLAG_OUTPUT_TO_FILE = 0x00080000, +#ifdef WITH_DBG_SYSLOG + DBG_FLAG_OUTPUT_TO_SYSLOG = 0x00100000, +#endif + + // Default value of flags + DBG_FLAG_DEFAULT = DBG_FLAG_OUTPUT_TO_STDOUT, // Output to stdout +}; + +enum DBG_OPTION { + /** + * No more options / last option. This is used + * to terminate the VARARGs list. + */ + DBG_OPTION_END, + + /** + * const char* argument containing a filename which will be used for log + * output. To be used with the DBG_FLAG_OUTPUT_TO_FILE flag. + */ + DBG_OPTION_FILENAME, + + /** + * Integer argument describing a file descriptor which will be used for log + * output. To be used with the DBG_FLAG_OUTPUT_TO_FD flag. + */ + DBG_OPTION_FD, + + /** + * Set this option to make the stdout output to be printed without any date + * information in the header. + * Parameter is an integer. + * Values: + * 0 := use date + * 1 := do not use date. + */ + DBG_OPTION_STDOUT_NO_DATE, + + /** + * Host and port to use when logging on an external server. + * Host is a const char* argument, port is an integer. + * To be used with the DBG_FLAG_USE_SYSLOG flag. + * Linux: If DBG_OPTION_SYSLOG_HOST is not supplied, the local syslog will be + * used. + * Windows: If DBG_OPTION_SYSLOG_HOST is not supplied an error will be + * returned by debug_init. + * If DBG_OPTION_SYSLOG_PORT is not supplied, the default syslogd port will + * be used (port 514). + */ +#ifdef WITH_DBG_SYSLOG + DBG_OPTION_SYSLOG_HOST, + DBG_OPTION_SYSLOG_PORT, +#endif + + /** + * Filter option. Argument is a const char *. + * fmt := [set[,set]*]* + * set := [+-]channel + * | class[+-]channel + * | [+-]all + */ +#ifdef WITH_DBG_FILTER + DBG_OPTION_FILTER, +#endif +}; + +typedef enum { + DBG_STATUS_OK = 0, + DBG_STATUS_UNKNOWN_OPTION, + DBG_STATUS_ERROR, +} dbg_status_t; + +/** + * @param flags combination of DBG_FLAG values + * @param ... list of options (type-value pairs, + * terminated with DBG_OPTION_END). + * @return 0 on success, 1 on error. + */ +dbg_status_t dbg_init(unsigned int flags, ...); +void dbg_close(); + +/** + * Example of usage (use mutex protected calls, send output to file): + * + * dbg_status_t status; + * status = debug_init(DBG_FLAG_OUTPUT_TO_FILE | DBG_FLAG_USE_MUTEX, + * DBG_OPTION_FILENAME, "/tmp/my.log", + DBG_OPTION_END); + * if(status != DBG_STATUS_OK) exit(1); + * INFO(example, "We are up and running\n"); + * dbg_close(); + */ + +/** + * Example of usage (simply outputs to stdout): + * + * INFO(example, "We are up and running\n"); + */ + +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__*/ diff --git a/hugin.hpp b/hugin.hpp new file mode 100644 index 0000000..fb82878 --- /dev/null +++ b/hugin.hpp @@ -0,0 +1,39 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * debug.hpp + * + * 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_HPP__ +#define __DEBUG_MODULE_DEBUG_HPP__ + +#ifdef __cplusplus +extern "C" { +#endif +#include "debug.h" +#ifdef __cplusplus +} +#endif + +#endif/*__DEBUG_MODULE_DEBUG_HPP__*/ diff --git a/hugin_filter.c b/hugin_filter.c new file mode 100644 index 0000000..23e83dd --- /dev/null +++ b/hugin_filter.c @@ -0,0 +1,131 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * debug_filter.c + * + * Fri Dec 7 14:24:27 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_filter.h" + +#include +#include + +#ifndef WITH_DBG_FILTER +#warning debug_filter.c compiled but WITH_DBG_FILTER not defined +#endif + +#define NELEM(x) (sizeof(x)/sizeof((x)[0])) +struct __debug_channel +{ + char name[32]; + unsigned flags; +}; + +#define __DEBUG_CHANNEL_MAX 256 + +static struct __debug_channel debug_channel[__DEBUG_CHANNEL_MAX]; +static unsigned n_debug_channel = 0; + +// Default is to enable everything... +static unsigned debug_flags = 0xffffffff;//(1<<__class_err)|(1<<__class_fixme); + +int dbg_filter_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); +} + +/* + * fmt := [set[,set]*]* + * set := [+-]channel + * | class[+-]channel + * | [+-]all + */ +int dbg_filter_parse(const char *filter) +{ + char *s; + char *next; + char *opt; + + // NOTE: This must be identical to the debug_class_str in debug.c + const char * const debug_class_str[] = + { "fixme", "err", "warn", "info", "debug" }; + + if(!(s = strdup(filter))) return 1; + + 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); + + return 0; +} diff --git a/hugin_filter.h b/hugin_filter.h new file mode 100644 index 0000000..d312d56 --- /dev/null +++ b/hugin_filter.h @@ -0,0 +1,36 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * debug_filter.h + * + * Fri Dec 7 14:24:27 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_FILTER_H__ +#define __DEBUG_MODULE_DEBUG_FILTER_H__ + +#include "debug.h" + +int dbg_filter_enabled(const enum __debug_class cl, const char *ch); +int dbg_filter_parse(const char *filter); + +#endif/*__DEBUG_MODULE_DEBUG_FILTER_H__*/ diff --git a/hugin_syslog.c b/hugin_syslog.c new file mode 100644 index 0000000..2a8abb3 --- /dev/null +++ b/hugin_syslog.c @@ -0,0 +1,274 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * debug_syslog.c + * + * Fri Dec 7 14:24:54 CET 2012 + * Copyright 2012 Jonas Suhr Christensen + * jsc@umbraculum.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_syslog.h" + +#include +#include +#include + +#ifdef WIN32 +#include +#include +typedef SOCKET socket_t; +#else +typedef int socket_t; +#include +#include +#include +#include +#endif + +#include +#include + +#include "debug_util.h" + +#ifndef WITH_DBG_SYSLOG +#warning debug_syslog.c compiled but WITH_DBG_SYSLOG not defined +#endif + +#define SYSLOG_MSGLEN 1024 +#define SYSLOG_PRILEN 5 +#define SYSLOG_TIMELEN 32 +#define SYSLOG_TAGLEN 32 +#define SYSLOG_CONTENTLEN SYSLOG_MSGLEN - SYSLOG_PRILEN - SYSLOG_TIMELEN - SYSLOG_TAGLEN +//#define SYSLOG_CONTENTLEN SYSLOG_MSGLEN - SYSLOG_PRILEN - SYSLOG_TAGLEN - SYSLOG_HEADERLEN -1 + +#define SYSLOG_LENOFEXECNAME 256 + +static int dbg_syslog_sock; +static struct sockaddr_in dbg_syslog_sockaddr; +static pid_t pid; +static char execname[SYSLOG_LENOFEXECNAME]; + +#ifdef WIN32 +static void wsastartup() +{ + WORD wVersionRequested = MAKEWORD(2, 0); + WSADATA wsaData; + + int ret = WSAStartup(wVersionRequested, &wsaData); + if(ret != 0) { + fprintf(stderr, "WSAStartup failed.\n"); + } +} +#endif + +void dbg_syslog_init(const char* host, int port) +{ + +#ifdef WIN32 + wsastartup(); +#endif + + printf("Initializing syslog module remote %s:%d\n", host, port); + if ( (dbg_syslog_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { + fprintf(stderr, "Failed to create socket\n"); + return; + } + memset(&dbg_syslog_sockaddr, 0, sizeof(dbg_syslog_sockaddr)); + dbg_syslog_sockaddr.sin_family = AF_INET; +// dbg_syslog_sockaddr.sin_addr.s_addr = inet_addr(host); + struct hostent *hp = gethostbyname(host); + memcpy(&(dbg_syslog_sockaddr.sin_addr),*(hp->h_addr_list),sizeof(struct in_addr)); + dbg_syslog_sockaddr.sin_port = htons(port); + + // This implementation has all kind of possible errors: + // * It is Linux only + // * Max length of execname (including path, avoid this by cmdline file in reverse order) + // is set to SYSLOG_LENOFEXECNAME + // * If not found execname is "" + pid = getpid(); + char buf[SYSLOG_LENOFEXECNAME]; + FILE* f = fopen("/proc/self/cmdline", "r"); + if(f) { + char* s = NULL; + s = fgets(buf, SYSLOG_LENOFEXECNAME, f); + fclose(f); + } + + char* bufptr = strrchr(buf, '/'); + if(bufptr) { + strncpy(execname, bufptr+1, SYSLOG_LENOFEXECNAME-1); + } + else { + strcpy(execname, "\0"); + } + + printf("\tRunning as %s with pid %d\n", execname, pid); +} + +/* +void dbg_syslog_createheader() { + const time_t rawtime = time(NULL); + struct tm time; + localtime_r(&rawtime, &time); + char timebuf[256]; + strftime(timebuf, 256, "%b %e %H:%M:%S ", &time); + + char bufpri[SYSLOG_PRILEN] = "<20>"; + char buftag[SYSLOG_TAGLEN] = "PROGRAM[PID]: "; + +} +*/ + +/* + * Syslog message specification (based on rfc3164). + * Modified after reading syslog v2. documentation + * + * MSG: "<%d%d>%s %s %s%c%s", FACILITY, SEVERITY, TIME, CLIENT, TAG, DELIM, CONTENT + * + * Example: + * <34>Oct 11 22:14:15 mymachine su: 'su root' failed for lonvick on /dev/pts/8 + * + * + * NOTE! Length of MSG must not exceed 1024 characters! + * + * FACILITY: + * 0 kernel messages + * 1 user-level messages + * 2 mail system + * 3 system daemons + * 4 security/authorization messages (note 1) + * 5 messages generated internally by syslogd + * 6 line printer subsystem + * 7 network news subsystem + * 8 UUCP subsystem + * 9 clock daemon (note 2) + * 10 security/authorization messages (note 1) + * 11 FTP daemon + * 12 NTP subsystem + * 13 log audit (note 1) + * 14 log alert (note 1) + * 15 clock daemon (note 2) + * 16 local use 0 (local0) + * 17 local use 1 (local1) + * 18 local use 2 (local2) + * 19 local use 3 (local3) + * 20 local use 4 (local4) + * 21 local use 5 (local5) + * 22 local use 6 (local6) + * 23 local use 7 (local7) + * + * SEVERITY: + * 0 Emergency: system is unusable + * 1 Alert: action must be taken immediately + * 2 Critical: critical conditions + * 3 Error: error conditions + * 4 Warning: warning conditions + * 5 Notice: normal but significant condition + * 6 Informational: informational messages + * 7 Debug: debug-level messages + * + * TIME: + * Time of message formatted like: Mmm dd hh:mm:ss + * + * CLIENT: + * Hostname or + * IPv4 address or + * IPv6 address + * + * TAG: format: PROGRAM[PID] + * Program or process information + * PROGRAM is Alphanumeric + * PID is numeric + * Must not exceed 32 characters + * + * DELIM: + * A nonn-alphanumeric character (eg. ':', '[' ...) + * In implementation DELIM is the first character of CONTENT + * + * CONTENT: + * The message to log. + * String of printable characters + * + * */ + +void dbg_syslog_output(char* msg) +{ + if(dbg_syslog_sock < 0) return; + + const time_t rawtime = time(NULL); + struct tm time; + dbg_localtime(&rawtime, &time); + char buftime[SYSLOG_TIMELEN]; + strftime(buftime, SYSLOG_TIMELEN, "%b %e %H:%M:%S ", &time); + + // Currently everything is mapped to local facility 4 as debug + char bufpri[SYSLOG_PRILEN]; + strncpy(bufpri, "<167>", SYSLOG_PRILEN); + + char buftag[SYSLOG_TAGLEN]; + snprintf(buftag, SYSLOG_TAGLEN, "%s[%d]: ", execname, pid); + + char buf[SYSLOG_MSGLEN]; + memset(buf, 0, sizeof(buf)); + strncat(buf, bufpri, SYSLOG_PRILEN); + strncat(buf, buftime, SYSLOG_TIMELEN); + strncat(buf, buftag, SYSLOG_TAGLEN); + strncat(buf, msg, SYSLOG_CONTENTLEN); + strcat(buf, "\n"); + + +// printf("Sending to syslog: %s\n", buf); + int buf_len = strlen(buf); + if((sendto(dbg_syslog_sock, buf, buf_len, 0, (struct sockaddr *) &dbg_syslog_sockaddr, + sizeof(dbg_syslog_sockaddr))) != buf_len) { + fprintf(stderr, "Failed to send message successfully: %s\n", strerror(errno)); + } +} + +void dbg_syslog_close() { + printf("Closing syslog module\n"); + if(dbg_syslog_sock < 0) return; + +#ifdef WIN32 + closesocket(dbg_syslog_sock); + WSACleanup(); +#else + close(dbg_syslog_sock); +#endif +} + +#ifdef TEST_DEBUG_SYSLOG +//Additional dependency files +//deps: +//Required cflags (autoconf vars may be used) +//cflags: +//Required link options (autoconf vars may be used) +//libs: +#include "test.h" + +TEST_BEGIN; + +// TODO: Put some testcode here (see test.h for usable macros). +TEST_TRUE(false, "No tests yet!"); + +TEST_END; + +#endif/*TEST_DEBUG_SYSLOG*/ diff --git a/hugin_syslog.h b/hugin_syslog.h new file mode 100644 index 0000000..ab26b6c --- /dev/null +++ b/hugin_syslog.h @@ -0,0 +1,37 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * debug_syslog.h + * + * Fri Dec 7 14:24:54 CET 2012 + * Copyright 2012 Jonas Suhr Christensen + * jsc@umbraculum.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_SYSLOG_H__ +#define __DEBUG_MODULE_DEBUG_SYSLOG_H__ + +void dbg_syslog_init(const char* host, int port); + +void dbg_syslog_output(char* msg); + +void dbg_syslog_close(); + +#endif/*__DEBUG_MODULE_DEBUG_SYSLOG_H__*/ diff --git a/hugin_util.h b/hugin_util.h new file mode 100644 index 0000000..589f223 --- /dev/null +++ b/hugin_util.h @@ -0,0 +1,39 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * debug_util.h + * + * Fri Jan 4 10:10:03 CET 2013 + * Copyright 2013 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_UTIL_H__ +#define __DEBUG_MODULE_DEBUG_UTIL_H__ + +#include + +/** + * Threadsafe version of localtime (if compiled with mutex support). + * See man page for the posix localtime_r function. + */ +struct tm *dbg_localtime(const time_t *timep, struct tm *result); + +#endif/*__DEBUG_MODULE_DEBUG_UTIL_H__*/ -- cgit v1.2.3