blob: 66a206765f5ea34245dd2ac938b0ad0570d7c01e [file] [log] [blame]
jjakoa760e322003-04-11 09:43:22 +00001/*
2 * Syslog functions.
jjako0fe0df02004-09-17 11:30:40 +00003 * Copyright (C) 2003, 2004 Mondru AB.
jjakoa760e322003-04-11 09:43:22 +00004 *
5 * The contents of this file may be used under the terms of the GNU
6 * General Public License Version 2, provided that the above copyright
7 * notice and this permission notice is included in all copies or
8 * substantial portions of the software.
9 *
jjakoa760e322003-04-11 09:43:22 +000010 */
11
12#include <stdarg.h>
jjakoa760e322003-04-11 09:43:22 +000013#include <sys/socket.h>
14#include <netinet/in.h>
15#include <stdio.h>
16#include <syslog.h>
17#include <string.h>
18#include <netinet/in.h>
19#include <arpa/inet.h>
20
21#include "syserr.h"
22
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +010023static FILE* err_log;
24
25void sys_err_setlogfile(FILE* log)
26{
27 err_log = log;
28}
29
Harald Weltebed35df2011-11-02 13:06:18 +010030void sys_err(int pri, char *fn, int ln, int en, char *fmt, ...)
31{
32 va_list args;
33 char buf[SYSERR_MSGSIZE];
jjakoa760e322003-04-11 09:43:22 +000034
Harald Weltebed35df2011-11-02 13:06:18 +010035 va_start(args, fmt);
36 vsnprintf(buf, SYSERR_MSGSIZE, fmt, args);
37 va_end(args);
38 buf[SYSERR_MSGSIZE - 1] = 0; /* Make sure it is null terminated */
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +010039 if (en) {
40 if (err_log)
41 fprintf(err_log, "%s: %d: %d (%s) %s\n",
42 fn, ln, en, strerror(en), buf);
Harald Weltebed35df2011-11-02 13:06:18 +010043 syslog(pri, "%s: %d: %d (%s) %s", fn, ln, en, strerror(en),
44 buf);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +010045 } else {
46 if (err_log)
47 fprintf(err_log, "%s: %d: %s\n", fn, ln, buf);
Harald Weltebed35df2011-11-02 13:06:18 +010048 syslog(pri, "%s: %d: %s", fn, ln, buf);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +010049 }
jjakoa760e322003-04-11 09:43:22 +000050}
51
52void sys_errpack(int pri, char *fn, int ln, int en, struct sockaddr_in *peer,
Harald Weltebed35df2011-11-02 13:06:18 +010053 void *pack, unsigned len, char *fmt, ...)
54{
jjakoa760e322003-04-11 09:43:22 +000055
Harald Weltebed35df2011-11-02 13:06:18 +010056 va_list args;
57 char buf[SYSERR_MSGSIZE];
58 char buf2[SYSERR_MSGSIZE];
59 unsigned int n;
60 int pos;
61
62 va_start(args, fmt);
63 vsnprintf(buf, SYSERR_MSGSIZE, fmt, args);
64 va_end(args);
65 buf[SYSERR_MSGSIZE - 1] = 0;
66
67 snprintf(buf2, SYSERR_MSGSIZE,
68 "Packet from %s:%u, length: %d, content:",
69 inet_ntoa(peer->sin_addr), ntohs(peer->sin_port), len);
70 buf2[SYSERR_MSGSIZE - 1] = 0;
71 pos = strlen(buf2);
72 for (n = 0; n < len; n++) {
73 if ((pos + 4) < SYSERR_MSGSIZE) {
74 sprintf((buf2 + pos), " %02hhx",
75 ((unsigned char *)pack)[n]);
76 pos += 3;
77 }
78 }
79 buf2[pos] = 0;
80
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +010081 if (en) {
82 if (err_log)
83 fprintf(err_log, "%s: %d: %d (%s) %s. %s\n",
84 fn, ln, en, strerror(en), buf, buf2);
Harald Weltebed35df2011-11-02 13:06:18 +010085 syslog(pri, "%s: %d: %d (%s) %s. %s", fn, ln, en, strerror(en),
86 buf, buf2);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +010087 } else {
88 if (err_log)
89 fprintf(err_log, "%s: %d: %s. %s\n", fn, ln, buf, buf2);
Harald Weltebed35df2011-11-02 13:06:18 +010090 syslog(pri, "%s: %d: %s. %s", fn, ln, buf, buf2);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +010091 }
jjakoa760e322003-04-11 09:43:22 +000092
93}