summarylogtreecommitdiffstats
path: root/arpon-syslog.patch
blob: 0476de2413e385a7ecdaa6162fd4b8f20d3db33b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
diff --git a/ArpON-3.0-ng/src/main.c b/ArpON-3.0-ng/src/main.c
index 7045a13..23d9f0d 100644
--- a/ArpON-3.0-ng/src/main.c
+++ b/ArpON-3.0-ng/src/main.c
@@ -33,6 +33,9 @@
 
 #include <stdio.h>
 #include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
 
 #include "dmn.h"
 #include "env.h"
@@ -43,12 +46,33 @@
 #include "sig.h"
 #include "thd.h"
 
+int syslog_verbosity;
+
 /*
  * ArpON Main.
  */
 int
 main(int argc, char **argv, char **envp)
 {
+    char* verbosity = getenv("ARPON_SYSLOG_VERBOSITY");
+
+    if (verbosity == NULL)
+      syslog_verbosity = LOG_INFO;
+    else if (strcmp(verbosity, "CRIT") == 0)
+      syslog_verbosity = LOG_CRIT;
+    else if (strcmp(verbosity, "ERR") == 0)
+      syslog_verbosity = LOG_ERR;
+    else if (strcmp(verbosity, "WARNING") == 0)
+      syslog_verbosity = LOG_WARNING;
+    else if (strcmp(verbosity, "INFO") == 0)
+      syslog_verbosity = LOG_INFO;
+    else if (strcmp(verbosity, "DEBUG") == 0)
+      syslog_verbosity = LOG_DEBUG;
+    else
+      syslog_verbosity = LOG_INFO;
+
+    /* Initialize syslog. */
+    openlog("arpon", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_DAEMON);
 
     /* Audit the environment. */
     env_audit(argv, envp);
diff --git a/ArpON-3.0-ng/src/msg.c b/ArpON-3.0-ng/src/msg.c
index 77ee70d..174c6e2 100644
--- a/ArpON-3.0-ng/src/msg.c
+++ b/ArpON-3.0-ng/src/msg.c
@@ -42,6 +42,7 @@
 #include <errno.h>
 #include <assert.h>
 #include <pthread.h>
+#include <syslog.h>
 
 #include "config.h"
 #include "exit.h"
@@ -58,23 +59,12 @@
  */
 #define MSG_MESSAGESIZE     512     /* 512 bytes. */
 
-/*
- * Log file permissions to 640.
- */
-#define MSG_LOGPERMS        S_IRUSR | S_IWUSR | S_IRGRP
+extern int syslog_verbosity;
 
 /*
  * Function prototypes not exported.
  */
-static void msg_init(void);
-static void msg_destroy(void);
 static void msg_gettimestamp(char *ts);
-static void msg_putmessage(FILE *stream, const char *msg);
-
-/*
- * Initialize the log file stream.
- */
-static FILE *log = NULL;
 
 /*
  * Initialize the message mutex.
@@ -82,7 +72,7 @@ static FILE *log = NULL;
 static pthread_mutex_t msg_mtx = PTHREAD_MUTEX_INITIALIZER;
 
 /*
- * Initialize the log file stream and print the message with logging.
+ * Print the message.
  * The syntax of each message is:
  *
  * timestamp1 [log level] message1
@@ -93,21 +83,38 @@ static pthread_mutex_t msg_mtx = PTHREAD_MUTEX_INITIALIZER;
 void
 msg(FILE *stream, const char *level, const char *fmt, ...)
 {
-
     do {
         char ts[MSG_TIMESTAMPSIZE], msg[MSG_MESSAGESIZE];
         va_list ap;
         int len1, len2, tot_len = MSG_MESSAGESIZE;
+        int syslog_lvl;
 
         /* Lock the mutex of the message. */
         if (pthread_mutex_lock(&msg_mtx) != 0)
             break;
 
-        /* Log file stream of the messages already initialized and open? */
-        if (log == NULL) {
-            /* Initialize the log file stream of the messages. */
-            msg_init();
-        }
+        if (strcmp(level, "BUG") == 0)
+          syslog_lvl = LOG_CRIT;
+        else if (strcmp(level, "ERROR") == 0)
+          syslog_lvl = LOG_ERR;
+        else if (strcmp(level, "WARN") == 0)
+          syslog_lvl = LOG_WARNING;
+        else if (strcmp(level, "INFO") == 0)
+          syslog_lvl = LOG_INFO;
+        else if (strcmp(level, "DEBUG") == 0)
+          syslog_lvl = LOG_DEBUG;
+        else
+          syslog_lvl = LOG_NOTICE;
+
+        if (syslog_verbosity < syslog_lvl)
+            return;
+
+        va_start(ap, fmt);
+
+        /* Print the message to syslog. */
+        vsyslog(syslog_lvl, fmt, ap);
+
+        va_end(ap);
 
         /* Get the timestamp of the message. */
         msg_gettimestamp(ts);
@@ -139,125 +146,17 @@ msg(FILE *stream, const char *level, const char *fmt, ...)
         /* No message truncated. */
         assert(tot_len > 0);
 
-        /* Print the message in the log file stream and the file stream. */
-        msg_putmessage(stream, msg);
-
-        /* Unlock the mutex of the message. */
-        if (pthread_mutex_unlock(&msg_mtx) != 0)
-            break;
-
-        return;
-    } while (0);
-
-    ERROR("%s", strerror(errno));
-    exit(EXIT_FAILURE);
-}
-
-/*
- * Initialize the log file stream of the messages.
- */
-static void
-msg_init(void)
-{
-
-    do {
-        struct stat stats;
-        bool UNUSED(logcreate) = false;
-
-        /* Check if the log file exist. */
-        if (stat(LOG_FILE, &stats) < 0) {
-            if (errno == ENOENT) {
-                int fd;
-
-                /* Create and open the log file with the 640 perms. */
-                if ((fd = open(LOG_FILE, O_CREAT, MSG_LOGPERMS)) < 0)
-                    break;
-
-                /* Close the log file descriptor. */
-                if (close(fd) < 0)
-                    break;
-
-                /* Call again. */
-                if (stat(LOG_FILE, &stats) < 0)
-                    break;
-
-#ifndef NDEBUG
-                /* Log file created. */
-                logcreate = true;
-#endif  /* !NDEBUG */
-            } else {
-                break;
-            }
-        }
-
-        /* Check if the log file is a regular file. */
-        if (S_ISREG(stats.st_mode) == 0) {
-            ERROR("%s is not a regular file", LOG_FILE);
-            exit(EXIT_FAILURE);
-        }
-
-        /* Fix the log file perms to 640. */
-        if (chmod(LOG_FILE, MSG_LOGPERMS) < 0)
-            break;
+        /* Print the message in the file stream. */
+        fprintf(stream, "%s", msg);
 
-        /* Open the log file stream to append. */
-        if ((log = fopen(LOG_FILE, "a")) == NULL)
+        /* Flush the file stream. */
+        if (fflush(stream) == EOF)
             break;
 
-#ifndef NDEBUG
         /* Unlock the mutex of the message. */
         if (pthread_mutex_unlock(&msg_mtx) != 0)
             break;
 
-        /* Log file created? */
-        if (logcreate == true)
-            MSG_DEBUG("Create %s with 640 perms successful", LOG_FILE);
-#endif  /* !NDEBUG */
-
-        MSG_DEBUG("Open %s successful", LOG_FILE);
-        MSG_DEBUG("Start logging");
-
-        /* Push msg_destroy() to be called on exit_cleanup(). */
-        exit_push(msg_destroy, "msg_destroy");
-
-#ifndef NDEBUG
-        /* Lock the mutex of the message. */
-        if (pthread_mutex_lock(&msg_mtx) != 0)
-            break;
-#endif  /* !NDEBUG */
-
-        return;
-    } while (0);
-
-    ERROR("%s", strerror(errno));
-    exit(EXIT_FAILURE);
-}
-
-/*
- * Destroy the log file stream of the messages.
- */
-static void
-msg_destroy(void)
-{
-
-    do {
-        /* Unlock the mutex of the message before the destruction. */
-        if (pthread_mutex_unlock(&msg_mtx) != 0)
-            break;
-
-        /* Log file stream of the messages already destroyed and closed? */
-        if (log != NULL) {
-            MSG_DEBUG("End logging");
-            MSG_DEBUG("Close %s successful", LOG_FILE);
-
-            /* Close the log file stream. */
-            if (fclose(log) == EOF)
-                break;
-
-            /* Set the log file stream to NULL. */
-            log = NULL;
-        }
-
         return;
     } while (0);
 
@@ -299,64 +198,6 @@ msg_gettimestamp(char *ts)
     exit(EXIT_FAILURE);
 }
 
