6 * Copyright (c) 2010-2021, PostgreSQL Global Development Group
7 * src/bin/pg_upgrade/util.c
10 #include "postgres_fe.h"
14 #include "common/username.h"
15 #include "pg_upgrade.h"
19 static void pg_log_v(eLogType type
, const char *fmt
, va_list ap
) pg_attribute_printf(2, 0);
25 * Displays the result of an operation (ok, failed, error message,...)
28 report_status(eLogType type
, const char *fmt
,...)
31 char message
[MAX_STRING
];
34 vsnprintf(message
, sizeof(message
), fmt
, args
);
37 pg_log(type
, "%s\n", message
);
41 /* force blank output for progress display */
43 end_progress_output(void)
46 * In case nothing printed; pass a space so gcc doesn't complain about
47 * empty format string.
56 * Displays a message that describes an operation we are about to begin.
57 * We pad the message out to MESSAGE_WIDTH characters so that all of the "ok" and
58 * "failed" indicators line up nicely.
60 * A typical sequence would look like this:
61 * prep_status("about to flarb the next %d files", fileCount );
63 * if(( message = flarbFiles(fileCount)) == NULL)
64 * report_status(PG_REPORT, "ok" );
66 * pg_log(PG_FATAL, "failed - %s\n", message );
69 prep_status(const char *fmt
,...)
72 char message
[MAX_STRING
];
75 vsnprintf(message
, sizeof(message
), fmt
, args
);
78 if (strlen(message
) > 0 && message
[strlen(message
) - 1] == '\n')
79 pg_log(PG_REPORT
, "%s", message
);
81 /* trim strings that don't end in a newline */
82 pg_log(PG_REPORT
, "%-*s", MESSAGE_WIDTH
, message
);
87 pg_log_v(eLogType type
, const char *fmt
, va_list ap
)
89 char message
[QUERY_ALLOC
];
91 vsnprintf(message
, sizeof(message
), _(fmt
), ap
);
93 /* PG_VERBOSE and PG_STATUS are only output in verbose mode */
94 /* fopen() on log_opts.internal might have failed, so check it */
95 if (((type
!= PG_VERBOSE
&& type
!= PG_STATUS
) || log_opts
.verbose
) &&
96 log_opts
.internal
!= NULL
)
98 if (type
== PG_STATUS
)
99 /* status messages need two leading spaces and a newline */
100 fprintf(log_opts
.internal
, " %s\n", message
);
102 fprintf(log_opts
.internal
, "%s", message
);
103 fflush(log_opts
.internal
);
109 if (log_opts
.verbose
)
110 printf("%s", message
);
114 /* for output to a display, do leading truncation and append \r */
115 if (isatty(fileno(stdout
)))
116 /* -2 because we use a 2-space indent */
117 printf(" %s%-*.*s\r",
118 /* prefix with "..." if we do leading truncation */
119 strlen(message
) <= MESSAGE_WIDTH
- 2 ? "" : "...",
120 MESSAGE_WIDTH
- 2, MESSAGE_WIDTH
- 2,
121 /* optional leading truncation */
122 strlen(message
) <= MESSAGE_WIDTH
- 2 ? message
:
123 message
+ strlen(message
) - MESSAGE_WIDTH
+ 3 + 2);
125 printf(" %s\n", message
);
130 printf("%s", message
);
134 printf("\n%s", message
);
135 printf(_("Failure, exiting\n"));
147 pg_log(eLogType type
, const char *fmt
,...)
152 pg_log_v(type
, fmt
, args
);
158 pg_fatal(const char *fmt
,...)
163 pg_log_v(PG_FATAL
, fmt
, args
);
165 printf(_("Failure, exiting\n"));
174 report_status(PG_REPORT
, "ok");
181 * Properly double-quote a SQL identifier.
183 * The result should be pg_free'd, but most callers don't bother because
184 * memory leakage is not a big deal in this program.
187 quote_identifier(const char *s
)
189 char *result
= pg_malloc(strlen(s
) * 2 + 3);
211 get_user_info(char **user_name_p
)
214 const char *user_name
;
223 user_name
= get_user_name(&errstr
);
225 pg_fatal("%s\n", errstr
);
228 *user_name_p
= pg_strdup(user_name
);
237 * convert string to oid
240 str2uint(const char *str
)
242 return strtoul(str
, NULL
, 10);