Skip to content

Commit 3836509

Browse files
committed
[Issue #224] enable the stdout and stderr coloring by default
1 parent 26578ed commit 3836509

File tree

6 files changed

+32
-25
lines changed

6 files changed

+32
-25
lines changed

doc/pgprobackup.xml

+5-5
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] [--color]
3569+
[--help] [--instance <replaceable>instance_name</replaceable> [-i <replaceable>backup_id</replaceable> | --archive]] [--format=plain|json] [--no-color]
35703570
</programlisting>
35713571
<para>
35723572
Shows the contents of the backup catalog. If
@@ -3581,8 +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.
3584+
If <literal>--no-color</literal> flag is used,
3585+
then the output is not colored.
35863586
</para>
35873587
<para>
35883588
For details on usage, see the sections
@@ -4607,10 +4607,10 @@ pg_probackup archive-get -B <replaceable>backup_dir</replaceable> --instance <re
46074607
<variablelist>
46084608

46094609
<varlistentry>
4610-
<term><option>--color</option></term>
4610+
<term><option>--no-color</option></term>
46114611
<listitem>
46124612
<para>
4613-
Color the console log messages of <literal>warning</literal> and <literal>error</literal> levels.
4613+
Disable the coloring for console log messages of <literal>warning</literal> and <literal>error</literal> levels.
46144614
</para>
46154615
</listitem>
46164616
</varlistentry>

src/help.c

+13-13
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ help_pg_probackup(void)
127127
printf(_(" [--error-log-filename=error-log-filename]\n"));
128128
printf(_(" [--log-directory=log-directory]\n"));
129129
printf(_(" [--log-rotation-size=log-rotation-size]\n"));
130-
printf(_(" [--log-rotation-age=log-rotation-age]\n"));
130+
printf(_(" [--log-rotation-age=log-rotation-age] [--no-color]\n"));
131131
printf(_(" [--delete-expired] [--delete-wal] [--merge-expired]\n"));
132132
printf(_(" [--retention-redundancy=retention-redundancy]\n"));
133133
printf(_(" [--retention-window=retention-window]\n"));
@@ -188,7 +188,7 @@ help_pg_probackup(void)
188188
printf(_("\n %s show -B backup-path\n"), PROGRAM_NAME);
189189
printf(_(" [--instance=instance_name [-i backup-id]]\n"));
190190
printf(_(" [--format=format] [--archive]\n"));
191-
printf(_(" [--help]\n"));
191+
printf(_(" [--no-color] [--help]\n"));
192192

193193
printf(_("\n %s delete -B backup-path --instance=instance_name\n"), PROGRAM_NAME);
194194
printf(_(" [-j num-threads] [--progress]\n"));
@@ -273,7 +273,7 @@ help_backup(void)
273273
printf(_(" [--error-log-filename=error-log-filename]\n"));
274274
printf(_(" [--log-directory=log-directory]\n"));
275275
printf(_(" [--log-rotation-size=log-rotation-size]\n"));
276-
printf(_(" [--log-rotation-age=log-rotation-age]\n"));
276+
printf(_(" [--log-rotation-age=log-rotation-age] [--no-color]\n"));
277277
printf(_(" [--delete-expired] [--delete-wal] [--merge-expired]\n"));
278278
printf(_(" [--retention-redundancy=retention-redundancy]\n"));
279279
printf(_(" [--retention-window=retention-window]\n"));
@@ -310,7 +310,6 @@ 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"));
314313
printf(_(" --log-level-console=log-level-console\n"));
315314
printf(_(" level for console logging (default: info)\n"));
316315
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -330,6 +329,7 @@ help_backup(void)
330329
printf(_(" --log-rotation-age=log-rotation-age\n"));
331330
printf(_(" rotate logfile if its age exceeds this value; 0 disables; (default: 0)\n"));
332331
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n"));
332+
printf(_(" --no-color disable the coloring of error and warning console messages\n"));
333333

