prefer-ubik-print-to-printf-20010403
[openafs.git] / src / util / serverLog.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*  serverLog.c     - Server logging                                      */
11 /*                                                                        */
12 /*  Information Technology Center                                         */
13 /*  Date: 05/21/97                                                        */
14 /*                                                                        */
15 /*  Function    - These routiens implement logging from the servers       */
16 /*                                                                        */
17 /* ********************************************************************** */
18
19 #include <afs/param.h>
20 #include <stdio.h>
21 #ifdef AFS_NT40_ENV
22 #include <io.h>
23 #include <time.h>
24 #else
25 #ifdef AFS_AIX_ENV
26 #include <time.h>
27 #endif
28 #include <sys/param.h>
29 #include <sys/time.h>
30 #include <syslog.h>
31 #endif
32 #include <afs/procmgmt.h>  /* signal(), kill(), wait(), etc. */
33 #include <fcntl.h>
34 #include <afs/stds.h>
35 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
36 #include <string.h>
37 #else
38 #include <strings.h>
39 #endif
40 #include "afsutil.h"
41 #include "fileutil.h"
42 #if defined(AFS_PTHREAD_ENV)
43 #include <assert.h>
44 #include <pthread.h>
45 static pthread_mutex_t serverLogMutex;
46 #define LOCK_SERVERLOG() assert(pthread_mutex_lock(&serverLogMutex)==0)
47 #define UNLOCK_SERVERLOG() assert(pthread_mutex_unlock(&serverLogMutex)==0)
48
49 #ifdef AFS_NT40_ENV
50 #define NULLDEV "NUL"
51 #else
52 #define NULLDEV "/dev/null"
53 #endif
54
55 #else /* AFS_PTHREAD_ENV */
56 #define LOCK_SERVERLOG() 
57 #define UNLOCK_SERVERLOG() 
58 #endif  /* AFS_PTHREAD_ENV */
59
60 #ifdef AFS_NT40_ENV
61 #define F_OK 0
62 #endif
63
64 char *threadname();
65
66 static int serverLogFD = -1;
67
68 #ifndef AFS_NT40_ENV
69 int serverLogSyslog = 0;
70 int serverLogSyslogFacility = LOG_DAEMON;
71 #endif
72
73 #include <stdarg.h>
74 int LogLevel;
75 int mrafsStyleLogs = 0;
76 int printLocks = 0;
77 static char ourName[MAXPATHLEN];
78
79 /* VARARGS1 */
80 void FSLog (const char *format, ...)
81 {
82     va_list args;
83
84     time_t currenttime;
85     char *timeStamp;
86     char tbuffer[1024];
87     char *info;
88     int len;
89     int i;
90     char *name;
91
92     currenttime = time(0);
93     timeStamp = afs_ctime(&currenttime, tbuffer, sizeof(tbuffer));
94     timeStamp[24] = ' ';  /* ts[24] is the newline, 25 is the null */
95     info = &timeStamp[25];
96
97     if (mrafsStyleLogs) {
98        name = threadname();
99        sprintf(info, "[%s] ", name);
100        info += strlen(info);
101     }
102
103     va_start(args, format);
104     (void) vsprintf(info, format, args);
105     va_end(args);
106
107     len = strlen(tbuffer);
108     LOCK_SERVERLOG();
109 #ifndef AFS_NT40_ENV
110     if ( serverLogSyslog ){
111         syslog(LOG_INFO, "%s", info);
112     } else 
113 #endif
114         if (serverLogFD > 0)
115             write(serverLogFD, tbuffer, len);
116     UNLOCK_SERVERLOG();
117
118 #if !defined(AFS_PTHREAD_ENV)
119     if ( ! serverLogSyslog ) {
120                 if ( serverLogFD > 0 )
121             fflush(serverLogFD); /* in case not on stdout/stderr */
122         fflush(stdout);
123         fflush(stderr);     /* in case they're sharing the same FD */
124     }
125 #endif
126 }
127
128 static void DebugOn(int loglevel)
129 {
130     if (loglevel == 0) {
131         ViceLog(0, ("Reset Debug levels to 0\n"));
132     } else {
133         ViceLog(0, ("Set Debug On level = %d\n",loglevel));
134     }
135 } /*DebugOn*/
136
137
138
139 void SetDebug_Signal(int signo)
140 {
141     extern int IOMGR_SoftSig();
142
143     if (LogLevel > 0) {
144         LogLevel *= 5;
145     }
146     else {
147         LogLevel = 1;
148     }
149     printLocks = 2;
150 #if defined(AFS_PTHREAD_ENV)
151     DebugOn(LogLevel);
152 #else /* AFS_PTHREAD_ENV */
153     IOMGR_SoftSig(DebugOn, LogLevel);
154 #endif /* AFS_PTHREAD_ENV */
155
156     signal(signo, SetDebug_Signal);   /* on some platforms, this signal */
157                                       /* handler needs to be set again */
158 } /*SetDebug_Signal*/
159
160 void ResetDebug_Signal(int signo)
161 {
162     LogLevel = 0;
163
164     if (printLocks >0) --printLocks;
165 #if defined(AFS_PTHREAD_ENV)
166     DebugOn(LogLevel);
167 #else /* AFS_PTHREAD_ENV */
168     IOMGR_SoftSig(DebugOn, LogLevel);
169 #endif /* AFS_PTHREAD_ENV */
170
171     signal(signo, ResetDebug_Signal);   /* on some platforms, this signal */
172                                         /* handler needs to be set again */
173     if (mrafsStyleLogs)
174         OpenLog((char *)&ourName);
175 } /*ResetDebug_Signal*/
176
177
178 void SetupLogSignals(void)
179 {
180     signal(SIGHUP, ResetDebug_Signal);
181     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
182     signal(SIGTSTP, SetDebug_Signal);
183 }
184
185 int OpenLog(const char *fileName) 
186 {
187     /*
188      * This function should allow various libraries that inconsistently
189      * use stdout/stderr to all go to the same place
190      */
191     int tempfd;
192     char oldName[MAXPATHLEN];
193     struct timeval Start;
194     struct tm *TimeFields;
195     char FileName[MAXPATHLEN]; 
196
197     if ( serverLogSyslog ) {
198 #ifndef AFS_NT40_ENV
199         openlog(NULL, LOG_PID, serverLogSyslogFacility);
200         return;
201 #endif
202     }
203
204     if (mrafsStyleLogs) {
205         TM_GetTimeOfDay(&Start, 0);
206         TimeFields = localtime(&Start.tv_sec);
207         if (fileName) {
208             if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
209             strcpy((char *)&ourName, (char *) fileName);
210         }
211         sprintf(FileName, "%s.%d%02d%02d%02d%02d%02d", ourName,
212                 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, 
213                 TimeFields->tm_mday, TimeFields->tm_hour, 
214                 TimeFields->tm_min, TimeFields->tm_sec);
215         rename (fileName, FileName); /* don't check error code */
216         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666); 
217     } else {
218         strcpy(oldName, fileName);
219         strcat(oldName, ".old");
220
221         /* don't check error */
222         renamefile(fileName, oldName);
223         tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
224     }
225
226     if(tempfd < 0)
227     {
228         printf("Unable to open log file %s\n", fileName);
229         return -1;
230     }
231
232 #if defined(AFS_PTHREAD_ENV)
233     /* redirect stdout and stderr so random printf's don't write to data */
234     assert(freopen(NULLDEV, "w", stdout) != NULL);
235     assert(freopen(NULLDEV, "w", stderr) != NULL);
236
237     assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
238
239     serverLogFD = tempfd;
240 #else
241     close(tempfd); /* just checking.... */
242     freopen(fileName, "w", stdout);
243     freopen(fileName, "w", stderr);
244     serverLogFD = fileno(stdout);
245 #endif /* AFS_PTHREAD_ENV */
246
247     return 0;
248 } /*OpenLog*/
249
250 int ReOpenLog(const char *fileName) 
251 {
252 #if !defined(AFS_PTHREAD_ENV)
253     int tempfd;
254 #endif
255
256     if (access(fileName, F_OK)==0)
257         return 0; /* exists, no need to reopen. */
258
259     if ( serverLogSyslog ) {
260         return 0;
261     }
262
263 #if defined(AFS_PTHREAD_ENV)
264     LOCK_SERVERLOG();
265     if (serverLogFD > 0)
266         close(serverLogFD);
267     serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
268     UNLOCK_SERVERLOG();
269     return serverLogFD < 0 ? -1 : 0;
270 #else
271
272     tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
273     if(tempfd < 0)
274     {
275         printf("Unable to open log file %s\n", fileName);
276         return -1;
277     }
278     close(tempfd);
279
280     freopen(fileName, "a", stdout);
281     freopen(fileName, "a", stderr);
282     serverLogFD = fileno(stdout);
283   
284
285     return 0;
286 #endif /* AFS_PTHREAD_ENV */
287 }