Set KillMode=mixed in the systemd service file.
[tinc] / src / logger.c
1 /*
2     logger.c -- logging code
3     Copyright (C) 2004-2016 Guus Sliepen <guus@tinc-vpn.org>
4                   2004-2005 Ivo Timmermans
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include "conf.h"
24 #include "logger.h"
25
26 debug_t debug_level = DEBUG_NOTHING;
27 static logmode_t logmode = LOGMODE_STDERR;
28 static pid_t logpid;
29 extern char *logfilename;
30 static FILE *logfile = NULL;
31 #ifdef HAVE_MINGW
32 static HANDLE loghandle = NULL;
33 #endif
34 static const char *logident = NULL;
35
36 void openlogger(const char *ident, logmode_t mode) {
37         logident = ident;
38         logmode = mode;
39         
40         switch(mode) {
41                 case LOGMODE_STDERR:
42                         logpid = getpid();
43                         break;
44                 case LOGMODE_FILE:
45                         logpid = getpid();
46                         logfile = fopen(logfilename, "a");
47                         if(!logfile) {
48                                 fprintf(stderr, "Could not open log file %s: %s\n", logfilename, strerror(errno));
49                                 logmode = LOGMODE_NULL;
50                         }
51                         break;
52                 case LOGMODE_SYSLOG:
53 #ifdef HAVE_MINGW
54                         loghandle = RegisterEventSource(NULL, logident);
55                         if(!loghandle) {
56                                 fprintf(stderr, "Could not open log handle!");
57                                 logmode = LOGMODE_NULL;
58                         }
59                         break;
60 #else
61 #ifdef HAVE_SYSLOG_H
62                         openlog(logident, LOG_CONS | LOG_PID, LOG_DAEMON);
63                         break;
64 #endif
65 #endif
66                 case LOGMODE_NULL:
67                         break;
68         }
69 }
70
71 void reopenlogger() {
72         if(logmode != LOGMODE_FILE)
73                 return;
74
75         fflush(logfile);
76         FILE *newfile = fopen(logfilename, "a");
77         if(!newfile) {
78                 logger(LOG_ERR, "Unable to reopen log file %s: %s", logfilename, strerror(errno));
79                 return;
80         }
81         fclose(logfile);
82         logfile = newfile;
83 }
84
85 void logger(int priority, const char *format, ...) {
86         va_list ap;
87         char timestr[32] = "";
88         time_t now;
89
90         va_start(ap, format);
91
92         switch(logmode) {
93                 case LOGMODE_STDERR:
94                         vfprintf(stderr, format, ap);
95                         fprintf(stderr, "\n");
96                         fflush(stderr);
97                         break;
98                 case LOGMODE_FILE:
99                         now = time(NULL);
100                         strftime(timestr, sizeof timestr, "%Y-%m-%d %H:%M:%S", localtime(&now));
101                         fprintf(logfile, "%s %s[%ld]: ", timestr, logident, (long)logpid);
102                         vfprintf(logfile, format, ap);
103                         fprintf(logfile, "\n");
104                         fflush(logfile);
105                         break;
106                 case LOGMODE_SYSLOG:
107 #ifdef HAVE_MINGW
108                         {
109                                 char message[4096];
110                                 const char *messages[] = {message};
111                                 vsnprintf(message, sizeof(message), format, ap);
112                                 message[sizeof message - 1] = 0;
113                                 ReportEvent(loghandle, priority, 0, 0, NULL, 1, 0, messages, NULL);
114                         }
115 #else
116 #ifdef HAVE_SYSLOG_H
117 #ifdef HAVE_VSYSLOG
118                         vsyslog(priority, format, ap);
119 #else
120                         {
121                                 char message[4096];
122                                 vsnprintf(message, sizeof(message), format, ap);
123                                 syslog(priority, "%s", message);
124                         }
125 #endif
126                         break;
127 #endif
128 #endif
129                 case LOGMODE_NULL:
130                         break;
131         }
132
133         va_end(ap);
134 }
135
136 void closelogger(void) {
137         switch(logmode) {
138                 case LOGMODE_FILE:
139                         fclose(logfile);
140                         break;
141                 case LOGMODE_SYSLOG:
142 #ifdef HAVE_MINGW
143                         DeregisterEventSource(loghandle);
144                         break;
145 #else
146 #ifdef HAVE_SYSLOG_H
147                         closelog();
148                         break;
149 #endif
150 #endif
151                 case LOGMODE_NULL:
152                 case LOGMODE_STDERR:
153                         break;
154                         break;
155         }
156 }