|
@@ -24,6 +24,7 @@
|
24
|
24
|
#include <signal.h>
|
25
|
25
|
#include <fcntl.h>
|
26
|
26
|
#include <errno.h>
|
|
27
|
+#include <syslog.h>
|
27
|
28
|
|
28
|
29
|
#include "data.h"
|
29
|
30
|
#include "util.h"
|
|
@@ -49,9 +50,14 @@ static void LOG_func(const char *msg) {
|
49
|
50
|
fprintf(stderr, "%s | %s", date, msg);
|
50
|
51
|
}
|
51
|
52
|
|
|
53
|
+static void LOG_func_syslog(const char *msg) {
|
|
54
|
+ syslog(LOG_INFO, msg, strlen(msg));
|
|
55
|
+}
|
|
56
|
+
|
52
|
57
|
static const struct option long_options[] = {
|
53
|
58
|
{ "ident", required_argument, NULL, 'i' },
|
54
|
59
|
{ "daemon", required_argument, NULL, 'd' },
|
|
60
|
+ { "syslog", no_argument, NULL, 'S' },
|
55
|
61
|
{ "syslog-host", required_argument, NULL, 'l' },
|
56
|
62
|
{ "syslog-port", required_argument, NULL, 'L' },
|
57
|
63
|
{ "notify-program", required_argument, NULL, 'N' },
|
|
@@ -101,6 +107,7 @@ static void show_help(struct ts *ts) {
|
101
|
107
|
printf(" -d --daemon <pidfile> | Daemonize program and write pid file.\n");
|
102
|
108
|
printf(" -N --notify-program <prg> | Execute <prg> to report events. Default: empty\n");
|
103
|
109
|
printf("\n");
|
|
110
|
+ printf(" -S --syslog | Log messages using syslog.\n");
|
104
|
111
|
printf(" -l --syslog-host <host> | Syslog server address. Default: disabled\n");
|
105
|
112
|
printf(" -L --syslog-port <port> | Syslog server port. Default: %d\n", ts->syslog_port);
|
106
|
113
|
printf("\n");
|
|
@@ -209,10 +216,15 @@ static void parse_options(struct ts *ts, int argc, char **argv) {
|
209
|
216
|
ts->notify_program[sizeof(ts->notify_program) - 1] = 0;
|
210
|
217
|
break;
|
211
|
218
|
|
|
219
|
+ case 'S':
|
|
220
|
+ ts->syslog_active = 1;
|
|
221
|
+ ts->syslog_remote = 0;
|
|
222
|
+ break;
|
212
|
223
|
case 'l':
|
213
|
224
|
strncpy(ts->syslog_host, optarg, sizeof(ts->syslog_host) - 1);
|
214
|
225
|
ts->syslog_host[sizeof(ts->syslog_host) - 1] = 0;
|
215
|
226
|
ts->syslog_active = 1;
|
|
227
|
+ ts->syslog_remote = 1;
|
216
|
228
|
break;
|
217
|
229
|
case 'L':
|
218
|
230
|
ts->syslog_port = atoi(optarg);
|
|
@@ -368,9 +380,12 @@ static void parse_options(struct ts *ts, int argc, char **argv) {
|
368
|
380
|
ts_LOGf("Daemonize : %s pid file.\n", ts->pidfile);
|
369
|
381
|
else
|
370
|
382
|
ts_LOGf("Daemonize : no daemon\n");
|
371
|
|
- if (ts->syslog_active)
|
372
|
|
- ts_LOGf("Syslog : %s:%d\n", ts->syslog_host, ts->syslog_port);
|
373
|
|
- else
|
|
383
|
+ if (ts->syslog_active) {
|
|
384
|
+ if (ts->syslog_remote)
|
|
385
|
+ ts_LOGf("Syslog : %s:%d\n", ts->syslog_host, ts->syslog_port);
|
|
386
|
+ else
|
|
387
|
+ ts_LOGf("Syslog : enabled\n");
|
|
388
|
+ } else
|
374
|
389
|
ts_LOGf("Syslog : disabled\n");
|
375
|
390
|
|
376
|
391
|
if (ts->forced_caid)
|
|
@@ -539,8 +554,13 @@ int main(int argc, char **argv) {
|
539
|
554
|
if (!ts.syslog_active) {
|
540
|
555
|
ts_set_log_func(LOG_func);
|
541
|
556
|
} else {
|
542
|
|
- ts_set_log_func(LOG);
|
543
|
|
- log_init(ts.ident, ts.syslog_active, ts.daemonize != 1, ts.syslog_host, ts.syslog_port);
|
|
557
|
+ if (ts.syslog_remote) {
|
|
558
|
+ ts_set_log_func(LOG);
|
|
559
|
+ log_init(ts.ident, 1, 1, ts.syslog_host, ts.syslog_port);
|
|
560
|
+ } else {
|
|
561
|
+ openlog(ts.ident, LOG_NDELAY | LOG_PID, LOG_USER);
|
|
562
|
+ ts_set_log_func(LOG_func_syslog);
|
|
563
|
+ }
|
544
|
564
|
}
|
545
|
565
|
|
546
|
566
|
ts.notify = notify_alloc(&ts);
|
|
@@ -634,8 +654,12 @@ EXIT:
|
634
|
654
|
notify_sync(&ts, "STOP", "Stopping %s", program_id);
|
635
|
655
|
ts_LOGf("Stop %s\n", program_id);
|
636
|
656
|
|
637
|
|
- if (ts.syslog_active)
|
638
|
|
- log_close();
|
|
657
|
+ if (ts.syslog_active) {
|
|
658
|
+ if (ts.syslog_remote)
|
|
659
|
+ log_close();
|
|
660
|
+ else
|
|
661
|
+ closelog();
|
|
662
|
+ }
|
639
|
663
|
|
640
|
664
|
if (ts.daemonize)
|
641
|
665
|
unlink(ts.pidfile);
|