Skip to content

Commit 9e8b171

Browse files
committed
pg_probackup reads logging options
1 parent ea4ff53 commit 9e8b171

File tree

4 files changed

+126
-55
lines changed

4 files changed

+126
-55
lines changed

configure.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,24 @@ readBackupCatalogConfigFile(void)
103103

104104
pgut_option options[] =
105105
{
106-
/* configure options */
107-
{ 'U', 0, "system-identifier", &(config->system_identifier), SOURCE_FILE_STRICT },
106+
/* retention options */
107+
{ 'u', 0, "retention-redundancy", &(config->retention_redundancy),SOURCE_FILE_STRICT },
108+
{ 'u', 0, "retention-window", &(config->retention_window), SOURCE_FILE_STRICT },
109+
/* logging options */
110+
// { 'f', 40, "log-level", opt_log_level, SOURCE_CMDLINE },
111+
// { 's', 41, "log-filename", &log_filename, SOURCE_CMDLINE },
112+
// { 's', 42, "error-log-filename", &error_log_filename, SOURCE_CMDLINE },
113+
// { 's', 43, "log-directory", &log_directory, SOURCE_CMDLINE },
114+
// { 'u', 44, "log-rotation-size", &log_rotation_size, SOURCE_CMDLINE },
115+
// { 'u', 45, "log-rotation-age", &log_rotation_age, SOURCE_CMDLINE },
116+
/* connection options */
108117
{ 's', 0, "pgdata", &(config->pgdata), SOURCE_FILE_STRICT },
109118
{ 's', 0, "pgdatabase", &(config->pgdatabase), SOURCE_FILE_STRICT },
110119
{ 's', 0, "pghost", &(config->pghost), SOURCE_FILE_STRICT },
111120
{ 's', 0, "pgport", &(config->pgport), SOURCE_FILE_STRICT },
112121
{ 's', 0, "pguser", &(config->pguser), SOURCE_FILE_STRICT },
113-
{ 'u', 0, "retention-redundancy", &(config->retention_redundancy),SOURCE_FILE_STRICT },
114-
{ 'u', 0, "retention-window", &(config->retention_window), SOURCE_FILE_STRICT },
122+
/* other options */
123+
{ 'U', 0, "system-identifier", &(config->system_identifier), SOURCE_FILE_STRICT },
115124
{0}
116125
};
117126

pg_probackup.c

Lines changed: 72 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,35 @@ const char *PROGRAM_URL = "https://github.com/postgrespro/pg_probackup";
2121
const char *PROGRAM_EMAIL = "https://github.com/postgrespro/pg_probackup/issues";
2222

2323
/* path configuration */
24-
char *backup_path;
25-
char *pgdata;
26-
char arclog_path[MAXPGPATH];
24+
char *backup_path = NULL;
25+
char *pgdata = NULL;
26+
char arclog_path[MAXPGPATH] = "";
2727

2828
/* directory configuration */
2929
pgBackup current;
30-
ProbackupSubcmd backup_subcmd;
30+
ProbackupSubcmd backup_subcmd;
3131

32-
bool help = false;
32+
bool help = false;
3333

34-
char *backup_id_string_param = NULL;
35-
bool backup_logs = false;
34+
char *backup_id_string_param = NULL;
35+
bool backup_logs = false;
3636

37-
bool smooth_checkpoint;
38-
int num_threads = 1;
39-
bool stream_wal = false;
40-
bool from_replica = false;
41-
bool progress = false;
42-
bool delete_wal = false;
43-
bool delete_expired = false;
44-
bool apply_to_all = false;
45-
bool force_delete = false;
46-
uint32 archive_timeout = 300; /* Wait timeout for WAL segment archiving */
37+
bool smooth_checkpoint;
38+
int num_threads = 1;
39+
bool stream_wal = false;
40+
bool from_replica = false;
41+
bool progress = false;
42+
bool delete_wal = false;
43+
bool delete_expired = false;
44+
bool apply_to_all = false;
45+
bool force_delete = false;
46+
/* Wait timeout for WAL segment archiving */
47+
uint32 archive_timeout = 300;
4748

48-
uint64 system_identifier = 0;
49+
uint64 system_identifier = 0;
4950

50-
uint32 retention_redundancy = 0;
51-
uint32 retention_window = 0;
51+
uint32 retention_redundancy = 0;
52+
uint32 retention_window = 0;
5253

5354
/* restore configuration */
5455
static char *target_time;
@@ -57,48 +58,56 @@ static char *target_inclusive;
5758
static TimeLineID target_tli;
5859

5960
static void opt_backup_mode(pgut_option *opt, const char *arg);
61+
static void opt_log_level(pgut_option *opt, const char *arg);
6062

