From 7c67d78c39bbcb094ee2c410b5be1c6acd4c50d0 Mon Sep 17 00:00:00 2001 From: Jonas Suhr Christensen Date: Fri, 7 Dec 2012 15:32:00 +0100 Subject: Added syslog udp. --- debug_syslog.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ debug_syslog.h | 37 ++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 debug_syslog.c create mode 100644 debug_syslog.h 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 +#include +#include + +#include +#include +#include +#include +#include + +#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__*/ -- cgit v1.2.3