Improve common/logging.c's support for multiple verbosity levels.
authorTom Lane <[email protected]>
Thu, 17 Sep 2020 16:52:18 +0000 (12:52 -0400)
committerTom Lane <[email protected]>
Thu, 17 Sep 2020 16:52:18 +0000 (12:52 -0400)
Instead of hard-wiring specific verbosity levels into the option
processing of client applications, invent pg_logging_increase_verbosity()
and encourage clients to implement --verbose by calling that.  Then,
the common convention that more -v's gets you more verbosity just works.

In particular, this allows resurrection of the debug-grade messages that
have long existed in pg_dump and its siblings.  They were unreachable
before this commit due to lack of a way to select PG_LOG_DEBUG logging
level.  (It appears that they may have been unreachable for some time
before common/logging.c was introduced, too, so I'm not specifically
blaming cc8d41511 for the oversight.  One reason for thinking that is
that it's now apparent that _allocAH()'s message needs a null-pointer
guard.  Testing might have failed to reveal that before 96bf88d52.)

Discussion: https://postgr.es/m/1173106.1600116625@sss.pgh.pa.us

12 files changed:
doc/src/sgml/ref/pg_dump.sgml
doc/src/sgml/ref/pg_dumpall.sgml
doc/src/sgml/ref/pg_restore.sgml
src/bin/pg_archivecleanup/pg_archivecleanup.c
src/bin/pg_dump/pg_backup_archiver.c
src/bin/pg_dump/pg_dump.c
src/bin/pg_dump/pg_dumpall.c
src/bin/pg_dump/pg_restore.c
src/bin/pg_rewind/pg_rewind.c
src/bin/pgbench/pgbench.c
src/common/logging.c
src/include/common/logging.h

index 0b2e2de87b6d88e1c936d7f0dd87b6ceeb4e1b90..0ec99165c5c006057f3927a148b33e21c992c21d 100644 (file)
@@ -594,6 +594,8 @@ PostgreSQL documentation
         <application>pg_dump</application> to output detailed object
         comments and start/stop times to the dump file, and progress
         messages to standard error.
+        Repeating the option causes additional debug-level messages
+        to appear on standard error.
        </para>
       </listitem>
      </varlistentry>
index 43abc530a0f617380a76434b0d6c39f878c92bc5..5b514e4567b20f234a04be6cbb44d18b343b7761 100644 (file)
@@ -202,7 +202,9 @@ PostgreSQL documentation
         Specifies verbose mode.  This will cause
         <application>pg_dumpall</application> to output start/stop
         times to the dump file, and progress messages to standard error.
-        It will also enable verbose output in <application>pg_dump</application>.
+        Repeating the option causes additional debug-level messages
+        to appear on standard error.
+        The option is also passed down to <application>pg_dump</application>.
        </para>
       </listitem>
      </varlistentry>
index 27eab2f02a52285d1e8f01459b8036bb878e9ab6..e0d9d2ad64f407e3124616ba4e88968ecad432b9 100644 (file)
@@ -483,7 +483,12 @@ PostgreSQL documentation
       <term><option>--verbose</option></term>
       <listitem>
        <para>
-        Specifies verbose mode.
+        Specifies verbose mode.  This will cause
+        <application>pg_restore</application> to output detailed object
+        comments and start/stop times to the output file, and progress
+        messages to standard error.
+        Repeating the option causes additional debug-level messages
+        to appear on standard error.
        </para>
       </listitem>
      </varlistentry>