-/*
- * Put the message in the log file stream and the file stream.
- */
-static void
-msg_putmessage(FILE *stream, const char *msg)
-{
-
-    do {
-        struct stat stats;
-
-        /* Check if the log file exist. */
-        if (stat(LOG_FILE, &stats) < 0) {
-            if (errno == ENOENT) {
-                /* Close the log file stream. */
-                if (fclose(log) == EOF)
-                    break;
-
-                /* Set the log file stream to NULL. */
-                log = NULL;
-
-                /* Re-initialize the log file stream. */
-                msg_init();
-
-                /* Call again. */
-                if (stat(LOG_FILE, &stats) < 0)
-                    break;
-            } else {
-                break;
-            }
-        }
-
-        /* Check if the log file is a regular file. */
-        if (S_ISREG(stats.st_mode) == 0) {
-            ERROR("%s is not a regular file", LOG_FILE);
-            exit(EXIT_FAILURE);
-        }
-
-        /* Print the message in the log file stream. */
-        fprintf(log, "%s", msg);
-
-        /* Flush the log file stream. */
-        if (fflush(log) == EOF)
-            break;
-
-        /* Print the message in the file stream. */
-        fprintf(stream, "%s", msg);
-
-        /* Flush the file stream. */
-        if (fflush(stream) == EOF)
-            break;
-
-        return;
-    } while (0);
-
-    ERROR("%s", strerror(errno));
-    exit(EXIT_FAILURE);
-}
-
 /*
  * EOF
  *