summaryrefslogtreecommitdiff
path: root/hugin_syslog.c
diff options
context:
space:
mode:
authorJonas Suhr Christensen <jsc@umbraculum.org>2014-02-12 15:48:20 +0100
committerJonas Suhr Christensen <jsc@umbraculum.org>2014-02-12 15:48:20 +0100
commit0b293f1f2c1ac8823efba78c3ce62102db9bab59 (patch)
treedcda52cfd6319c13c99e1ef36c1439c686d97671 /hugin_syslog.c
parent708c973d837c8ddfd7e56c0c118b0a606363f731 (diff)
DNS lookup before trying to connect to host.
Cropping of to long messages.
Diffstat (limited to 'hugin_syslog.c')
-rw-r--r--hugin_syslog.c90
1 files changed, 36 insertions, 54 deletions
diff --git a/hugin_syslog.c b/hugin_syslog.c
index b4e5ceb..5981395 100644
--- a/hugin_syslog.c
+++ b/hugin_syslog.c
@@ -57,13 +57,8 @@ typedef int socket_t;
#define DO_NOT_USE_REMOTE -2
-#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_MAXMSGLEN 1024
+#define SYSLOG_MAXTIMELEN 32
#define SYSLOG_LENOFEXECNAME 256
static int hug_syslog_sock;
@@ -94,21 +89,22 @@ void hug_syslog_init(const char* host, int port)
wsastartup();
#endif
+
+ struct hostent *hp = gethostbyname(host);
+ if(!hp) {
+ fprintf(stderr, "Failed to get host by name\n");
+ return;
+ }
+
// printf("Initializing syslog module remote %s:%d\n", host, port);
if ( (hug_syslog_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
fprintf(stderr, "Failed to create socket\n");
return;
}
+
memset(&hug_syslog_sockaddr, 0, sizeof(hug_syslog_sockaddr));
hug_syslog_sockaddr.sin_family = AF_INET;
// hug_syslog_sockaddr.sin_addr.s_addr = inet_addr(host);
- struct hostent *hp = gethostbyname(host);
-
- if(!hp) {
- fprintf(stderr, "Failed to get host by name\n");
- return;
- }
-
memcpy(&(hug_syslog_sockaddr.sin_addr),*(hp->h_addr_list),sizeof(struct in_addr));
hug_syslog_sockaddr.sin_port = htons(port);
@@ -122,7 +118,11 @@ void hug_syslog_init(const char* host, int port)
FILE* f = fopen("/proc/self/cmdline", "r");
if(f) {
char* s = NULL;
+
+ // This will not include commandline parameters
+ // as they are separated by \0
s = fgets(buf, SYSLOG_LENOFEXECNAME, f);
+
(void)s;
fclose(f);
}
@@ -242,30 +242,30 @@ void hug_syslog_output(char* msg)
const time_t rawtime = time(NULL);
struct tm time;
hug_localtime(&rawtime, &time);
- char buftime[SYSLOG_TIMELEN];
- strftime(buftime, SYSLOG_TIMELEN, "%b %e %H:%M:%S ", &time);
+ char buftime[SYSLOG_MAXTIMELEN+1];
+ strftime(buftime, SYSLOG_MAXTIMELEN, "%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(hug_syslog_sock, buf, buf_len, 0, (struct sockaddr *) &hug_syslog_sockaddr,
- sizeof(hug_syslog_sockaddr))) != buf_len) {
- fprintf(stderr, "Failed to send message successfully: %s\n", strerror(errno));
+ int prio = 167;
+
+ char buf[SYSLOG_MAXMSGLEN+1];
+ buf[0] = '\0';
+ snprintf(buf, SYSLOG_MAXMSGLEN, "<%d>%s %s[%d]: ", prio, buftime, execname, pid);
+ int headerlen = strlen(buf);
+
+ int msglen = strlen(msg);
+ char* msgptr = msg;
+ while( (msgptr - msg) < msglen ) {
+ buf[headerlen] = '\0';
+ strncat(buf, msgptr, SYSLOG_MAXMSGLEN - headerlen);
+ int buflen = strlen(buf);
+
+ msgptr += buflen - headerlen;
+
+ if((sendto(hug_syslog_sock, buf, buflen, 0, (struct sockaddr *) &hug_syslog_sockaddr,
+ sizeof(hug_syslog_sockaddr))) != buflen) {
+ fprintf(stderr, "Failed to send message successfully: %s\n", strerror(errno));
+ }
}
}
@@ -282,21 +282,3 @@ void hug_syslog_close() {
}
#endif/*DISABLE_HUGIN*/
-
-#ifdef TEST_HUGIN_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_HUGIN_SYSLOG*/