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:
Enrico Weigelt, metux IT consult 2024-09-09 15:18:41 +02:00
parent 081f7a53e0
commit 11fc023a72

View File

@ -110,7 +110,6 @@ OR PERFORMANCE OF THIS SOFTWARE.
#define DEFAULT_LOG_VERBOSITY 0 #define DEFAULT_LOG_VERBOSITY 0
#define DEFAULT_LOG_FILE_VERBOSITY 3 #define DEFAULT_LOG_FILE_VERBOSITY 3
static FILE *logFile = NULL;
static int logFileFd = -1; static int logFileFd = -1;
static Bool logFlush = FALSE; static Bool logFlush = FALSE;
static Bool logSync = FALSE; static Bool logSync = FALSE;
@ -236,18 +235,15 @@ LogInit(const char *fname, const char *backup)
saved_log_backup = strdup(backup); saved_log_backup = strdup(backup);
} else } else
logFileName = LogFilePrep(fname, backup, display); 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. */ /* Flush saved log information. */
if (saveBuffer && bufferSize > 0) { if (saveBuffer && bufferSize > 0) {
fwrite(saveBuffer, bufferPos, 1, logFile); write(logFileFd, saveBuffer, bufferPos);
fflush(logFile);
#ifndef WIN32 #ifndef WIN32
fsync(fileno(logFile)); fsync(logFileFd);
#endif #endif
} }
} }
@ -299,14 +295,13 @@ LogSetDisplay(void)
void void
LogClose(enum ExitCode error) LogClose(enum ExitCode error)
{ {
if (logFile) { if (logFileFd != -1) {
int msgtype = (error == EXIT_NO_ERROR) ? X_INFO : X_ERROR; int msgtype = (error == EXIT_NO_ERROR) ? X_INFO : X_ERROR;
LogMessageVerb(msgtype, -1, LogMessageVerb(msgtype, -1,
"Server terminated %s (%d). Closing log file.\n", "Server terminated %s (%d). Closing log file.\n",
(error == EXIT_NO_ERROR) ? "successfully" : "with error", (error == EXIT_NO_ERROR) ? "successfully" : "with error",
error); error);
fclose(logFile); close(logFileFd);
logFile = NULL;
logFileFd = -1; logFileFd = -1;
} }
} }
@ -570,24 +565,22 @@ LogSWrite(int verb, const char *buf, size_t len, Bool end_line)
fsync(logFileFd); fsync(logFileFd);
#endif #endif
} }
else if (!inSignalContext && logFile) { else if (!inSignalContext && logFileFd != -1) {
if (newline) { if (newline) {
time_t t = time(NULL); time_t t = time(NULL);
struct tm tm; struct tm tm;
char fmt_tm[32]; char fmt_tm[32];
localtime_r(&t, &tm); localtime_r(&t, &tm);
strftime(fmt_tm, sizeof(fmt_tm) - 1, "%Y-%m-%d %H:%M:%S", &tm); strftime(fmt_tm, sizeof(fmt_tm) - 1, "[%Y-%m-%d %H:%M:%S] ", &tm);
write(logFileFd, fmt_tm, strlen(fmt_tm));
fprintf(logFile, "[%s] ", fmt_tm);
} }
newline = end_line; newline = end_line;
fwrite(buf, len, 1, logFile); write(logFileFd, buf, len);
if (logFlush) { if (logFlush) {
fflush(logFile);
#ifndef WIN32 #ifndef WIN32
if (logSync) if (logSync)
fsync(fileno(logFile)); fsync(logFileFd);
#endif #endif
} }
} }