diff options
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | debug.c | 26 | ||||
| -rw-r--r-- | debug.h | 2 | ||||
| -rw-r--r-- | debug_syslog.c | 130 | ||||
| -rw-r--r-- | debug_syslog.h | 37 | ||||
| -rw-r--r-- | main.c | 14 | 
6 files changed, 203 insertions, 9 deletions
@@ -1,5 +1,4 @@  include Makefile.files  all: -	gcc ${DEBUG_SOURCES} main.c -o debug -DWITH_DBG_FILTER -# -DWITH_DBG_MUTEX
\ No newline at end of file +	gcc ${DEBUG_SOURCES} main.c debug_syslog.c -o debug  -DWITH_DBG_SYSLOG -DWITH_DBG_FILTER @@ -52,6 +52,10 @@ struct {  #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() @@ -118,6 +122,14 @@ dbg_status_t dbg_init(unsigned int flags, ...)          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*)); @@ -132,6 +144,12 @@ dbg_status_t dbg_init(unsigned int flags, ...)    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); @@ -144,6 +162,10 @@ void dbg_close()      if(dbg_config.file_fd != -1) close(dbg_config.file_fd);    } +#ifdef WITH_DBG_SYSLOG +  dbg_syslog_close(); +#endif +    dbg_mutex_close();  } @@ -232,6 +254,10 @@ int __debug(const char *func, const int line,      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(); @@ -46,7 +46,7 @@ enum DBG_FLAG {    DBG_FLAG_OUTPUT_TO_FD     = 0x00040000,    DBG_FLAG_OUTPUT_TO_FILE   = 0x00080000,  #ifdef WITH_DBG_SYSLOG -  DBG_FLAG_OUTPUT_TO_SYSLOG = 0x000f0000, +  DBG_FLAG_OUTPUT_TO_SYSLOG = 0x00100000,  #endif    // Default value of flags diff --git a/debug_syslog.c b/debug_syslog.c new file mode 100644 index 0000000..fb7ab2f --- /dev/null +++ b/debug_syslog.c @@ -0,0 +1,130 @@ +/* -*- 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" + +#define _GNU_SOURCE + +#include <stdio.h> +#include <string.h> +#include <time.h> + +#include <sys/socket.h> +#include <arpa/inet.h> +#include <unistd.h> +#include <netinet/in.h> +#include <errno.h> + +#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 + + +static int dbg_syslog_sock; +static struct sockaddr_in dbg_syslog_sockaddr; + +void dbg_syslog_init(const char* host, int port)  +{ +  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); +  dbg_syslog_sockaddr.sin_port = htons(port); +} + +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]: "; + +} + +void dbg_syslog_output(char* msg)  +{ +  if(dbg_syslog_sock < 0) return; + +  const time_t rawtime = time(NULL); +  struct tm time; +  localtime_r(&rawtime, &time); +  char buftime[SYSLOG_TIMELEN]; +  strftime(buftime, SYSLOG_TIMELEN, "%b %e %H:%M:%S ", &time); + +  char bufpri[SYSLOG_PRILEN] = "<20>"; +  char buftag[SYSLOG_TAGLEN] = "PROGRAM[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; +  close(dbg_syslog_sock); +} + +#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 new file mode 100644 index 0000000..ab26b6c --- /dev/null +++ b/debug_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__*/ @@ -44,14 +44,18 @@ int main(int argc, char *argv[])    dbg_status_t status = dbg_init(//DBG_FLAG_USE_MUTEX |                                   //DBG_FLAG_OUTPUT_TO_FILE |                                   //DBG_FLAG_OUTPUT_TO_FD | -                                 DBG_FLAG_OUTPUT_TO_STDOUT | +//                                 DBG_FLAG_OUTPUT_TO_STDOUT | +                                 DBG_FLAG_OUTPUT_TO_SYSLOG |                                   //DBG_FLAG_OUTPUT_TO_STDERR |                                   DBG_FLAG_USE_FILTER |                                   0,                                   //DBG_OPTION_FD, fd, -                                 //DBG_OPTION_FILENAME, "/tmp/my2.log", -                                 DBG_OPTION_FILTER, "-all,+foo", -                                  +//                                 DBG_OPTION_FILENAME, "", +                                 DBG_OPTION_SYSLOG_HOST, +                                 "127.0.0.1", +                                 DBG_OPTION_SYSLOG_PORT, +                                 514, +                                 DBG_OPTION_FILTER, "+all",                                   DBG_OPTION_END);    if(status != DBG_STATUS_OK) { @@ -67,7 +71,5 @@ int main(int argc, char *argv[])    dbg_close(); -  //close(fd); -    return 0;  }  | 