6163
static pgut_option options[] =
6264
{
6365
/* directory options */
64-
{ 'b', 1, "help", &help, SOURCE_CMDLINE },
65-
{ 's', 'D', "pgdata", &pgdata, SOURCE_CMDLINE },
66-
{ 's', 'B', "backup-path", &backup_path, SOURCE_CMDLINE },
66+
{ 'b', 1, "help", &help, SOURCE_CMDLINE },
67+
{ 's', 'D', "pgdata", &pgdata, SOURCE_CMDLINE },
68+
{ 's', 'B', "backup-path", &backup_path, SOURCE_CMDLINE },
6769
/* common options */
68-
{ 'u', 'j', "threads", &num_threads, SOURCE_CMDLINE },
69-
{ 'b', 8, "stream", &stream_wal, SOURCE_CMDLINE },
70-
{ 'b', 11, "progress", &progress, SOURCE_CMDLINE },
70+
{ 'u', 'j', "threads", &num_threads, SOURCE_CMDLINE },
71+
{ 'b', 1, "stream", &stream_wal, SOURCE_CMDLINE },
72+
{ 'b', 2, "progress", &progress, SOURCE_CMDLINE },
7173
{ 's', 'i', "backup-id", &backup_id_string_param, SOURCE_CMDLINE },
7274
/* backup options */
73-
{ 'b', 10, "backup-pg-log", &backup_logs, SOURCE_CMDLINE },
75+
{ 'b', 10, "backup-pg-log", &backup_logs, SOURCE_CMDLINE },
7476
{ 'f', 'b', "backup-mode", opt_backup_mode, SOURCE_CMDLINE },
7577
{ 'b', 'C', "smooth-checkpoint", &smooth_checkpoint, SOURCE_CMDLINE },
7678
{ 's', 'S', "slot", &replication_slot, SOURCE_CMDLINE },
77-
{ 'u', 2, "archive-timeout", &archive_timeout, SOURCE_CMDLINE },
78-
{ 'b', 19, "delete-expired", &delete_expired, SOURCE_CMDLINE },
79+
{ 'u', 11, "archive-timeout", &archive_timeout, SOURCE_CMDLINE },
80+
{ 'b', 12, "delete-expired", &delete_expired, SOURCE_CMDLINE },
7981
/* restore options */
80-
{ 's', 3, "time", &target_time, SOURCE_CMDLINE },
81-
{ 's', 4, "xid", &target_xid, SOURCE_CMDLINE },
82-
{ 's', 5, "inclusive", &target_inclusive, SOURCE_CMDLINE },
83-
{ 'u', 6, "timeline", &target_tli, SOURCE_CMDLINE },
82+
{ 's', 20, "time", &target_time, SOURCE_CMDLINE },
83+
{ 's', 21, "xid", &target_xid, SOURCE_CMDLINE },
84+
{ 's', 22, "inclusive", &target_inclusive, SOURCE_CMDLINE },
85+
{ 'u', 23, "timeline", &target_tli, SOURCE_CMDLINE },
8486
{ 'f', 'T', "tablespace-mapping", opt_tablespace_map, SOURCE_CMDLINE },
8587
/* delete options */
86-
{ 'b', 12, "wal", &delete_wal, SOURCE_CMDLINE },
87-
{ 'b', 16, "expired", &delete_expired, SOURCE_CMDLINE },
88-
{ 'b', 17, "all", &apply_to_all, SOURCE_CMDLINE },
88+
{ 'b', 30, "wal", &delete_wal, SOURCE_CMDLINE },
89+
{ 'b', 31, "expired", &delete_expired, SOURCE_CMDLINE },
90+
{ 'b', 32, "all", &apply_to_all, SOURCE_CMDLINE },
8991
/* TODO not implemented yet */
90-
{ 'b', 18, "force", &force_delete, SOURCE_CMDLINE },
91-
/* configure options */
92-
{ 'u', 13, "retention-redundancy", &retention_redundancy, SOURCE_CMDLINE },
93-
{ 'u', 14, "retention-window", &retention_window, SOURCE_CMDLINE },
94-
/* other */
95-
{ 'U', 15, "system-identifier", &system_identifier, SOURCE_FILE_STRICT },
96-
97-
{ 's', 'd', "pgdatabase" , &pgut_dbname, SOURCE_CMDLINE },
98-
{ 's', 'h', "pghost" , &host, SOURCE_CMDLINE },
99-
{ 's', 'p', "pgport" , &port, SOURCE_CMDLINE },
100-
{ 's', 'U', "pguser" , &username, SOURCE_CMDLINE },
101-
{ 'B', 'w', "no-password" , &prompt_password, SOURCE_CMDLINE },
92+
{ 'b', 33, "force", &force_delete, SOURCE_CMDLINE },
93+
/* retention options */
94+
{ 'u', 34, "retention-redundancy", &retention_redundancy, SOURCE_CMDLINE },
95+
{ 'u', 35, "retention-window", &retention_window, SOURCE_CMDLINE },
96+
/* logging options */
97+
{ 'f', 40, "log-level", opt_log_level, SOURCE_CMDLINE },
98+
{ 's', 41, "log-filename", &log_filename, SOURCE_CMDLINE },
99+
{ 's', 42, "error-log-filename", &error_log_filename, SOURCE_CMDLINE },
100+
{ 's', 43, "log-directory", &log_directory, SOURCE_CMDLINE },
101+
{ 'u', 44, "log-rotation-size", &log_rotation_size, SOURCE_CMDLINE },
102+
{ 'u', 45, "log-rotation-age", &log_rotation_age, SOURCE_CMDLINE },
103+
/* connection options */
104+
{ 's', 'd', "pgdatabase", &pgut_dbname, SOURCE_CMDLINE },
105+
{ 's', 'h', "pghost", &host, SOURCE_CMDLINE },
106+
{ 's', 'p', "pgport", &port, SOURCE_CMDLINE },
107+
{ 's', 'U', "pguser", &username, SOURCE_CMDLINE },
108+
{ 'B', 'w', "no-password", &prompt_password, SOURCE_CMDLINE },
109+
/* other options */
110+
{ 'U', 50, "system-identifier", &system_identifier, SOURCE_FILE_STRICT },
102111
{ 0 }
103112
};
104113

