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