mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
In pg_upgrade, add various logging improvements:
add ability to control permissions of created files have psql echo its queries for easier debugging output four separate log files, and delete them on success add -r/--retain option to keep log files after success make logs file append-only remove -g/-G/-l logging options sugggest tailing appropriate log file on failure enhance -v/--verbose behavior
This commit is contained in:
@ -11,8 +11,10 @@
|
||||
|
||||
#include "pg_upgrade.h"
|
||||
|
||||
#include "getopt_long.h"
|
||||
|
||||
#include <getopt_long.h>
|
||||
#include <time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#ifdef WIN32
|
||||
#include <io.h>
|
||||
#endif
|
||||
@ -46,18 +48,18 @@ parseCommandLine(int argc, char *argv[])
|
||||
|
||||
{"user", required_argument, NULL, 'u'},
|
||||
{"check", no_argument, NULL, 'c'},
|
||||
{"debug", no_argument, NULL, 'g'},
|
||||
{"debugfile", required_argument, NULL, 'G'},
|
||||
{"link", no_argument, NULL, 'k'},
|
||||
{"logfile", required_argument, NULL, 'l'},
|
||||
{"retain", no_argument, NULL, 'r'},
|
||||
{"verbose", no_argument, NULL, 'v'},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
int option; /* Command line option */
|
||||
int optindex = 0; /* used by getopt_long */
|
||||
int os_user_effective_id;
|
||||
char *return_buf;
|
||||
|
||||
FILE *fp;
|
||||
int i;
|
||||
time_t run_time = time(NULL);
|
||||
|
||||
user_opts.transfer_mode = TRANSFER_MODE_COPY;
|
||||
|
||||
os_info.progname = get_progname(argv[0]);
|
||||
@ -94,11 +96,10 @@ parseCommandLine(int argc, char *argv[])
|
||||
if (os_user_effective_id == 0)
|
||||
pg_log(PG_FATAL, "%s: cannot be run as root\n", os_info.progname);
|
||||
|
||||
return_buf = getcwd(os_info.cwd, MAXPGPATH);
|
||||
if (return_buf == NULL)
|
||||
pg_log(PG_FATAL, "Could not access current working directory: %s\n", getErrorText(errno));
|
||||
if ((log_opts.internal = fopen_priv(INTERNAL_LOG_FILE, "a")) == NULL)
|
||||
pg_log(PG_FATAL, "cannot write to log file %s\n", INTERNAL_LOG_FILE);
|
||||
|
||||
while ((option = getopt_long(argc, argv, "d:D:b:B:cgG:kl:o:O:p:P:u:v",
|
||||
while ((option = getopt_long(argc, argv, "d:D:b:B:cko:O:p:P:ru:v",
|
||||
long_options, &optindex)) != -1)
|
||||
{
|
||||
switch (option)
|
||||
@ -125,27 +126,10 @@ parseCommandLine(int argc, char *argv[])
|
||||
new_cluster.pgconfig = pg_strdup(optarg);
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
pg_log(PG_REPORT, "Running in debug mode\n");
|
||||
log_opts.debug = true;
|
||||
break;
|
||||
|
||||
case 'G':
|
||||
if ((log_opts.debug_fd = fopen(optarg, "w")) == NULL)
|
||||
{
|
||||
pg_log(PG_FATAL, "cannot open debug file\n");
|
||||
exit(1);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'k':
|
||||
user_opts.transfer_mode = TRANSFER_MODE_LINK;
|
||||
break;
|
||||
|
||||
case 'l':
|
||||
log_opts.filename = pg_strdup(optarg);
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
old_cluster.pgopts = pg_strdup(optarg);
|
||||
break;
|
||||
@ -175,6 +159,10 @@ parseCommandLine(int argc, char *argv[])
|
||||
}
|
||||
break;
|
||||
|
||||
case 'r':
|
||||
log_opts.retain = true;
|
||||
break;
|
||||
|
||||
case 'u':
|
||||
pg_free(os_info.user);
|
||||
os_info.user = pg_strdup(optarg);
|
||||
@ -199,36 +187,18 @@ parseCommandLine(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
if (log_opts.filename != NULL)
|
||||
/* label start of upgrade in logfiles */
|
||||
for (i = 0; i < NUM_LOG_FILES; i++)
|
||||
{
|
||||
/*
|
||||
* We must use append mode so output generated by child processes via
|
||||
* ">>" will not be overwritten, and we want the file truncated on
|
||||
* start.
|
||||
*/
|
||||
/* truncate */
|
||||
if ((log_opts.fd = fopen(log_opts.filename, "w")) == NULL)
|
||||
pg_log(PG_FATAL, "cannot write to log file %s\n", log_opts.filename);
|
||||
fclose(log_opts.fd);
|
||||
if ((log_opts.fd = fopen(log_opts.filename, "a")) == NULL)
|
||||
pg_log(PG_FATAL, "cannot write to log file %s\n", log_opts.filename);
|
||||
}
|
||||
else
|
||||
log_opts.filename = pg_strdup(DEVNULL);
|
||||
|
||||
/* WIN32 files do not accept writes from multiple processes */
|
||||
#ifndef WIN32
|
||||
log_opts.filename2 = pg_strdup(log_opts.filename);
|
||||
#else
|
||||
log_opts.filename2 = pg_strdup(DEVNULL);
|
||||
#endif
|
||||
|
||||
/* if no debug file name, output to the terminal */
|
||||
if (log_opts.debug && !log_opts.debug_fd)
|
||||
{
|
||||
log_opts.debug_fd = fopen(DEVTTY, "w");
|
||||
if (!log_opts.debug_fd)
|
||||
pg_log(PG_FATAL, "cannot write to terminal\n");
|
||||
if ((fp = fopen_priv(output_files[i], "a")) == NULL)
|
||||
pg_log(PG_FATAL, "cannot write to log file %s\n",
|
||||
output_files[i]);
|
||||
fprintf(fp, "\n"
|
||||
"-----------------------------------------------------------------\n"
|
||||
" pg_upgrade run on %s"
|
||||
"-----------------------------------------------------------------\n\n",
|
||||
ctime(&run_time));
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
/* Get values from env if not already set */
|
||||
@ -256,16 +226,14 @@ Options:\n\
|
||||
-c, --check check clusters only, don't change any data\n\
|
||||
-d, --old-datadir=OLDDATADIR old cluster data directory\n\
|
||||
-D, --new-datadir=NEWDATADIR new cluster data directory\n\
|
||||
-g, --debug enable debugging\n\
|
||||
-G, --debugfile=FILENAME output debugging activity to file\n\
|
||||
-k, --link link instead of copying files to new cluster\n\
|
||||
-l, --logfile=FILENAME log internal activity to file\n\
|
||||
-o, --old-options=OPTIONS old cluster options to pass to the server\n\
|
||||
-O, --new-options=OPTIONS new cluster options to pass to the server\n\
|
||||
-p, --old-port=OLDPORT old cluster port number (default %d)\n\
|
||||
-P, --new-port=NEWPORT new cluster port number (default %d)\n\
|
||||
-r, --retain retain SQL and log files after success\n\
|
||||
-u, --user=NAME cluster superuser (default \"%s\")\n\
|
||||
-v, --verbose enable verbose output\n\
|
||||
-v, --verbose enable verbose internal logging\n\
|
||||
-V, --version display version information, then exit\n\
|
||||
-h, --help show this help, then exit\n\
|
||||
\n\
|
||||
@ -354,19 +322,19 @@ adjust_data_dir(ClusterInfo *cluster)
|
||||
{
|
||||
char filename[MAXPGPATH];
|
||||
char cmd[MAXPGPATH], cmd_output[MAX_STRING];
|
||||
FILE *fd, *output;
|
||||
FILE *fp, *output;
|
||||
|
||||
/* If there is no postgresql.conf, it can't be a config-only dir */
|
||||
snprintf(filename, sizeof(filename), "%s/postgresql.conf", cluster->pgconfig);
|
||||
if ((fd = fopen(filename, "r")) == NULL)
|
||||
if ((fp = fopen(filename, "r")) == NULL)
|
||||
return;
|
||||
fclose(fd);
|
||||
fclose(fp);
|
||||
|
||||
/* If PG_VERSION exists, it can't be a config-only dir */
|
||||
snprintf(filename, sizeof(filename), "%s/PG_VERSION", cluster->pgconfig);
|
||||
if ((fd = fopen(filename, "r")) != NULL)
|
||||
if ((fp = fopen(filename, "r")) != NULL)
|
||||
{
|
||||
fclose(fd);
|
||||
fclose(fp);
|
||||
return;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user