334334
printf(_("\n Retention options:\n"));
335335
printf(_(" --delete-expired delete backups expired according to current\n"));
@@ -471,7 +471,6 @@ help_restore(void)
471471
printf(_(" -S, --primary-slot-name=slotname replication slot to be used for WAL streaming from the primary server\n"));
472472

473473
printf(_("\n Logging options:\n"));
474-
printf(_(" --color color the error and warning console messages\n"));
475474
printf(_(" --log-level-console=log-level-console\n"));
476475
printf(_(" level for console logging (default: info)\n"));
477476
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -491,6 +490,7 @@ help_restore(void)
491490
printf(_(" --log-rotation-age=log-rotation-age\n"));
492491
printf(_(" rotate logfile if its age exceeds this value; 0 disables; (default: 0)\n"));
493492
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n"));
493+
printf(_(" --no-color disable the coloring of error and warning console messages\n"));
494494

495495
printf(_("\n Remote options:\n"));
496496
printf(_(" --remote-proto=protocol remote protocol to use\n"));
@@ -538,7 +538,6 @@ help_validate(void)
538538
printf(_(" --skip-block-validation set to validate only file-level checksum\n"));
539539

540540
printf(_("\n Logging options:\n"));
541-
printf(_(" --color color the error and warning console messages\n"));
542541
printf(_(" --log-level-console=log-level-console\n"));
543542
printf(_(" level for console logging (default: info)\n"));
544543
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -557,7 +556,8 @@ help_validate(void)
557556
printf(_(" available units: 'kB', 'MB', 'GB', 'TB' (default: kB)\n"));
558557
printf(_(" --log-rotation-age=log-rotation-age\n"));
559558
printf(_(" rotate logfile if its age exceeds this value; 0 disables; (default: 0)\n"));
560-
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n\n"));
559+
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n"));
560+
printf(_(" --no-color disable the coloring of error and warning console messages\n\n"));
561561
}
562562

563563
static void
@@ -583,7 +583,6 @@ help_checkdb(void)
583583
printf(_(" can be used only with '--amcheck' option\n"));
584584

585585
printf(_("\n Logging options:\n"));
586-
printf(_(" --color color the error and warning console messages\n"));
587586
printf(_(" --log-level-console=log-level-console\n"));
588587
printf(_(" level for console logging (default: info)\n"));
589588
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -603,6 +602,7 @@ help_checkdb(void)
603602
printf(_(" --log-rotation-age=log-rotation-age\n"));
604603
printf(_(" rotate logfile if its age exceeds this value; 0 disables; (default: 0)\n"));
605604
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n"));
605+
printf(_(" --no-color disable the coloring of error and warning console messages\n"));
606606

607607
printf(_("\n Connection options:\n"));
608608
printf(_(" -U, --pguser=USERNAME user name to connect as (default: current local user)\n"));
@@ -625,7 +625,7 @@ help_show(void)
625625
printf(_(" -i, --backup-id=backup-id show info about specific backups\n"));
626626
printf(_(" --archive show WAL archive information\n"));
627627
printf(_(" --format=format show format=PLAIN|JSON\n"));
628-
printf(_(" --color color the info for plain format\n\n"));
628+
printf(_(" --no-color disable the coloring for plain format\n\n"));
629629
}
630630

631631
static void
@@ -660,7 +660,6 @@ help_delete(void)
660660
printf(_(" --status=backup_status delete all backups with specified status\n"));
661661

662662
printf(_("\n Logging options:\n"));
663-
printf(_(" --color color the error and warning console messages\n"));
664663
printf(_(" --log-level-console=log-level-console\n"));
665664
printf(_(" level for console logging (default: info)\n"));
666665
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -679,7 +678,8 @@ help_delete(void)
679678
printf(_(" available units: 'kB', 'MB', 'GB', 'TB' (default: kB)\n"));
680679
printf(_(" --log-rotation-age=log-rotation-age\n"));
681680
printf(_(" rotate logfile if its age exceeds this value; 0 disables; (default: 0)\n"));
682-
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n\n"));
681+
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n"));
682+
printf(_(" --no-color disable the coloring of error and warning console messages\n\n"));
683683
}
684684