@@ -202,6 +211,15 @@ main(int argc, char *argv[])
202211
if (!is_absolute_path(backup_path))
203212
elog(ERROR, "-B, --backup-path must be an absolute path");
204213

214+
/* Set log path */
215+
if (log_filename || error_log_filename)
216+
{
217+
if (log_directory)
218+
join_path_components(log_path, backup_path, log_directory);
219+
else
220+
join_path_components(log_path, backup_path, "log");
221+
}
222+
205223
if (backup_id_string_param != NULL)
206224
{
207225
current.backup_id = base36dec(backup_id_string_param);
@@ -228,9 +246,6 @@ main(int argc, char *argv[])
228246

229247
join_path_components(arclog_path, backup_path, "wal");
230248

231-
log_filename = "pg_probackup-%Y-%m-%d_%H%M%S.log";
232-
join_path_components(log_path, backup_path, "log");
233-
234249
/* setup exclusion list for file search */
235250
if (!backup_logs)
236251
{
@@ -292,3 +307,9 @@ opt_backup_mode(pgut_option *opt, const char *arg)
292307
{
293308
current.backup_mode = parse_backup_mode(arg);
294309
}
310+
311+
static void
312+
opt_log_level(pgut_option *opt, const char *arg)
313+
{
314+
log_level = parse_log_level(arg);
315+
}

utils/logger.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,45 @@ pg_log(eLogType type, const char *fmt, ...)
204204
va_end(args);
205205
}
206206

207+
/*
208+
* Parses string representation of log level.
209+
*/
210+
int
211+
parse_log_level(const char *level)
212+
{
213+
const char *v = level;
214+
size_t len;
215+
216+
/* Skip all spaces detected */
217+
while (isspace((unsigned char)*v))
218+
v++;
219+
len = strlen(v);
220+
221+
if (len == 0)
222+
elog(ERROR, "log-level is empty");
223+
224+
if (pg_strncasecmp("verbose", v, len) == 0)
225+
return VERBOSE;
226+
else if (pg_strncasecmp("log", v, len) == 0)
227+
return LOG;
228+
else if (pg_strncasecmp("info", v, len) == 0)
229+
return INFO;
230+
else if (pg_strncasecmp("notice", v, len) == 0)
231+
return NOTICE;
232+
else if (pg_strncasecmp("warning", v, len) == 0)
233+
return WARNING;
234+
else if (pg_strncasecmp("error", v, len) == 0)
235+
return ERROR;
236+
else if (pg_strncasecmp("fatal", v, len) == 0)
237+
return FATAL;
238+
else if (pg_strncasecmp("panic", v, len) == 0)
239+
return PANIC;
240+
241+
/* Log level is invalid */
242+
elog(ERROR, "invalid log-level \"%s\"", level);
243+
return 0;
244+
}
245+
207246
/*
208247
* Construct logfile name using timestamp information.
209248
*

utils/logger.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ extern int log_rotation_age;
3737
#undef elog
3838
extern void elog(int elevel, const char *fmt, ...) pg_attribute_printf(2, 3);
3939

40+
extern int parse_log_level(const char *level);
41+
4042
#endif /* LOGGER_H */

0 commit comments

Comments
 (0)