os: log via fd instead of FILE
Instead of maintaining both the logfile fd, as well as ANSI FILE pointer, simplify it to just a fd. Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
		
							parent
							
								
									081f7a53e0
								
							
						
					
					
						commit
						11fc023a72
					
				
							
								
								
									
										29
									
								
								os/log.c
								
								
								
								
							
							
						
						
									
										29
									
								
								os/log.c
								
								
								
								
							| 
						 | 
				
			
			@ -110,7 +110,6 @@ OR PERFORMANCE OF THIS SOFTWARE.
 | 
			
		|||
#define DEFAULT_LOG_VERBOSITY		0
 | 
			
		||||
#define DEFAULT_LOG_FILE_VERBOSITY	3
 | 
			
		||||
 | 
			
		||||
static FILE *logFile = NULL;
 | 
			
		||||
static int logFileFd = -1;
 | 
			
		||||
static Bool logFlush = FALSE;
 | 
			
		||||
static Bool logSync = FALSE;
 | 
			
		||||
| 
						 | 
				
			
			@ -236,18 +235,15 @@ LogInit(const char *fname, const char *backup)
 | 
			
		|||
                saved_log_backup = strdup(backup);
 | 
			
		||||
        } else
 | 
			
		||||
            logFileName = LogFilePrep(fname, backup, display);
 | 
			
		||||
        if ((logFile = fopen(logFileName, "w")) == NULL)
 | 
			
		||||
            FatalError("Cannot open log file \"%s\"\n", logFileName);
 | 
			
		||||
        setvbuf(logFile, NULL, _IONBF, 0);
 | 
			
		||||
 | 
			
		||||
        logFileFd = fileno(logFile);
 | 
			
		||||
        if ((logFileFd = open(logFileName, O_WRONLY | O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP)) == -1)
 | 
			
		||||
            FatalError("Cannot open log file \"%s\": %s\n", logFileName, strerror(errno));
 | 
			
		||||
 | 
			
		||||
        /* Flush saved log information. */
 | 
			
		||||
        if (saveBuffer && bufferSize > 0) {
 | 
			
		||||
            fwrite(saveBuffer, bufferPos, 1, logFile);
 | 
			
		||||
            fflush(logFile);
 | 
			
		||||
            write(logFileFd, saveBuffer, bufferPos);
 | 
			
		||||
#ifndef WIN32
 | 
			
		||||
            fsync(fileno(logFile));
 | 
			
		||||
            fsync(logFileFd);
 | 
			
		||||
#endif
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			@ -299,14 +295,13 @@ LogSetDisplay(void)
 | 
			
		|||
void
 | 
			
		||||
LogClose(enum ExitCode error)
 | 
			
		||||
{
 | 
			
		||||
    if (logFile) {
 | 
			
		||||
    if (logFileFd != -1) {
 | 
			
		||||
        int msgtype = (error == EXIT_NO_ERROR) ? X_INFO : X_ERROR;
 | 
			
		||||
        LogMessageVerb(msgtype, -1,
 | 
			
		||||
                "Server terminated %s (%d). Closing log file.\n",
 | 
			
		||||
                (error == EXIT_NO_ERROR) ? "successfully" : "with error",
 | 
			
		||||
                error);
 | 
			
		||||
        fclose(logFile);
 | 
			
		||||
        logFile = NULL;
 | 
			
		||||
        close(logFileFd);
 | 
			
		||||
        logFileFd = -1;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -570,24 +565,22 @@ LogSWrite(int verb, const char *buf, size_t len, Bool end_line)
 | 
			
		|||
                fsync(logFileFd);
 | 
			
		||||
#endif
 | 
			
		||||
        }
 | 
			
		||||
        else if (!inSignalContext && logFile) {
 | 
			
		||||
        else if (!inSignalContext && logFileFd != -1) {
 | 
			
		||||
            if (newline) {
 | 
			
		||||
                time_t t = time(NULL);
 | 
			
		||||
                struct tm tm;
 | 
			
		||||
                char fmt_tm[32];
 | 
			
		||||
 | 
			
		||||
                localtime_r(&t, &tm);
 | 
			
		||||
                strftime(fmt_tm, sizeof(fmt_tm) - 1, "%Y-%m-%d %H:%M:%S", &tm);
 | 
			
		||||
 | 
			
		||||
                fprintf(logFile, "[%s] ", fmt_tm);
 | 
			
		||||
                strftime(fmt_tm, sizeof(fmt_tm) - 1, "[%Y-%m-%d %H:%M:%S] ", &tm);
 | 
			
		||||
                write(logFileFd, fmt_tm, strlen(fmt_tm));
 | 
			
		||||
            }
 | 
			
		||||
            newline = end_line;
 | 
			
		||||
            fwrite(buf, len, 1, logFile);
 | 
			
		||||
            write(logFileFd, buf, len);
 | 
			
		||||
            if (logFlush) {
 | 
			
		||||
                fflush(logFile);
 | 
			
		||||
#ifndef WIN32
 | 
			
		||||
                if (logSync)
 | 
			
		||||
                    fsync(fileno(logFile));
 | 
			
		||||
                    fsync(logFileFd);
 | 
			
		||||
#endif
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue