Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 311b7b1

Browse files
author
Arthur Zakirov
committed
Added --log parameter, each log entry has timestamp value now
1 parent a8ea8ab commit 311b7b1

File tree

7 files changed

+64
-30
lines changed

7 files changed

+64
-30
lines changed

src/configure.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ do_configure(bool show_only)
4141
if (replica_timeout != 300) /* 300 is default value */
4242
config->replica_timeout = replica_timeout;
4343

44-
if (log_level_defined)
45-
config->log_level = log_level;
44+
if (log_to_file != LOGGER_NONE)
45+
config->log_to_file = LOG_TO_FILE;
46+
if (log_level != LOGGER_NONE)
47+
config->log_level = LOG_LEVEL;
4648
if (log_filename)
4749
config->log_filename = log_filename;
4850
if (error_log_filename)
@@ -88,6 +90,7 @@ pgBackupConfigInit(pgBackupConfig *config)
8890
config->master_user = NULL;
8991
config->replica_timeout = INT_MIN; /* INT_MIN means "undefined" */
9092

93+
config->log_to_file = INT_MIN; /* INT_MIN means "undefined" */
9194
config->log_level = INT_MIN; /* INT_MIN means "undefined" */
9295
config->log_filename = NULL;
9396
config->error_log_filename = NULL;
@@ -132,6 +135,8 @@ writeBackupCatalogConfig(FILE *out, pgBackupConfig *config)
132135
fprintf(out, "replica_timeout = %d\n", config->replica_timeout);
133136

134137
fprintf(out, "#Logging parameters:\n");
138+
if (config->log_to_file != INT_MIN)
139+
fprintf(out, "log = %d\n", config->log_to_file);
135140
if (config->log_level != INT_MIN)
136141
fprintf(out, "log-level = %s\n", deparse_log_level(config->log_level));
137142
if (config->log_filename)
@@ -193,12 +198,13 @@ readBackupCatalogConfigFile(void)
193198
{ 'f', 36, "compress-algorithm", opt_compress_alg, SOURCE_CMDLINE },
194199
{ 'u', 37, "compress-level", &(config->compress_level), SOURCE_CMDLINE },
195200
/* logging options */
196-
{ 'f', 40, "log-level", opt_log_level, SOURCE_CMDLINE },
197-
{ 's', 41, "log-filename", &(config->log_filename), SOURCE_CMDLINE },
198-
{ 's', 42, "error-log-filename", &(config->error_log_filename), SOURCE_CMDLINE },
199-
{ 's', 43, "log-directory", &(config->log_directory), SOURCE_CMDLINE },
200-
{ 'u', 44, "log-rotation-size", &(config->log_rotation_size), SOURCE_CMDLINE },
201-
{ 'u', 45, "log-rotation-age", &(config->log_rotation_age), SOURCE_CMDLINE },
201+
{ 'i', 0, "log", &(config->log_to_file), SOURCE_CMDLINE },
202+
{ 'f', 0, "log-level", opt_log_level, SOURCE_CMDLINE },
203+
{ 's', 0, "log-filename", &(config->log_filename), SOURCE_CMDLINE },
204+
{ 's', 0, "error-log-filename", &(config->error_log_filename), SOURCE_CMDLINE },
205+
{ 's', 0, "log-directory", &(config->log_directory), SOURCE_CMDLINE },
206+
{ 'u', 0, "log-rotation-size", &(config->log_rotation_size), SOURCE_CMDLINE },
207+
{ 'u', 0, "log-rotation-age", &(config->log_rotation_age), SOURCE_CMDLINE },
202208
/* connection options */
203209
{ 's', 0, "pgdata", &(config->pgdata), SOURCE_FILE_STRICT },
204210
{ 's', 0, "pgdatabase", &(config->pgdatabase), SOURCE_FILE_STRICT },

