Browse Source

Add --syslog parameter to enable local syslog logging.

Georgi Chorbadzhiyski 12 years ago
parent
commit
e56d85bfd8
4 changed files with 36 additions and 7 deletions
  1. 1
    0
      ChangeLog
  2. 1
    0
      data.h
  3. 3
    0
      tsdecrypt.1
  4. 31
    7
      tsdecrypt.c

+ 1
- 0
ChangeLog View File

@@ -1,4 +1,5 @@
1 1
 xxxx-xx-xx : Version -next
2
+ * Add --syslog parameter to enable local syslog logging.
2 3
  * Set thread names under Linux.
3 4
  * notify: Added no_code_word/code_word_ok events.
4 5
  * notify: Added input_timeout/input_ok events.

+ 1
- 0
data.h View File

@@ -147,6 +147,7 @@ struct ts {
147 147
 	char				syslog_host[128];
148 148
 	int					syslog_port;
149 149
 	int					syslog_active;
150
+	int					syslog_remote;
150 151
 
151 152
 	int					daemonize;
152 153
 	char				pidfile[PATH_MAX];

+ 3
- 0
tsdecrypt.1 View File

@@ -29,6 +29,9 @@ and an example on how to create your own notification script.
29 29
 
30 30
 See \fBEVENTS\fR section for detailed description of the events.
31 31
 .TP
32
+\fB\-S\fR, \fB\-\-syslog\fR
33
+Write log messages to local syslog.
34
+.TP
32 35
 \fB\-l\fR, \fB\-\-syslog\-host\fR <addr>
33 36
 Set syslog host. tsdecrypt sends messages to this host over tcp in
34 37
 syslog compatible format. syslog-ng was tested as receiving syslog server.

+ 31
- 7
tsdecrypt.c View File

@@ -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);

Loading…
Cancel
Save