685685
static void
@@ -703,7 +703,6 @@ help_merge(void)
703703
printf(_(" --progress show progress\n"));
704704

705705
printf(_("\n Logging options:\n"));
706-
printf(_(" --color color the error and warning console messages\n"));
707706
printf(_(" --log-level-console=log-level-console\n"));
708707
printf(_(" level for console logging (default: info)\n"));
709708
printf(_(" available options: 'off', 'error', 'warning', 'info', 'log', 'verbose'\n"));
@@ -722,7 +721,8 @@ help_merge(void)
722721
printf(_(" available units: 'kB', 'MB', 'GB', 'TB' (default: kB)\n"));
723722
printf(_(" --log-rotation-age=log-rotation-age\n"));
724723
printf(_(" rotate logfile if its age exceeds this value; 0 disables; (default: 0)\n"));
725-
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n\n"));
724+
printf(_(" available units: 'ms', 's', 'min', 'h', 'd' (default: min)\n"));
725+
printf(_(" --no-color disable the coloring of error and warning console messages\n\n"));
726726
}
727727

728728
static void

src/pg_probackup.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ 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;
70+
bool no_color = false;
71+
bool show_color = true;
7172
bool is_archive_cmd = false;
7273
pid_t my_pid = 0;
7374
__thread int my_thread_num = 1;
@@ -179,7 +180,7 @@ static ConfigOption cmd_options[] =
179180
{ 'b', 132, "progress", &progress, SOURCE_CMD_STRICT },
180181
{ 's', 'i', "backup-id", &backup_id_string, SOURCE_CMD_STRICT },
181182
{ 'b', 133, "no-sync", &no_sync, SOURCE_CMD_STRICT },
182-
{ 'b', 134, "color", &show_color, SOURCE_CMD_STRICT },
183+
{ 'b', 134, "no-color", &no_color, SOURCE_CMD_STRICT },
183184
/* backup options */
184185
{ 'b', 180, "backup-pg-log", &backup_logs, SOURCE_CMD_STRICT },
185186
{ 'f', 'b', "backup-mode", opt_backup_mode, SOURCE_CMD_STRICT },
@@ -287,6 +288,9 @@ main(int argc, char *argv[])
287288

288289
PROGRAM_NAME_FULL = argv[0];
289290

291+
/* Check terminal presense and initialize ANSI escape codes for Windows */
292+
init_console();
293+
290294
/* Initialize current backup */
291295
pgBackupInit(&current);
292296

@@ -442,9 +446,8 @@ main(int argc, char *argv[])
442446

443447
pgut_init();
444448

445-
/* Check terminal presense and initialize ANSI escape codes for Windows */
446-
if (show_color)
447-
init_console();
449+
if (no_color)
450+
show_color = false;
448451

449452
if (help_opt)
450453
help_command(command_name);

src/utils/logger.c

-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ init_console(void)
132132
!isatty(fileno(stdout)))
133133
{
134134
show_color = false;
135-
elog(WARNING, "No terminal detected, ignoring '--color' flag");
136135
return;
137136
}
138137

tests/archive.py

+5
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,11 @@ def test_archive_push_file_exists(self):
432432
'pg_probackup archive-push completed successfully',
433433
log_content)
434434

435+
# btw check that console coloring codes are not slipped into log file
436+
self.assertNotIn('[0m', log_content)
437+
438+
print(log_content)
439+
435440
# Clean after yourself
436441
self.del_test_dir(module_name, fname)
437442

tests/show.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def test_color_with_no_terminal(self):
559559
# FULL
560560
try:
561561
self.backup_node(
562-
backup_dir, 'node', node, options=['--color', '--archive-timeout=1s'])
562+
backup_dir, 'node', node, options=['--archive-timeout=1s'])
563563
# we should die here because exception is what we expect to happen
564564
self.assertEqual(
565565
1, 0,

0 commit comments

Comments
 (0)