PostgreSQL Source Code  git master
util.c File Reference
#include "postgres_fe.h"
#include <signal.h>
#include "common/username.h"
#include "pg_upgrade.h"
Include dependency graph for util.c:

Go to the source code of this file.

Functions

static void pg_log_v (eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2
 
static void void report_status (eLogType type, const char *fmt,...)
 
void end_progress_output (void)
 
void prep_status (const char *fmt,...)
 
void prep_status_progress (const char *fmt,...)
 
void pg_log (eLogType type, const char *fmt,...)
 
void pg_fatal (const char *fmt,...)
 
void check_ok (void)
 
char * quote_identifier (const char *s)
 
int get_user_info (char **user_name_p)
 
unsigned int str2uint (const char *str)
 

Variables

LogOpts log_opts
 

Function Documentation

◆ check_ok()

void check_ok ( void  )

Definition at line 211 of file util.c.

212 {
213  /* all seems well */
214  report_status(PG_REPORT, "ok");
215  fflush(stdout);
216 }
static void const char fflush(stdout)
@ PG_REPORT
Definition: pg_upgrade.h:237
static void void report_status(eLogType type, const char *fmt,...)
Definition: util.c:28

References fflush(), PG_REPORT, report_status(), and generate_unaccent_rules::stdout.

◆ end_progress_output()

void end_progress_output ( void  )

Definition at line 42 of file util.c.

43 {
44  /*
45  * For output to a tty, erase prior contents of progress line. When either
46  * tty or verbose, indent so that report_status() output will align
47  * nicely.
48  */
49  if (log_opts.isatty)
50  pg_log(PG_REPORT, "\r%-*s", MESSAGE_WIDTH, "");
51  else if (log_opts.verbose)
52  pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, "");
53 }
#define MESSAGE_WIDTH
Definition: pg_upgrade.h:24
bool isatty
Definition: pg_upgrade.h:282
bool verbose
Definition: pg_upgrade.h:276
void pg_log(eLogType type, const char *fmt,...)
Definition: util.c:187
LogOpts log_opts
Definition: util.c:17

References LogOpts::isatty, log_opts, MESSAGE_WIDTH, pg_log(), PG_REPORT, and LogOpts::verbose.

Referenced by create_new_objects(), generate_old_dump(), and transfer_all_new_tablespaces().

◆ get_user_info()

int get_user_info ( char **  user_name_p)

Definition at line 251 of file util.c.

