LCOV - code coverage report
Current view: top level - src/common - logging.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 70.3 % 148 104
Test Date: 2026-07-21 09:15:43 Functions: 90.0 % 10 9
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 51.7 % 89 46

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  * Logging framework for frontend programs
       3                 :             :  *
       4                 :             :  * Copyright (c) 2018-2026, PostgreSQL Global Development Group
       5                 :             :  *
       6                 :             :  * src/common/logging.c
       7                 :             :  *
       8                 :             :  *-------------------------------------------------------------------------
       9                 :             :  */
      10                 :             : 
      11                 :             : #ifndef FRONTEND
      12                 :             : #error "This file is not expected to be compiled for backend code"
      13                 :             : #endif
      14                 :             : 
      15                 :             : #include "postgres_fe.h"
      16                 :             : 
      17                 :             : #include <unistd.h>
      18                 :             : 
      19                 :             : #include "common/logging.h"
      20                 :             : 
      21                 :             : enum pg_log_level __pg_log_level;
      22                 :             : 
      23                 :             : static const char *progname;
      24                 :             : static int  log_flags;
      25                 :             : 
      26                 :             : static void (*log_pre_callback) (void);
      27                 :             : static void (*log_locus_callback) (const char **, uint64 *);
      28                 :             : 
      29                 :             : static FILE *log_logfile;
      30                 :             : 
      31                 :             : static const char *sgr_error = NULL;
      32                 :             : static const char *sgr_warning = NULL;
      33                 :             : static const char *sgr_note = NULL;
      34                 :             : static const char *sgr_locus = NULL;
      35                 :             : 
      36                 :             : #define SGR_ERROR_DEFAULT "01;31"
      37                 :             : #define SGR_WARNING_DEFAULT "01;35"
      38                 :             : #define SGR_NOTE_DEFAULT "01;36"
      39                 :             : #define SGR_LOCUS_DEFAULT "01"
      40                 :             : 
      41                 :             : #define ANSI_ESCAPE_FMT "\x1b[%sm"
      42                 :             : #define ANSI_ESCAPE_RESET "\x1b[0m"
      43                 :             : 
      44                 :             : #ifdef WIN32
      45                 :             : 
      46                 :             : #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
      47                 :             : #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
      48                 :             : #endif
      49                 :             : 
      50                 :             : /*
      51                 :             :  * Attempt to enable VT100 sequence processing for colorization on Windows.
      52                 :             :  * If current environment is not VT100-compatible or if this mode could not
      53                 :             :  * be enabled, return false.
      54                 :             :  */
      55                 :             : static bool
      56                 :             : enable_vt_processing(void)
      57                 :             : {
      58                 :             :     /* Check stderr */
      59                 :             :     HANDLE      hOut = GetStdHandle(STD_ERROR_HANDLE);
      60                 :             :     DWORD       dwMode = 0;
      61                 :             : 
      62                 :             :     if (hOut == INVALID_HANDLE_VALUE)
      63                 :             :         return false;
      64                 :             : 
      65                 :             :     /*
      66                 :             :      * Look for the current console settings and check if VT100 is already
      67                 :             :      * enabled.
      68                 :             :      */
      69                 :             :     if (!GetConsoleMode(hOut, &dwMode))
      70                 :             :         return false;
      71                 :             :     if ((dwMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0)
      72                 :             :         return true;
      73                 :             : 
      74                 :             :     dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
      75                 :             :     if (!SetConsoleMode(hOut, dwMode))
      76                 :             :         return false;
      77                 :             :     return true;
      78                 :             : }
      79                 :             : #endif                          /* WIN32 */
      80                 :             : 
      81                 :             : /*
      82                 :             :  * This should be called before any output happens.
      83                 :             :  */
      84                 :             : void
      85                 :       15866 : pg_logging_init(const char *argv0)
      86                 :             : {
      87                 :       15866 :     const char *pg_color_env = getenv("PG_COLOR");
      88                 :       15866 :     bool        log_color = false;
      89                 :       15866 :     bool        color_terminal = isatty(fileno(stderr));
      90                 :             : 
      91                 :             : #ifdef WIN32
      92                 :             : 
      93                 :             :     /*
      94                 :             :      * On Windows, check if environment is VT100-compatible if using a
      95                 :             :      * terminal.
      96                 :             :      */
      97                 :             :     if (color_terminal)
      98                 :             :         color_terminal = enable_vt_processing();
      99                 :             : #endif
     100                 :             : 
     101                 :             :     /* usually the default, but not on Windows */
     102                 :       15866 :     setvbuf(stderr, NULL, _IONBF, 0);
     103                 :             : 
     104                 :       15866 :     progname = get_progname(argv0);
     105                 :       15866 :     __pg_log_level = PG_LOG_INFO;
     106                 :             : 
     107         [ -  + ]:       15866 :     if (pg_color_env)
     108                 :             :     {
     109         [ #  # ]:           0 :         if (strcmp(pg_color_env, "always") == 0 ||
     110   [ #  #  #  # ]:           0 :             (strcmp(pg_color_env, "auto") == 0 && color_terminal))
     111                 :           0 :             log_color = true;
     112                 :             :     }
     113                 :             : 
     114         [ -  + ]:       15866 :     if (log_color)
     115                 :             :     {
     116                 :           0 :         const char *pg_colors_env = getenv("PG_COLORS");
     117                 :             : 
     118         [ #  # ]:           0 :         if (pg_colors_env)
     119                 :             :         {
     120                 :           0 :             char       *colors = strdup(pg_colors_env);
     121                 :             : 
     122         [ #  # ]:           0 :             if (colors)
     123                 :             :             {
     124                 :             :                 char       *token;
     125                 :           0 :                 char       *cp = colors;
     126                 :             : 
     127         [ #  # ]:           0 :                 while ((token = strsep(&cp, ":")))
     128                 :             :                 {
     129                 :           0 :                     char       *e = strchr(token, '=');
     130                 :             : 
     131         [ #  # ]:           0 :                     if (e)
     132                 :             :                     {
     133                 :             :                         char       *name;
     134                 :             :                         char       *value;
     135                 :             : 
     136                 :           0 :                         *e = '\0';
     137                 :           0 :                         name = token;
     138                 :           0 :                         value = e + 1;
     139                 :             : 
     140         [ #  # ]:           0 :                         if (strcmp(name, "error") == 0)
     141                 :           0 :                             sgr_error = strdup(value);
     142         [ #  # ]:           0 :                         if (strcmp(name, "warning") == 0)
     143                 :           0 :                             sgr_warning = strdup(value);
     144         [ #  # ]:           0 :                         if (strcmp(name, "note") == 0)
     145                 :           0 :                             sgr_note = strdup(value);
     146         [ #  # ]:           0 :                         if (strcmp(name, "locus") == 0)
     147                 :           0 :                             sgr_locus = strdup(value);
     148                 :             :                     }
     149                 :             :                 }
     150                 :             : 
     151                 :           0 :                 free(colors);
     152                 :             :             }
     153                 :             :         }
     154                 :             :         else
     155                 :             :         {
     156                 :           0 :             sgr_error = SGR_ERROR_DEFAULT;
     157                 :           0 :             sgr_warning = SGR_WARNING_DEFAULT;
     158                 :           0 :             sgr_note = SGR_NOTE_DEFAULT;
     159                 :           0 :             sgr_locus = SGR_LOCUS_DEFAULT;
     160                 :             :         }
     161                 :             :     }
     162                 :       15866 : }
     163                 :             : 
     164                 :             : /*
     165                 :             :  * Change the logging flags.
     166                 :             :  */
     167                 :             : void
     168                 :       20797 : pg_logging_config(int new_flags)
     169                 :             : {
     170                 :       20797 :     log_flags = new_flags;
     171                 :       20797 : }
     172                 :             : 
     173                 :             : /*
     174                 :             :  * pg_logging_init sets the default log level to INFO.  Programs that prefer
     175                 :             :  * a different default should use this to set it, immediately afterward.
     176                 :             :  */
     177                 :             : void
     178                 :         491 : pg_logging_set_level(enum pg_log_level new_level)
     179                 :             : {
     180                 :         491 :     __pg_log_level = new_level;
     181                 :         491 : }
     182                 :             : 
     183                 :             : /*
     184                 :             :  * Command line switches such as --verbose should invoke this.
     185                 :             :  */
     186                 :             : void
     187                 :          96 : pg_logging_increase_verbosity(void)
     188                 :             : {
     189                 :             :     /*
     190                 :             :      * The enum values are chosen such that we have to decrease __pg_log_level
     191                 :             :      * in order to become more verbose.
     192                 :             :      */
     193         [ +  - ]:          96 :     if (__pg_log_level > PG_LOG_NOTSET + 1)
     194                 :          96 :         __pg_log_level--;
     195                 :          96 : }
     196                 :             : 
     197                 :             : void
     198                 :       10683 : pg_logging_set_pre_callback(void (*cb) (void))
     199                 :             : {
     200                 :       10683 :     log_pre_callback = cb;
     201                 :       10683 : }
     202                 :             : 
     203                 :             : void
     204                 :       10683 : pg_logging_set_locus_callback(void (*cb) (const char **filename, uint64 *lineno))
     205                 :             : {
     206                 :       10683 :     log_locus_callback = cb;
     207                 :       10683 : }
     208                 :             : 
     209                 :             : void
     210                 :           1 : pg_logging_set_logfile(FILE *logfile)
     211                 :             : {
     212                 :           1 :     log_logfile = logfile;
     213                 :           1 : }
     214                 :             : 
     215                 :             : void
     216                 :           0 : pg_logging_unset_logfile(void)
     217                 :             : {
     218                 :           0 :     log_logfile = NULL;
     219                 :           0 : }
     220                 :             : 
     221                 :             : void
     222                 :      296391 : pg_log_generic(enum pg_log_level level, enum pg_log_part part,
     223                 :             :                const char *pg_restrict fmt, ...)
     224                 :             : {
     225                 :             :     va_list     ap;
     226                 :             : 
     227                 :      296391 :     va_start(ap, fmt);
     228                 :      296391 :     pg_log_generic_v(level, part, fmt, ap);
     229                 :      296391 :     va_end(ap);
     230                 :      296391 : }
     231                 :             : 
     232                 :             : void
     233                 :      297511 : pg_log_generic_v(enum pg_log_level level, enum pg_log_part part,
     234                 :             :                  const char *pg_restrict fmt, va_list ap)
     235                 :             : {
     236                 :      297511 :     int         save_errno = errno;
     237                 :      297511 :     const char *filename = NULL;
     238                 :      297511 :     uint64      lineno = 0;
     239                 :             :     va_list     ap2;
     240                 :             :     size_t      required_len;
     241                 :             :     char       *buf;
     242                 :             : 
     243                 :             :     Assert(progname);
     244                 :             :     Assert(level);
     245                 :             :     Assert(fmt);
     246                 :             :     Assert(fmt[strlen(fmt) - 1] != '\n');
     247                 :             : 
     248                 :             :     /* Do nothing if log level is too low. */
     249         [ +  + ]:      297511 :     if (level < __pg_log_level)
     250                 :       49902 :         return;
     251                 :             : 
     252                 :             :     /*
     253                 :             :      * Flush stdout before output to stderr, to ensure sync even when stdout
     254                 :             :      * is buffered.
     255                 :             :      */
     256                 :      247609 :     fflush(stdout);
     257                 :             : 
     258         [ +  + ]:      247609 :     if (log_pre_callback)
     259                 :      217619 :         log_pre_callback();
     260                 :             : 
     261         [ +  + ]:      247609 :     if (log_locus_callback)
     262                 :      217619 :         log_locus_callback(&filename, &lineno);
     263                 :             : 
     264                 :      247609 :     fmt = _(fmt);
     265                 :             : 
     266   [ +  +  -  + ]:      247609 :     if (!(log_flags & PG_LOG_FLAG_TERSE) || filename)
     267                 :             :     {
     268         [ -  + ]:      200111 :         if (sgr_locus)
     269                 :           0 :             fprintf(stderr, ANSI_ESCAPE_FMT, sgr_locus);
     270         [ +  - ]:      200111 :         if (!(log_flags & PG_LOG_FLAG_TERSE))
     271                 :      200111 :             fprintf(stderr, "%s:", progname);
     272         [ +  + ]:      200111 :         if (filename)
     273                 :             :         {
     274                 :      169921 :             fprintf(stderr, "%s:", filename);
     275         [ +  - ]:      169921 :             if (lineno > 0)
     276                 :      169921 :                 fprintf(stderr, UINT64_FORMAT ":", lineno);
     277                 :             :         }
     278                 :      200111 :         fprintf(stderr, " ");
     279         [ -  + ]:      200111 :         if (sgr_locus)
     280                 :           0 :             fprintf(stderr, ANSI_ESCAPE_RESET);
     281                 :             :     }
     282                 :             : 
     283         [ +  + ]:      247609 :     if (!(log_flags & PG_LOG_FLAG_TERSE))
     284                 :             :     {
     285   [ +  +  +  - ]:      200111 :         switch (part)
     286                 :             :         {
     287                 :      199939 :             case PG_LOG_PRIMARY:
     288      [ +  +  + ]:      199939 :                 switch (level)
     289                 :             :                 {
     290                 :        1953 :                     case PG_LOG_ERROR:
     291         [ -  + ]:        1953 :                         if (sgr_error)
     292                 :           0 :                             fprintf(stderr, ANSI_ESCAPE_FMT, sgr_error);
     293                 :        1953 :                         fprintf(stderr, _("error: "));
     294         [ -  + ]:        1953 :                         if (log_logfile)
     295                 :           0 :                             fprintf(log_logfile, _("error: "));
     296         [ -  + ]:        1953 :                         if (sgr_error)
     297                 :           0 :                             fprintf(stderr, ANSI_ESCAPE_RESET);
     298                 :        1953 :                         break;
     299                 :         140 :                     case PG_LOG_WARNING:
     300         [ -  + ]:         140 :                         if (sgr_warning)
     301                 :           0 :                             fprintf(stderr, ANSI_ESCAPE_FMT, sgr_warning);
     302                 :         140 :                         fprintf(stderr, _("warning: "));
     303         [ -  + ]:         140 :                         if (log_logfile)
     304                 :           0 :                             fprintf(log_logfile, _("warning: "));
     305         [ -  + ]:         140 :                         if (sgr_warning)
     306                 :           0 :                             fprintf(stderr, ANSI_ESCAPE_RESET);
     307                 :         140 :                         break;
     308                 :      197846 :                     default:
     309                 :      197846 :                         break;
     310                 :             :                 }
     311                 :      199939 :                 break;
     312                 :          30 :             case PG_LOG_DETAIL:
     313         [ -  + ]:          30 :                 if (sgr_note)
     314                 :           0 :                     fprintf(stderr, ANSI_ESCAPE_FMT, sgr_note);
     315                 :          30 :                 fprintf(stderr, _("detail: "));
     316         [ +  + ]:          30 :                 if (log_logfile)
     317                 :           1 :                     fprintf(log_logfile, _("detail: "));
     318         [ -  + ]:          30 :                 if (sgr_note)
     319                 :           0 :                     fprintf(stderr, ANSI_ESCAPE_RESET);
     320                 :          30 :                 break;
     321                 :         142 :             case PG_LOG_HINT:
     322         [ -  + ]:         142 :                 if (sgr_note)
     323                 :           0 :                     fprintf(stderr, ANSI_ESCAPE_FMT, sgr_note);
     324                 :         142 :                 fprintf(stderr, _("hint: "));
     325         [ +  + ]:         142 :                 if (log_logfile)
     326                 :           1 :                     fprintf(log_logfile, _("hint: "));
     327         [ -  + ]:         142 :                 if (sgr_note)
     328                 :           0 :                     fprintf(stderr, ANSI_ESCAPE_RESET);
     329                 :         142 :                 break;
     330                 :             :         }
     331                 :             :     }
     332                 :             : 
     333                 :      247609 :     errno = save_errno;
     334                 :             : 
     335                 :      247609 :     va_copy(ap2, ap);
     336                 :      247609 :     required_len = vsnprintf(NULL, 0, fmt, ap2) + 1;
     337                 :      247609 :     va_end(ap2);
     338                 :             : 
     339                 :      247609 :     buf = pg_malloc_extended(required_len, MCXT_ALLOC_NO_OOM);
     340                 :             : 
     341                 :      247609 :     errno = save_errno;         /* malloc might change errno */
     342                 :             : 
     343         [ -  + ]:      247609 :     if (!buf)
     344                 :             :     {
     345                 :             :         /* memory trouble, just print what we can and get out of here */
     346                 :           0 :         vfprintf(stderr, fmt, ap);
     347                 :           0 :         return;
     348                 :             :     }
     349                 :             : 
     350                 :      247609 :     vsnprintf(buf, required_len, fmt, ap);
     351                 :             : 
     352                 :             :     /* strip one newline, for PQerrorMessage() */
     353   [ +  -  +  + ]:      247609 :     if (required_len >= 2 && buf[required_len - 2] == '\n')
     354                 :      216809 :         buf[required_len - 2] = '\0';
     355                 :             : 
     356                 :      247609 :     fprintf(stderr, "%s\n", buf);
     357         [ +  + ]:      247609 :     if (log_logfile)
     358                 :             :     {
     359                 :          41 :         fprintf(log_logfile, "%s\n", buf);
     360                 :          41 :         fflush(log_logfile);
     361                 :             :     }
     362                 :             : 
     363                 :      247609 :     pg_free(buf);
     364                 :             : }
        

Generated by: LCOV version 2.0-1