index e454bae767de2bc54e936e7c63f6e62421094549..12338e3bb2c26d83b1c507b515dfde659f3f5dbe 100644 (file)
@@ -302,7 +302,7 @@ main(int argc, char **argv)
        switch (c)
        {
            case 'd':           /* Debug mode */
-               pg_logging_set_level(PG_LOG_DEBUG);
+               pg_logging_increase_verbosity();
                break;
            case 'n':           /* Dry-Run mode */
                dryrun = true;
index c05a1fd6af0d77e68b5f4ebb126a6a8466c2de48..178b61d6cbc3389fab0a3d5b8477abbd24e5437b 100644 (file)
@@ -2278,7 +2278,8 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
 {
    ArchiveHandle *AH;
 
-   pg_log_debug("allocating AH for %s, format %d", FileSpec, fmt);
+   pg_log_debug("allocating AH for %s, format %d",
+                FileSpec ? FileSpec : "(stdio)", fmt);
 
    AH = (ArchiveHandle *) pg_malloc0(sizeof(ArchiveHandle));
 
index 340638887217ef57a7583b949788701613c803da..320820165b7d78e734a25f9f204fa3f4452f07e0 100644 (file)
@@ -512,7 +512,7 @@ main(int argc, char **argv)
 
            case 'v':           /* verbose */
                g_verbose = true;
-               pg_logging_set_level(PG_LOG_INFO);
+               pg_logging_increase_verbosity();
                break;
 
            case 'w':
index 97d2b8dac1c6f57fc865458aa942f39baa16e400..219ca963c3b7f0f4834f9e3219dcf1fd087f965e 100644 (file)
@@ -283,7 +283,7 @@ main(int argc, char *argv[])
 
            case 'v':
                verbose = true;
-               pg_logging_set_level(PG_LOG_INFO);
+               pg_logging_increase_verbosity();
                appendPQExpBufferStr(pgdumpopts, " -v");
                break;
 
index 544ae3bc5cdf52e5bd57706de4e2b353e8389b37..eebf0d300ba9e1021bfbc30ac737fc922d4c3115 100644 (file)
@@ -245,7 +245,7 @@ main(int argc, char **argv)
 
            case 'v':           /* verbose */
                opts->verbose = 1;
-               pg_logging_set_level(PG_LOG_INFO);
+               pg_logging_increase_verbosity();
                break;
 
            case 'w':
index 23fc749e445150119158777bcb1b2b357dd87b41..0ec52cb0327903463c34093b398a9fb169c8e78b 100644 (file)
@@ -181,7 +181,7 @@ main(int argc, char **argv)
 
            case 3:
                debug = true;
-               pg_logging_set_level(PG_LOG_DEBUG);
+               pg_logging_increase_verbosity();
                break;
 
            case 'D':           /* -D or --target-pgdata */
index 332eabf6379e69a91c762002891734dbeff9c35a..663d7d292a2abad24db8df7f78ad7957d0f46447 100644 (file)
@@ -5522,7 +5522,7 @@ main(int argc, char **argv)
                pgport = pg_strdup(optarg);
                break;
            case 'd':
-               pg_logging_set_level(PG_LOG_DEBUG);
+               pg_logging_increase_verbosity();
                break;
            case 'c':
                benchmarking_option_set = true;
index 6a3a437a34bd3163a087fb36c0d407ae3f8cfad2..d9632fffc8ad1f581ccf6f757e3d120c1c7e1275 100644 (file)
@@ -157,12 +157,30 @@ pg_logging_config(int new_flags)
    log_flags = new_flags;
 }
 
+/*
+ * pg_logging_init sets the default log level to INFO.  Programs that prefer
+ * a different default should use this to set it, immediately afterward.
+ */
 void
 pg_logging_set_level(enum pg_log_level new_level)
 {
    __pg_log_level = new_level;
 }
 
+/*
+ * Command line switches such as --verbose should invoke this.
+ */
+void
+pg_logging_increase_verbosity(void)
+{
+   /*
+    * The enum values are chosen such that we have to decrease __pg_log_level
+    * in order to become more verbose.
+    */
+   if (__pg_log_level > PG_LOG_NOTSET + 1)
+       __pg_log_level--;
+}
+
 void
 pg_logging_set_pre_callback(void (*cb) (void))
 {
index 028149c7a1528e062b31d539b84f886e2813c1d4..3205b8fef9b70b93fdafa2d9e8939bdfe4588f63 100644 (file)
@@ -66,6 +66,7 @@ extern enum pg_log_level __pg_log_level;
 void       pg_logging_init(const char *argv0);
 void       pg_logging_config(int new_flags);
 void       pg_logging_set_level(enum pg_log_level new_level);
+void       pg_logging_increase_verbosity(void);
 void       pg_logging_set_pre_callback(void (*cb) (void));
 void       pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno));