ossp-pkg/sa/test.c
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <syslog.h>
#include <sys/utsname.h>
#include "sa.h"
/* see RFC3164 */
static void die(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "test:ERROR: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
exit(1);
}
int main(int argc, char *argv[])
{
sa_addr_t *ra;
sa_addr_t *la;
sa_t *sa;
char caBuf[1024];
int nBuf;
char caTime[15+1];
time_t now;
struct tm *tm;
char caHost;
struct utsname uts;
char *cp;
char caTag[32+1];
char *cpHost;
int n;
if (sa_u2a(&ra, "udp://%s:514", argv[1]) != SA_OK)
die("sa_u2a ra");
if (sa_u2a(&la, "udp://0.0.0.0:0") != SA_OK)
die("sa_u2a la");
if (sa_create(&sa) != SA_OK)
die("sa_create");
if (sa_bind(sa, la) != SA_OK)
die("sa_bind");
/* RFC3164: The BSD syslog Protocol; C. Lonvick; August 2001. */
now = time(NULL);
tm = localtime(&now);
strftime(caTime, sizeof(caTime), "%b %e %H:%M:%S", tm);
if (uname(&uts) == 0) {
cpHost = strdup(uts.nodename);
if ((cp = strchr(cpHost, '.')) != NULL)
*cp = '\0';
}
else
cpHost = strdup("0.0.0.0"); /* FIXME */
strcpy(caTag, "progname[12]: ");
if (strlen(caTag) > 32)
caTag[32] = '\0';
sprintf(caBuf, "<%d>%s %s %s%s", LOG_MAIL|LOG_EMERG, caTime, cpHost, caTag, argv[2]);
fprintf(stderr, "%s\n", caBuf);
nBuf = strlen(caBuf);
if (sa_writeto(sa, caBuf, nBuf, &n, ra) != SA_OK)
die("sa_writeto");
if (sa_destroy(sa) != SA_OK)
die("sa_destroy");
free(ra);
free(la);
return 0;
}