logging-changes-for-large-files-20030619
[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         (void) 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        (void) afs_snprintf(info, (sizeof tbuffer) - strlen(tbuffer),
111                            "[%s] ", name);
112        info += strlen(info);
113     }
114
115     (void) afs_vsnprintf(info, (sizeof tbuffer) - strlen(tbuffer),
116                          format, args);
117
118     len = strlen(tbuffer);
119     LOCK_SERVERLOG();
120 #ifndef AFS_NT40_ENV
121     if ( serverLogSyslog ){
122         syslog(LOG_INFO, "%s", info);
123     } else 
124 #endif
125         if (serverLogFD > 0)
126             (void) write(serverLogFD, tbuffer, len);
127     UNLOCK_SERVERLOG();
128
129 #if !defined(AFS_PTHREAD_ENV) && !defined(AFS_NT40_ENV)
130     if ( ! serverLogSyslog ) {
131         fflush(stdout);
132         fflush(stderr);     /* in case they're sharing the same FD */
133     }
134 #endif
135 } /*vFSLog*/
136
137 /* VARARGS1 */
138 /*@printflike@*/
139 void FSLog(const char *format, ...)
140 {
141     va_list args;
142
143     va_start(args, format);
144     vFSLog(format, args);
145     va_end(args);
146 } /*FSLog*/
147
148 static int DebugOn(int loglevel)
149 {
150     if (loglevel == 0) {
151         ViceLog(0, ("Reset Debug levels to 0\n"));
152     } else {
153         ViceLog(0, ("Set Debug On level = %d\n",loglevel));
154     }
155     return 0;
156 } /*DebugOn*/
157
158
159
160 void SetDebug_Signal(int signo)
161 {
162     extern int IOMGR_SoftSig();
163
164     if (LogLevel > 0) {
165         LogLevel *= 5;
166     }
167     else {
168         LogLevel = 1;
169     }
170     printLocks = 2;
171 #if defined(AFS_PTHREAD_ENV)
172     DebugOn(LogLevel);
173 #else /* AFS_PTHREAD_ENV */
174     IOMGR_SoftSig(DebugOn, LogLevel);
175 #endif /* AFS_PTHREAD_ENV */
176
177     (void) signal(signo, SetDebug_Signal); /* on some platforms, this
178                                             * signal handler needs to
179                                             * be set again */
180 } /*SetDebug_Signal*/
181
182 void ResetDebug_Signal(int signo)
183 {
184     LogLevel = 0;
185
186     if (printLocks >0) --printLocks;
187 #if defined(AFS_PTHREAD_ENV)
188     DebugOn(LogLevel);
189 #else /* AFS_PTHREAD_ENV */
190     IOMGR_SoftSig(DebugOn, (void *)LogLevel);
191 #endif /* AFS_PTHREAD_ENV */
192
193     (void) signal(signo, ResetDebug_Signal);  /* on some platforms,
194                                                * this signal handler
195                                                * needs to be set
196                                                * again */
197     if (mrafsStyleLogs)
198         OpenLog((char *)&ourName);
199 } /*ResetDebug_Signal*/
200
201
202 void SetupLogSignals(void)
203 {
204     (void) signal(SIGHUP, ResetDebug_Signal);
205     /* Note that we cannot use SIGUSR1 -- Linux stole it for pthreads! */
206     (void) signal(SIGTSTP, SetDebug_Signal);
207 }
208
209 int OpenLog(const char *fileName) 
210 {
211     /*
212      * This function should allow various libraries that inconsistently
213      * use stdout/stderr to all go to the same place
214      */
215     int tempfd;
216     char oldName[MAXPATHLEN];
217     struct timeval Start;
218     struct tm *TimeFields;
219     char FileName[MAXPATHLEN]; 
220
221 #ifndef AFS_NT40_ENV
222     if ( serverLogSyslog ) {
223         openlog(serverLogSyslogTag, LOG_PID, serverLogSyslogFacility);
224         return(0);
225     }
226 #endif
227
228     if (mrafsStyleLogs) {
229         TM_GetTimeOfDay(&Start, 0);
230         TimeFields = localtime(&Start.tv_sec);
231         if (fileName) {
232             if (strncmp(fileName, (char *)&ourName, strlen(fileName)))
233             strcpy((char *)&ourName, (char *) fileName);
234         }
235         afs_snprintf(FileName, MAXPATHLEN, "%s.%d%02d%02d%02d%02d%02d",
236                      ourName,
237                 TimeFields->tm_year + 1900, TimeFields->tm_mon + 1, 
238                 TimeFields->tm_mday, TimeFields->tm_hour, 
239                 TimeFields->tm_min, TimeFields->tm_sec);
240         rename (fileName, FileName); /* don't check error code */
241         tempfd = open(fileName, O_WRONLY | O_TRUNC | O_CREAT, 0666); 
242     } else {
243         strcpy(oldName, fileName);
244         strcat(oldName, ".old");
245
246         /* don't check error */
247         renamefile(fileName, oldName);
248         tempfd = open(fileName, O_WRONLY|O_TRUNC|O_CREAT, 0666);
249     }
250
251     if(tempfd < 0)
252     {
253         printf("Unable to open log file %s\n", fileName);
254         return -1;
255     }
256
257 #if defined(AFS_PTHREAD_ENV)
258     /* redirect stdout and stderr so random printf's don't write to data */
259     assert(freopen(NULLDEV, "w", stdout) != NULL);
260     assert(freopen(NULLDEV, "w", stderr) != NULL);
261
262     assert(pthread_mutex_init(&serverLogMutex, NULL) == 0);
263
264     serverLogFD = tempfd;
265 #else
266     close(tempfd); /* just checking.... */
267     (void) freopen(fileName, "w", stdout);
268     (void) freopen(fileName, "w", stderr);
269     serverLogFD = fileno(stdout);
270 #endif /* AFS_PTHREAD_ENV */
271
272     return 0;
273 } /*OpenLog*/
274
275 int ReOpenLog(const char *fileName) 
276 {
277 #if !defined(AFS_PTHREAD_ENV)
278     int tempfd;
279 #endif
280
281     if (access(fileName, F_OK)==0)
282         return 0; /* exists, no need to reopen. */
283
284 #if !defined(AFS_NT40_ENV)
285     if ( serverLogSyslog ) {
286         return 0;
287     }
288 #endif
289
290 #if defined(AFS_PTHREAD_ENV)
291     LOCK_SERVERLOG();
292     if (serverLogFD > 0)
293         close(serverLogFD);
294     serverLogFD = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
295     UNLOCK_SERVERLOG();
296     return serverLogFD < 0 ? -1 : 0;
297 #else
298
299     tempfd = open(fileName,O_WRONLY|O_APPEND|O_CREAT, 0666);
300     if(tempfd < 0)
301     {
302         printf("Unable to open log file %s\n", fileName);
303         return -1;
304     }
305     close(tempfd);
306
307     (void) freopen(fileName, "a", stdout);
308     (void) freopen(fileName, "a", stderr);
309     serverLogFD = fileno(stdout);
310   
311
312     return 0;
313 #endif /* AFS_PTHREAD_ENV */
314 }