Skip to content

Commit 5cd672d

Browse files
committed
[Issue #224] New flag '--color' for coloring the show plain backup info and console ERROR and WARNING messages
1 parent 075346b commit 5cd672d

File tree

12 files changed

+233
-16
lines changed

12 files changed

+233
-16
lines changed

doc/pgprobackup.xml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3566,7 +3566,7 @@ pg_probackup show-config -B <replaceable>backup_dir</replaceable> --instance <re
35663566
<title>show</title>
35673567
<programlisting>
35683568
pg_probackup show -B <replaceable>backup_dir</replaceable>
3569-
[--help] [--instance <replaceable>instance_name</replaceable> [-i <replaceable>backup_id</replaceable> | --archive]] [--format=plain|json]
3569+
[--help] [--instance <replaceable>instance_name</replaceable> [-i <replaceable>backup_id</replaceable> | --archive]] [--format=plain|json] [--color]
35703570
</programlisting>
35713571
<para>
35723572
Shows the contents of the backup catalog. If
@@ -3581,6 +3581,8 @@ pg_probackup show -B <replaceable>backup_dir</replaceable>
35813581
plain text. You can specify the
35823582
<literal>--format=json</literal> option to get the result
35833583
in the <acronym>JSON</acronym> format.
3584+
If <literal>--color</literal> flag is used with plain text format,
3585+
then output is colored.
35843586
</para>
35853587
<para>
35863588
For details on usage, see the sections
@@ -4603,6 +4605,16 @@ pg_probackup archive-get -B <replaceable>backup_dir</replaceable> --instance <re
46034605
</para>
46044606
<para>
46054607
<variablelist>
4608+
4609+
<varlistentry>
4610+
<term><option>--color</option></term>
4611+
<listitem>
4612+
<para>
4613+
Color the console log messages of <literal>warning</literal> and <literal>error</literal> levels.
4614+
</para>
4615+
</listitem>
4616+
</varlistentry>
4617+
46064618
<varlistentry>
46074619
<term><option>--log-level-console=<replaceable>log_level</replaceable></option></term>
46084620
<listitem>

src/backup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ do_backup(time_t start_time, pgSetBackupParams *set_backup_params,
843843

844844
elog(INFO, "Backup start, pg_probackup version: %s, instance: %s, backup ID: %s, backup mode: %s, "
845845
"wal mode: %s, remote: %s, compress-algorithm: %s, compress-level: %i",
846-
PROGRAM_VERSION, instance_name, base36enc(start_time), pgBackupGetBackupMode(&current),
846+
PROGRAM_VERSION, instance_name, base36enc(start_time), pgBackupGetBackupMode(&current, false),
847847
current.stream ? "STREAM" : "ARCHIVE", IsSshProtocol() ? "true" : "false",
848848
deparse_compress_alg(current.compress_alg), current.compress_level);
849849

src/catalog.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,22 @@ lock_backup(pgBackup *backup, bool strict)
331331
* Get backup_mode in string representation.
332332
*/
333333
const char *
334-
pgBackupGetBackupMode(pgBackup *backup)
334+
pgBackupGetBackupMode(pgBackup *backup, bool show_color)
335335
{
336-
return backupModes[backup->backup_mode];
336+
if (show_color)
337+
{
338+
/* color the Backup mode */
339+
char *mode = pgut_malloc(24); /* leaking memory here */
340+
341+
if (backup->backup_mode == BACKUP_MODE_FULL)
342+
snprintf(mode, 24, "%s%s%s", TC_GREEN_BOLD, backupModes[backup->backup_mode], TC_RESET);
343+
else
344+
snprintf(mode, 24, "%s%s%s", TC_BLUE_BOLD, backupModes[backup->backup_mode], TC_RESET);
345+
346+
return mode;
347+
}
348+
else
349+
return backupModes[backup->backup_mode];
337350
}
338351

339352
static bool
@@ -1684,7 +1697,7 @@ pgBackupWriteControl(FILE *out, pgBackup *backup)
16841697
char timestamp[100];
16851698

16861699
fio_fprintf(out, "#Configuration\n");
1687-
fio_fprintf(out, "backup-mode = %s\n", pgBackupGetBackupMode(backup));
1700+
fio_fprintf(out, "backup-mode = %s\n", pgBackupGetBackupMode(backup, false));
16881701
fio_fprintf(out, "stream = %s\n", backup->stream ? "true" : "false");
16891702
fio_fprintf(out, "compress-alg = %s\n",
16901703
deparse_compress_alg(backup->compress_alg));

src/delete.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ do_retention_internal(parray *backup_list, parray *to_keep_list, parray *to_purg
402402
/* TODO: add ancestor(chain full backup) ID */
403403
elog(INFO, "Backup %s, mode: %s, status: %s. Redundancy: %i/%i, Time Window: %ud/%ud. %s",
404404
base36enc(backup->start_time),
405-
pgBackupGetBackupMode(backup),
405+
pgBackupGetBackupMode(backup, false),
406406
status2str(backup->status),
407407
cur_full_backup_num,
408408
instance_config.retention_redundancy,

src/help.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ help_backup(void)
310310
printf(_(" (example: --note='backup before app update to v13.1')\n"));
311311

312312
printf(_("\n Logging options:\n"));
313+
printf(_(" --color color the error and warning console messages\n"));
313314
printf(_(" --log-level-console=log-level-console\n"));
314315
printf(_(" level for console logging (default: info)\n"));
315316
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -470,6 +471,7 @@ help_restore(void)
470471
printf(_(" -S, --primary-slot-name=slotname replication slot to be used for WAL streaming from the primary server\n"));
471472

472473
printf(_("\n Logging options:\n"));
474+
printf(_(" --color color the error and warning console messages\n"));
473475
printf(_(" --log-level-console=log-level-console\n"));
474476
printf(_(" level for console logging (default: info)\n"));
475477
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -536,6 +538,7 @@ help_validate(void)
536538
printf(_(" --skip-block-validation set to validate only file-level checksum\n"));
537539

538540
printf(_("\n Logging options:\n"));
541+
printf(_(" --color color the error and warning console messages\n"));
539542
printf(_(" --log-level-console=log-level-console\n"));
540543
printf(_(" level for console logging (default: info)\n"));
541544
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -580,6 +583,7 @@ help_checkdb(void)
580583
printf(_(" can be used only with '--amcheck' option\n"));
581584

582585
printf(_("\n Logging options:\n"));
586+
printf(_(" --color color the error and warning console messages\n"));
583587
printf(_(" --log-level-console=log-level-console\n"));
584588
printf(_(" level for console logging (default: info)\n"));
585589
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -620,7 +624,8 @@ help_show(void)
620624
printf(_(" --instance=instance_name show info about specific instance\n"));
621625
printf(_(" -i, --backup-id=backup-id show info about specific backups\n"));
622626
printf(_(" --archive show WAL archive information\n"));
623-
printf(_(" --format=format show format=PLAIN|JSON\n\n"));
627+
printf(_(" --format=format show format=PLAIN|JSON\n"));
628+
printf(_(" --color color the info for plain format\n\n"));
624629
}
625630

626631
static void
@@ -655,6 +660,7 @@ help_delete(void)
655660
printf(_(" --status=backup_status delete all backups with specified status\n"));
656661

657662
printf(_("\n Logging options:\n"));
663+
printf(_(" --color color the error and warning console messages\n"));
658664
printf(_(" --log-level-console=log-level-console\n"));
659665
printf(_(" level for console logging (default: info)\n"));
660666
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -697,6 +703,7 @@ help_merge(void)
697703
printf(_(" --progress show progress\n"));
698704

699705
printf(_("\n Logging options:\n"));
706+
printf(_(" --color color the error and warning console messages\n"));
700707
printf(_(" --log-level-console=log-level-console\n"));
701708
printf(_(" level for console logging (default: info)\n"));
702709
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));

src/pg_probackup.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ char *externaldir = NULL;
6767
static char *backup_id_string = NULL;
6868
int num_threads = 1;
6969
bool stream_wal = false;
70+
bool show_color = false;
7071
bool is_archive_cmd = false;
7172
pid_t my_pid = 0;
7273
__thread int my_thread_num = 1;
@@ -178,6 +179,7 @@ static ConfigOption cmd_options[] =
178179
{ 'b', 132, "progress", &progress, SOURCE_CMD_STRICT },
179180
{ 's', 'i', "backup-id", &backup_id_string, SOURCE_CMD_STRICT },
180181
{ 'b', 133, "no-sync", &no_sync, SOURCE_CMD_STRICT },
182+
{ 'b', 134, "color", &show_color, SOURCE_CMD_STRICT },
181183
/* backup options */
182184
{ 'b', 180, "backup-pg-log", &backup_logs, SOURCE_CMD_STRICT },
183185
{ 'f', 'b', "backup-mode", opt_backup_mode, SOURCE_CMD_STRICT },
@@ -440,6 +442,10 @@ main(int argc, char *argv[])
440442

441443
pgut_init();
442444

445+
/* Check terminal presense and initialize ANSI escape codes for Windows */
446+
if (show_color)
447+
init_console();
448+
443449
if (help_opt)
444450
help_command(command_name);
445451

src/pg_probackup.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ extern const char *PROGRAM_EMAIL;
111111
#define XRecOffIsNull(xlrp) \
112112
((xlrp) % XLOG_BLCKSZ == 0)
113113

114+
/* Text Coloring macro */
115+
#define TC_LEN 11
116+
#define TC_RED "\033[0;31m"
117+
#define TC_RED_BOLD "\033[1;31m"
118+
#define TC_BLUE "\033[0;34m"
119+
#define TC_BLUE_BOLD "\033[1;34m"
120+
#define TC_GREEN "\033[0;32m"
121+
#define TC_GREEN_BOLD "\033[1;32m"
122+
#define TC_YELLOW "\033[0;33m"
123+
#define TC_YELLOW_BOLD "\033[1;33m"
124+
#define TC_MAGENTA "\033[0;35m"
125+
#define TC_MAGENTA_BOLD "\033[1;35m"
126+
#define TC_CYAN "\033[0;36m"
127+
#define TC_CYAN_BOLD "\033[1;36m"
128+
#define TC_RESET "\033[0m"
129+
114130
typedef struct RedoParams
115131
{
116132
TimeLineID tli;
@@ -715,6 +731,7 @@ extern pid_t my_pid;
715731
extern __thread int my_thread_num;
716732
extern int num_threads;
717733
extern bool stream_wal;
734+
extern bool show_color;
718735
extern bool progress;
719736
extern bool is_archive_cmd; /* true for archive-{get,push} */
720737
#if PG_VERSION_NUM >= 100000
@@ -866,7 +883,8 @@ extern void write_backup_status(pgBackup *backup, BackupStatus status,
866883
extern void write_backup_data_bytes(pgBackup *backup);
867884
extern bool lock_backup(pgBackup *backup, bool strict);
868885

869-
extern const char *pgBackupGetBackupMode(pgBackup *backup);
886+
extern const char *pgBackupGetBackupMode(pgBackup *backup, bool show_color);
887+
extern void pgBackupGetBackupModeColor(pgBackup *backup, char *mode);
870888

871889
extern parray *catalog_get_instance_list(void);
872890
extern parray *catalog_get_backup_list(const char *instance_name, time_t requested_backup_id);
@@ -1076,6 +1094,7 @@ extern void copy_pgcontrol_file(const char *from_fullpath, fio_location from_loc
10761094

10771095
extern void time2iso(char *buf, size_t len, time_t time);
10781096
extern const char *status2str(BackupStatus status);
1097+
const char *status2str_color(BackupStatus status);
10791098
extern BackupStatus str2status(const char *status);
10801099
extern const char *base36enc(long unsigned int value);
10811100
extern char *base36enc_dup(long unsigned int value);

src/show.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ print_backup_json_object(PQExpBuffer buf, pgBackup *backup)
321321
json_add_value(buf, "parent-backup-id",
322322
base36enc(backup->parent_backup), json_level, true);
323323

324-
json_add_value(buf, "backup-mode", pgBackupGetBackupMode(backup),
324+
json_add_value(buf, "backup-mode", pgBackupGetBackupMode(backup, false),
325325
json_level, true);
326326

327327
json_add_value(buf, "wal", backup->stream ? "STREAM": "ARCHIVE",
@@ -554,8 +554,8 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
554554
cur++;
555555

556556
/* Mode */
557-
row->mode = pgBackupGetBackupMode(backup);
558-
widths[cur] = Max(widths[cur], strlen(row->mode));
557+
row->mode = pgBackupGetBackupMode(backup, show_color);
558+
widths[cur] = Max(widths[cur], strlen(row->mode) - (show_color ? TC_LEN : 0));
559559
cur++;
560560

561561
/* WAL mode*/
@@ -628,8 +628,9 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
628628
cur++;
629629

630630
/* Status */
631-
row->status = status2str(backup->status);
632-
widths[cur] = Max(widths[cur], strlen(row->status));
631+
row->status = show_color ? status2str_color(backup->status) : status2str(backup->status);
632+
widths[cur] = Max(widths[cur], strlen(row->status) - (show_color ? TC_LEN : 0));
633+
633634
}
634635

635636
for (i = 0; i < SHOW_FIELDS_COUNT; i++)
@@ -679,7 +680,7 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
679680
row->recovery_time);
680681
cur++;
681682

682-
appendPQExpBuffer(&show_buf, field_formats[cur], widths[cur],
683+
appendPQExpBuffer(&show_buf, field_formats[cur], widths[cur] + (show_color ? TC_LEN : 0),
683684
row->mode);
684685
cur++;
685686

@@ -715,7 +716,7 @@ show_instance_plain(const char *instance_name, parray *backup_list, bool show_na
715716
row->stop_lsn);
716717
cur++;
717718

718-
appendPQExpBuffer(&show_buf, field_formats[cur], widths[cur],
719+
appendPQExpBuffer(&show_buf, field_formats[cur], widths[cur] + (show_color ? TC_LEN : 0),
719720
row->status);
720721
cur++;
721722

src/util.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,29 @@ status2str(BackupStatus status)
516516
return statusName[status];
517517
}
518518

519+
const char *
520+
status2str_color(BackupStatus status)
521+
{
522+
char *status_str = pgut_malloc(20);
523+
524+
/* UNKNOWN */
525+
if (status == BACKUP_STATUS_INVALID)
526+
snprintf(status_str, 20, "%s%s%s", TC_YELLOW_BOLD, "UNKNOWN", TC_RESET);
527+
/* CORRUPT, ERROR and ORPHAN */
528+
else if (status == BACKUP_STATUS_CORRUPT || status == BACKUP_STATUS_ERROR ||
529+
status == BACKUP_STATUS_ORPHAN)
530+
snprintf(status_str, 20, "%s%s%s", TC_RED_BOLD, statusName[status], TC_RESET);
531+
/* MERGING, MERGED, DELETING and DELETED */
532+
else if (status == BACKUP_STATUS_MERGING || status == BACKUP_STATUS_MERGED ||
533+
status == BACKUP_STATUS_DELETING || status == BACKUP_STATUS_DELETED)
534+
snprintf(status_str, 20, "%s%s%s", TC_YELLOW_BOLD, statusName[status], TC_RESET);
535+
/* OK and DONE */
536+
else
537+
snprintf(status_str, 20, "%s%s%s", TC_GREEN_BOLD, statusName[status], TC_RESET);
538+
539+
return status_str;
540+
}
541+
519542
BackupStatus
520543
str2status(const char *status)
521544
{

0 commit comments

Comments
 (0)