src/pg_probackup.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ static pgut_option options[] =
135135
{ 'u', 37, "compress-level", &compress_level, SOURCE_CMDLINE },
136136
{ 'b', 38, "compress", &compress_shortcut, SOURCE_CMDLINE },
137137
/* logging options */
138+
{ 'b', 'l', "log", &log_to_file, SOURCE_CMDLINE },
138139
{ 'f', 40, "log-level", opt_log_level, SOURCE_CMDLINE },
139140
{ 's', 41, "log-filename", &log_filename, SOURCE_CMDLINE },
140141
{ 's', 42, "error-log-filename", &error_log_filename, SOURCE_CMDLINE },
@@ -304,14 +305,8 @@ main(int argc, char *argv[])
304305
if (pgdata != NULL && !is_absolute_path(pgdata))
305306
elog(ERROR, "-D, --pgdata must be an absolute path");
306307

307-
/* Set log path */
308-
if (log_filename || error_log_filename)
309-
{
310-
if (log_directory)
311-
strcpy(log_path, log_directory);
312-
else
313-
join_path_components(log_path, backup_path, "log");
314-
}
308+
/* Initialize logger */
309+
init_logger(backup_path);
315310

316311
/* Sanity check of --backup-id option */
317312
if (backup_id_string_param != NULL)
@@ -430,7 +425,6 @@ static void
430425
opt_log_level(pgut_option *opt, const char *arg)
431426
{
432427
log_level = parse_log_level(arg);
433-
log_level_defined = true;
434428
}
435429

436430
CompressAlg

src/pg_probackup.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ typedef struct pgBackupConfig
155155
const char *master_user;
156156
int replica_timeout;
157157

158+
int log_to_file;
158159
int log_level;
159160
char *log_filename;
160161
char *error_log_filename;

src/restore.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ restore_backup(pgBackup *backup)
353353
parray_walk(files, pgFileFree);
354354
parray_free(files);
355355

356-
if (log_level <= LOG)
356+
if (LOG_LEVEL <= LOG)
357357
{
358358
char *backup_id;
359359

@@ -396,7 +396,7 @@ remove_deleted_files(pgBackup *backup)
396396
if (parray_bsearch(files, file, pgFileComparePathDesc) == NULL)
397397
{
398398
pgFileDelete(file);
399-
if (log_level <= LOG)
399+
if (LOG_LEVEL <= LOG)
400400
elog(LOG, "deleted %s", GetRelativePath(file->path, pgdata));
401401
}
402402
}
@@ -579,7 +579,7 @@ check_tablespace_mapping(pgBackup *backup)
579579
pgBackupGetPath(backup, this_backup_path, lengthof(this_backup_path), NULL);
580580
read_tablespace_map(links, this_backup_path);
581581

