1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2024-12-04 10:44:46 +02:00

Logging to error-log-file works

This commit is contained in:
Arthur Zakirov 2017-04-27 14:47:57 +03:00
parent 9e8b171116
commit cfe9b85b9b

View File

@ -44,15 +44,16 @@ static void elog_internal(int elevel, const char *fmt, va_list args)
pg_attribute_printf(2, 0); pg_attribute_printf(2, 0);
/* Functions to work with log files */ /* Functions to work with log files */
static void open_logfile(void); static void open_logfile(FILE **file, const char *filename_format);
static void release_logfile(void); static void release_logfile(void);
static char *logfile_getname(time_t timestamp); static char *logfile_getname(const char *format, time_t timestamp);
static FILE *logfile_open(const char *filename, const char *mode); static FILE *logfile_open(const char *filename, const char *mode);
/* Static variables */ /* Static variables */
static FILE *log_file = NULL; static FILE *log_file = NULL;
static char *last_file_name = NULL; static FILE *error_log_file = NULL;
static bool exit_hook_registered = false; static bool exit_hook_registered = false;
/* Logging to file is in progress */ /* Logging to file is in progress */
static bool logging_to_file = false; static bool logging_to_file = false;
@ -113,7 +114,7 @@ elog_internal(int elevel, const char *fmt, va_list args)
logging_to_file = true; logging_to_file = true;
if (log_file == NULL) if (log_file == NULL)
open_logfile(); open_logfile(&log_file, log_filename);
write_elevel(log_file, elevel); write_elevel(log_file, elevel);
@ -125,6 +126,27 @@ elog_internal(int elevel, const char *fmt, va_list args)
wrote_to_file = true; wrote_to_file = true;
} }
/*
* Write error message to error log file.
* Do not write to file if this error was raised during write previous
* message.
*/
if (elevel >= ERROR && error_log_filename && !logging_to_file)
{
logging_to_file = true;
if (error_log_file == NULL)
open_logfile(&error_log_file, error_log_filename);
write_elevel(error_log_file, elevel);
vfprintf(error_log_file, fmt, args);
fputc('\n', error_log_file);
fflush(error_log_file);
logging_to_file = false;
}
/* /*
* Write to stderr if the message was not written to log file. * Write to stderr if the message was not written to log file.
* Write to stderr if the message level is greater than WARNING anyway. * Write to stderr if the message level is greater than WARNING anyway.
@ -249,7 +271,7 @@ parse_log_level(const char *level)
* Result is palloc'd. * Result is palloc'd.
*/ */
static char * static char *
logfile_getname(time_t timestamp) logfile_getname(const char *format, time_t timestamp)
{ {
char *filename; char *filename;
size_t len; size_t len;
@ -265,8 +287,8 @@ logfile_getname(time_t timestamp)
len = strlen(filename); len = strlen(filename);
/* Treat log_filename as a strftime pattern */ /* Treat log_filename as a strftime pattern */
if (strftime(filename + len, MAXPGPATH - len, log_filename, tm) <= 0) if (strftime(filename + len, MAXPGPATH - len, format, tm) <= 0)
elog(ERROR, "strftime(%s) failed: %s", log_filename, strerror(errno)); elog(ERROR, "strftime(%s) failed: %s", format, strerror(errno));
return filename; return filename;
} }
@ -304,18 +326,13 @@ logfile_open(const char *filename, const char *mode)
* Open the log file. * Open the log file.
*/ */
static void static void
open_logfile(void) open_logfile(FILE **file, const char *filename_format)
{ {
char *filename; char *filename;
filename = logfile_getname(time(NULL)); filename = logfile_getname(filename_format, time(NULL));
*file = logfile_open(filename, "a");
log_file = logfile_open(filename, "a"); pfree(filename);
if (last_file_name != NULL)
pfree(last_file_name);
last_file_name = filename;
/* /*
* Arrange to close opened file at proc_exit. * Arrange to close opened file at proc_exit.
@ -338,6 +355,9 @@ release_logfile(void)
fclose(log_file); fclose(log_file);
log_file = NULL; log_file = NULL;
} }
if (last_file_name != NULL) if (error_log_file)
pfree(last_file_name); {
fclose(error_log_file);
error_log_file = NULL;
}
} }