252 {
253  int user_id;
254  const char *user_name;
255  char *errstr;
256 
257 #ifndef WIN32
258  user_id = geteuid();
259 #else
260  user_id = 1;
261 #endif
262 
263  user_name = get_user_name(&errstr);
264  if (!user_name)
265  pg_fatal("%s\n", errstr);
266 
267  /* make a copy */
268  *user_name_p = pg_strdup(user_name);
269 
270  return user_id;
271 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
const char * get_user_name(char **errstr)
Definition: username.c:31
void pg_fatal(const char *fmt,...)
Definition: util.c:198

References get_user_name(), pg_fatal(), and pg_strdup().

Referenced by parseCommandLine().

◆ pg_fatal()

void pg_fatal ( const char *  fmt,
  ... 
)

Definition at line 198 of file util.c.

199 {
200  va_list args;
201 
202  va_start(args, fmt);
204  va_end(args);
205  printf(_("Failure, exiting\n"));
206  exit(1);
207 }
#define _(x)
Definition: elog.c:89
va_end(args)
exit(1)
static void const char * fmt
va_start(args, fmt)
@ PG_FATAL
Definition: pg_upgrade.h:239
#define printf(...)
Definition: port.h:231
static void pg_log_v(eLogType type, const char *fmt, va_list ap) pg_attribute_printf(2
Definition: util.c:119

References _, generate_unaccent_rules::args, exit(), fmt, PG_FATAL, pg_log_v(), printf, va_end(), and va_start().

Referenced by get_user_info().

◆ pg_log()

void pg_log ( eLogType  type,
const char *  fmt,
  ... 
)

◆ pg_log_v()

static void pg_log_v ( eLogType  type,
const char *  fmt,
va_list  ap 
)
static

Definition at line 119 of file util.c.

120 {
121  char message[QUERY_ALLOC];
122 
123  vsnprintf(message, sizeof(message), _(fmt), ap);
124 
125  /* PG_VERBOSE and PG_STATUS are only output in verbose mode */
126  /* fopen() on log_opts.internal might have failed, so check it */
127  if (((type != PG_VERBOSE && type != PG_STATUS) || log_opts.verbose) &&
128  log_opts.internal != NULL)
129  {
130  if (type == PG_STATUS)
131  /* status messages need two leading spaces and a newline */
132  fprintf(log_opts.internal, " %s\n", message);
133  else
134  fprintf(log_opts.internal, "%s", message);
136  }
137 
138  switch (type)
139  {
140  case PG_VERBOSE:
141  if (log_opts.verbose)
142  printf("%s", message);
143  break;
144 
145  case PG_STATUS:
146 
147  /*
148  * For output to a display, do leading truncation. Append \r so
149  * that the next message is output at the start of the line.
150  *
151  * If going to non-interactive output, only display progress if
152  * verbose is enabled. Otherwise the output gets unreasonably
153  * large by default.
154  */
155  if (log_opts.isatty)
156  /* -2 because we use a 2-space indent */
157  printf(" %s%-*.*s\r",
158  /* prefix with "..." if we do leading truncation */
159  strlen(message) <= MESSAGE_WIDTH - 2 ? "" : "...",
160  MESSAGE_WIDTH - 2, MESSAGE_WIDTH - 2,
161  /* optional leading truncation */
162  strlen(message) <= MESSAGE_WIDTH - 2 ? message :
163  message + strlen(message) - MESSAGE_WIDTH + 3 + 2);
164  else if (log_opts.verbose)
165  printf(" %s\n", message);
166  break;
167 
168  case PG_REPORT:
169  case PG_WARNING:
170  printf("%s", message);
171  break;
172 
173  case PG_FATAL:
174  printf("\n%s", message);
175  printf(_("Failure, exiting\n"));
176  exit(1);
177  break;
178 
179  default:
180  break;
181  }
182  fflush(stdout);
183 }
#define QUERY_ALLOC
Definition: pg_upgrade.h:22
@ PG_STATUS
Definition: pg_upgrade.h:236
@ PG_WARNING
Definition: pg_upgrade.h:238
@ PG_VERBOSE
Definition: pg_upgrade.h:235
#define vsnprintf
Definition: port.h:224
#define fprintf
Definition: port.h:229
FILE * internal
Definition: pg_upgrade.h:275

References _, exit(), fflush(), fmt, fprintf, LogOpts::internal, LogOpts::isatty, log_opts, MESSAGE_WIDTH, PG_FATAL, PG_REPORT, PG_STATUS, PG_VERBOSE, PG_WARNING, printf, QUERY_ALLOC, generate_unaccent_rules::stdout, generate_unaccent_rules::type, LogOpts::verbose, and vsnprintf.

Referenced by pg_fatal(), and pg_log().

◆ prep_status()

void prep_status ( const char *  fmt,
  ... 
)

Definition at line 72 of file util.c.

73 {
74  va_list args;
75  char message[MAX_STRING];
76 
77  va_start(args, fmt);
78  vsnprintf(message, sizeof(message), fmt, args);
79  va_end(args);
80 
81  /* trim strings */
82  pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
83 }
#define MAX_STRING
Definition: pg_upgrade.h:21

References generate_unaccent_rules::args, fmt, MAX_STRING, MESSAGE_WIDTH, pg_log(), PG_REPORT, va_end(), va_start(), and vsnprintf.

◆ prep_status_progress()

void prep_status_progress ( const char *  fmt,
  ... 
)

Definition at line 99 of file util.c.

100 {
101  va_list args;
102  char message[MAX_STRING];
103 
104  va_start(args, fmt);
105  vsnprintf(message, sizeof(message), fmt, args);
106  va_end(args);
107 
108  /*
109  * If outputting to a tty or in verbose, append newline. pg_log_v() will
110  * put the individual progress items onto the next line.
111  */
113  pg_log(PG_REPORT, "%-*s\n", MESSAGE_WIDTH, message);
114  else
115  pg_log(PG_REPORT, "%-*s", MESSAGE_WIDTH, message);
116 }

References generate_unaccent_rules::args, fmt, LogOpts::isatty, log_opts, MAX_STRING, MESSAGE_WIDTH, pg_log(), PG_REPORT, va_end(), va_start(), LogOpts::verbose, and vsnprintf.

◆ quote_identifier()

char* quote_identifier ( const char *  s)

Definition at line 227 of file util.c.

228 {
229  char *result = pg_malloc(strlen(s) * 2 + 3);
230  char *r = result;
231 
232  *r++ = '"';
233  while (*s)
234  {
235  if (*s == '"')
236  *r++ = *s;
237  *r++ = *s;
238  s++;
239  }
240  *r++ = '"';
241  *r++ = '\0';
242 
243  return result;
244 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47

References pg_malloc().

◆ report_status()

static void void report_status ( eLogType  type,
const char *  fmt,
  ... 
)

Definition at line 28 of file util.c.

29 {
30  va_list args;
31  char message[MAX_STRING];
32 
33  va_start(args, fmt);
34  vsnprintf(message, sizeof(message), fmt, args);
35  va_end(args);
36 
37  pg_log(type, "%s\n", message);
38 }

References generate_unaccent_rules::args, fmt, MAX_STRING, pg_log(), generate_unaccent_rules::type, va_end(), va_start(), and vsnprintf.

Referenced by check_ok().

◆ str2uint()

unsigned int str2uint ( const char *  str)

Definition at line 280 of file util.c.

281 {
282  return strtoul(str, NULL, 10);
283 }

References generate_unaccent_rules::str.

Referenced by get_control_data().

Variable Documentation

◆ log_opts