Skip to content

Commit ea4ff53

Browse files
committed
Logs are written to log files
1 parent 62e4e90 commit ea4ff53

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

pg_probackup.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ main(int argc, char *argv[])
228228

229229
join_path_components(arclog_path, backup_path, "wal");
230230

231+
log_filename = "pg_probackup-%Y-%m-%d_%H%M%S.log";
232+
join_path_components(log_path, backup_path, "log");
233+
231234
/* setup exclusion list for file search */
232235
if (!backup_logs)
233236
{

utils/logger.c

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
*-------------------------------------------------------------------------
88
*/
99

10-
#include "postgres_fe.h"
11-
#include "pgtime.h"
12-
1310
#include <errno.h>
1411
#include <pthread.h>
1512
#include <stdio.h>
1613
#include <string.h>
14+
#include <sys/stat.h>
1715
#include <time.h>
1816

1917
#include "logger.h"
@@ -25,6 +23,7 @@ int log_level = INFO;
2523
char *log_filename = NULL;
2624
char *error_log_filename = NULL;
2725
char *log_directory = NULL;
26+
char log_path[MAXPGPATH] = "";
2827

2928
int log_rotation_size = 0;
3029
int log_rotation_age = 0;
@@ -47,7 +46,7 @@ static void elog_internal(int elevel, const char *fmt, va_list args)
4746
/* Functions to work with log files */
4847
static void open_logfile(void);
4948
static void release_logfile(void);
50-
static char *logfile_getname(pg_time_t timestamp);
49+
static char *logfile_getname(time_t timestamp);
5150
static FILE *logfile_open(const char *filename, const char *mode);
5251

5352
/* Static variables */
@@ -60,43 +59,47 @@ static bool logging_to_file = false;
6059

6160
static pthread_mutex_t log_file_mutex = PTHREAD_MUTEX_INITIALIZER;
6261

63-
/*
64-
* Logs to stderr or to log file and exit if ERROR or FATAL.
65-
*
66-
* Actual implementation for elog() and pg_log().
67-
*/
6862
static void
69-
elog_internal(int elevel, const char *fmt, va_list args)
63+
write_elevel(FILE *stream, int elevel)
7064
{
71-
bool wrote_to_file = false;
72-
7365
switch (elevel)
7466
{
7567
case LOG:
76-
fputs("LOG: ", stderr);
68+
fputs("LOG: ", stream);
7769
break;
7870
case INFO:
79-
fputs("INFO: ", stderr);
71+
fputs("INFO: ", stream);
8072
break;
8173
case NOTICE:
82-
fputs("NOTICE: ", stderr);
74+
fputs("NOTICE: ", stream);
8375
break;
8476
case WARNING:
85-
fputs("WARNING: ", stderr);
77+
fputs("WARNING: ", stream);
8678
break;
8779
case ERROR:
88-
fputs("ERROR: ", stderr);
80+
fputs("ERROR: ", stream);
8981
break;
9082
case FATAL:
91-
fputs("FATAL: ", stderr);
83+
fputs("FATAL: ", stream);
9284
break;
9385
case PANIC:
94-
fputs("PANIC: ", stderr);
86+
fputs("PANIC: ", stream);
9587
break;
9688
default:
9789
elog(ERROR, "invalid logging level: %d", elevel);
9890
break;
9991
}
92+
}
93+
94+
/*
95+
* Logs to stderr or to log file and exit if ERROR or FATAL.
96+
*
97+
* Actual implementation for elog() and pg_log().
98+
*/
99+
static void
100+
elog_internal(int elevel, const char *fmt, va_list args)
101+
{
102+
bool wrote_to_file = false;
100103

101104
pthread_mutex_lock(&log_file_mutex);
102105

@@ -112,6 +115,8 @@ elog_internal(int elevel, const char *fmt, va_list args)
112115
if (log_file == NULL)
113116
open_logfile();
114117

118+
write_elevel(log_file, elevel);
119+
115120
vfprintf(log_file, fmt, args);
116121
fputc('\n', log_file);
117122
fflush(log_file);
@@ -127,6 +132,8 @@ elog_internal(int elevel, const char *fmt, va_list args)
127132
if (!wrote_to_file ||
128133
elevel >= ERROR)
129134
{
135+
write_elevel(stderr, elevel);
136+
130137
vfprintf(stderr, fmt, args);
131138
fputc('\n', stderr);
132139
fflush(stderr);
@@ -203,20 +210,24 @@ pg_log(eLogType type, const char *fmt, ...)
203210
* Result is palloc'd.
204211
*/
205212
static char *
206-
logfile_getname(pg_time_t timestamp)
213+
logfile_getname(time_t timestamp)
207214
{
208215
char *filename;
209216
size_t len;
217+
struct tm *tm = localtime(&timestamp);
218+
219+
if (log_path[0] == '\0')
220+
elog(ERROR, "logging path is not set");
210221

211222
filename = (char *) palloc(MAXPGPATH);
212223

213-
snprintf(filename, MAXPGPATH, "%s/", log_directory);
224+
snprintf(filename, MAXPGPATH, "%s/", log_path);
214225

215226
len = strlen(filename);
216227

217-
/* treat pgaudit.log_filename as a strftime pattern */
218-
pg_strftime(filename + len, MAXPGPATH - len, log_filename,
219-
pg_localtime(&timestamp, log_timezone));
228+
/* Treat log_filename as a strftime pattern */
229+
if (strftime(filename + len, MAXPGPATH - len, log_filename, tm) <= 0)
230+
elog(ERROR, "strftime(%s) failed: %s", log_filename, strerror(errno));
220231

221232
return filename;
222233
}
@@ -229,6 +240,11 @@ logfile_open(const char *filename, const char *mode)
229240
{
230241
FILE *fh;
231242

243+
/*
244+
* Create log directory if not present; ignore errors
245+
*/
246+
mkdir(log_path, S_IRWXU);
247+
232248
fh = fopen(filename, mode);
233249

234250
if (fh)
@@ -257,7 +273,7 @@ open_logfile(void)
257273

258274
log_file = logfile_open(filename, "a");
259275

260-
if (last_file_name != NULL) /* probably shouldn't happen */
276+
if (last_file_name != NULL)
261277
pfree(last_file_name);
262278

263279
last_file_name = filename;

utils/logger.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#ifndef LOGGER_H
1111
#define LOGGER_H
1212

13+
#include "postgres_fe.h"
14+
1315
/* Log level */
1416
#define VERBOSE (-5)
1517
#define LOG (-4)
@@ -27,6 +29,7 @@ extern int log_level;
2729
extern char *log_filename;
2830
extern char *error_log_filename;
2931
extern char *log_directory;
32+
extern char log_path[MAXPGPATH];
3033

3134
extern int log_rotation_size;
3235
extern int log_rotation_age;

0 commit comments

Comments
 (0)