582-
if (log_level <= LOG)
582+
if (LOG_LEVEL <= LOG)
583583
{
584584
char *backup_id;
585585

src/utils/logger.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
/* Logger parameters */
2121

22-
int log_level = INFO;
23-
bool log_level_defined = false;
22+
int log_to_file = LOGGER_NONE;
23+
int log_level = LOGGER_NONE;
2424

2525
char *log_filename = NULL;
2626
char *error_log_filename = NULL;
@@ -68,6 +68,19 @@ static bool logging_to_file = false;
6868

6969
static pthread_mutex_t log_file_mutex = PTHREAD_MUTEX_INITIALIZER;
7070

71+
void
72+
init_logger(const char *root_path)
73+
{
74+
/* Set log path */
75+
if (LOG_TO_FILE || error_log_filename)
76+
{
77+
if (log_directory)
78+
strcpy(log_path, log_directory);
79+
else
80+
join_path_components(log_path, root_path, "log");
81+
}
82+
}
83+
7184
static void
7285
write_elevel(FILE *stream, int elevel)
7386
{
@@ -113,9 +126,11 @@ elog_internal(int elevel, const char *fmt, va_list args)
113126
write_to_stderr;
114127
va_list error_args,
115128
std_args;
129+
time_t log_time = (time_t) time(NULL);
130+
char strfbuf[128];
116131

117132
write_to_file = log_path[0] != '\0' && !logging_to_file &&
118-
(log_filename || error_log_filename);
133+
(LOG_TO_FILE || error_log_filename);
119134

120135
/*
121136
* There is no need to lock if this is elog() from upper elog() and
@@ -138,18 +153,28 @@ elog_internal(int elevel, const char *fmt, va_list args)
138153
if (write_to_stderr && write_to_file)
139154
va_copy(std_args, args);
140155

156+
if (write_to_file)
157+
strftime(strfbuf, sizeof(strfbuf), "%Y-%m-%d %H:%M:%S %Z",
158+
localtime(&log_time));
159+
141160
/*
142161
* Write message to log file.
143162
* Do not write to file if this error was raised during write previous
144163
* message.
145164
*/
146-
if (log_filename && write_to_file)
165+
if (LOG_TO_FILE && write_to_file)
147166
{
148167
logging_to_file = true;
149168

150169
if (log_file == NULL)
151-
open_logfile(&log_file, log_filename);
170+
{
171+
if (log_filename == NULL)
172+
open_logfile(&log_file, "pg_probackup.log");
173+
else
174+
open_logfile(&log_file, log_filename);
175+
}
152176

177+
fprintf(log_file, "%s: ", strfbuf);
153178
write_elevel(log_file, elevel);
154179

155180
vfprintf(log_file, fmt, args);
@@ -171,6 +196,7 @@ elog_internal(int elevel, const char *fmt, va_list args)
171196
if (error_log_file == NULL)
172197
open_logfile(&error_log_file, error_log_filename);
173198

199+
fprintf(log_file, "%s: ", strfbuf);
174200
write_elevel(error_log_file, elevel);
175201

176202
vfprintf(error_log_file, fmt, error_args);
@@ -220,7 +246,7 @@ elog(int elevel, const char *fmt, ...)
220246
* Do not log message if severity level is less than log_level.
221247
* It is the little optimisation to put it here not in elog_internal().
222248
*/
223-
if (elevel < log_level && elevel < ERROR)
249+
if (elevel < LOG_LEVEL && elevel < ERROR)
224250
return;
225251

226252
va_start(args, fmt);
@@ -261,7 +287,7 @@ pg_log(eLogType type, const char *fmt, ...)
261287
* Do not log message if severity level is less than log_level.
262288
* It is the little optimisation to put it here not in elog_internal().
263289
*/
264-
if (elevel < log_level && elevel < ERROR)
290+
if (elevel < LOG_LEVEL && elevel < ERROR)
265291
return;
266292

267293
va_start(args, fmt);

src/utils/logger.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
#include "postgres_fe.h"
1414

15+
#define LOGGER_NONE (-10)
16+
1517
/* Log level */
1618
#define VERBOSE (-5)
1719
#define LOG (-4)
@@ -24,8 +26,8 @@
2426

2527
/* Logger parameters */
2628

29+
extern int log_to_file;
2730
extern int log_level;
28-
extern bool log_level_defined;
2931

3032
extern char *log_filename;
3133
extern char *error_log_filename;
@@ -35,9 +37,14 @@ extern char log_path[MAXPGPATH];
3537
extern int log_rotation_size;
3638
extern int log_rotation_age;
3739

40+
#define LOG_TO_FILE ((log_to_file == LOGGER_NONE) ? false : (bool) log_to_file)
41+
#define LOG_LEVEL ((log_level == LOGGER_NONE) ? INFO : log_level)
42+
3843
#undef elog
3944
extern void elog(int elevel, const char *fmt, ...) pg_attribute_printf(2, 3);
4045

46+
extern void init_logger(const char *root_path);
47+
4148
extern int parse_log_level(const char *level);
4249
extern const char *deparse_log_level(int level);
4350

src/utils/pgut.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ pgut_execute(PGconn* conn, const char *query, int nParams, const char **params)
10921092
elog(ERROR, "interrupted");
10931093

10941094
/* write query to elog if verbose */
1095-
if (log_level <= LOG)
1095+
if (LOG_LEVEL <= LOG)
10961096
{
10971097
int i;
10981098

@@ -1141,7 +1141,7 @@ pgut_send(PGconn* conn, const char *query, int nParams, const char **params, int
11411141
elog(ERROR, "interrupted");
11421142

11431143
/* write query to elog if verbose */
1144-
if (log_level <= LOG)
1144+
if (LOG_LEVEL <= LOG)
11451145
{
11461146
int i;
11471147

0 commit comments

Comments
 (0)