File: log.c

package info (click to toggle)
pg-catcheck 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 260 kB
  • sloc: ansic: 2,806; makefile: 40; sh: 4
file content (159 lines) | stat: -rw-r--r-- 3,597 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*-------------------------------------------------------------------------
 *
 * log.c
 *
 * Logging support for the system catalog integrity checker.  This vaguely
 * parallel's the backend and pg_upgrade, but with some foibles specific
 * to our particular needs.
 *
 *-------------------------------------------------------------------------
 */

#include "postgres_fe.h"

#include "pg_catcheck.h"

bool		quiet = false;		/* Don't display progress messages. */
int			verbose = 0;		/* 1 = verbose messages; 2+ = debug messages */

static int	notices = 0;
static int	warnings = 0;
static int	errors = 0;
static pgcc_severity highest_message_severity = PGCC_DEBUG;

static bool pgcc_log_severity(pgcc_severity sev);

/*
 * Log a message.  We write messages of level NOTICE and below to standard
 * output; anything higher goes to standard error.
 */
void
pgcc_log(pgcc_severity sev, char *fmt,...)
{
	va_list		args;

	if (!pgcc_log_severity(sev))
		return;

	va_start(args, fmt);
	if (sev <= PGCC_NOTICE)
		vfprintf(stdout, fmt, args);
	else
		vfprintf(stderr, fmt, args);
	va_end(args);

	if (sev >= PGCC_FATAL)
		exit(2);
}

/*
 * Report a catalog inconsistency; this always uses level PGCC_NOTICE.
 */
void
pgcc_report(pg_catalog_table *tab, pg_catalog_column *tabcol,
			int rownum, char *fmt,...)
{
	va_list		args;
	pg_catalog_column *displaytabcol;
	bool		first = true;

	if (!pgcc_log_severity(PGCC_NOTICE))
		return;
	if (tabcol != NULL)
		printf("%s row has invalid %s \"%s\": ", tab->table_name, tabcol->name,
			   PQgetvalue(tab->data, rownum, tabcol->result_column));

	va_start(args, fmt);
	vfprintf(stdout, fmt, args);
	va_end(args);

	for (displaytabcol = tab->cols;
		 displaytabcol->name != NULL;
		 ++displaytabcol)
	{
		if (displaytabcol->is_display_column)
		{
			printf("%s%s=\"%s\"", first ?
				   "row identity: " : " ", displaytabcol->name,
				PQgetvalue(tab->data, rownum, displaytabcol->result_column));
			first = false;
		}
	}
	if (!first)
		printf("\n");
}

/*
 * Report that we have completed our checks, and exit with an appropriate
 * status code.
 */
void
pgcc_log_completion(void)
{
	pgcc_log(PGCC_PROGRESS,
			 "done (%d inconsistencies, %d warnings, %d errors)\n",
			 notices, warnings, errors);
	if (highest_message_severity > PGCC_NOTICE)
		exit(2);
	else if (highest_message_severity == PGCC_NOTICE)
		exit(1);
	exit(0);
}

/*
 * Common code for pgcc_log() and pgcc_report().
 *
 * Determine whether a message of the indicated severity should be logged
 * given the command-line options specified by the user, and print out the
 * severity level on standard output or standard error as appropriate, so that
 * it prefixes the message.
 *
 * Also updates statistics on what messages have been logged so that
 * pgcc_log_completion() can make use of that information.
 */
static bool
pgcc_log_severity(pgcc_severity sev)
{
	switch (sev)
	{
		case PGCC_DEBUG:
			if (verbose < 2)
				return false;
			printf("debug: ");
			break;
		case PGCC_VERBOSE:
			if (verbose < 1)
				return false;
			printf("verbose: ");
			break;
		case PGCC_PROGRESS:
			if (quiet)
				return false;
			printf("progress: ");
			break;
		case PGCC_NOTICE:
			printf("notice: ");
			break;
		case PGCC_WARNING:
			fprintf(stderr, "warning: ");
			break;
		case PGCC_ERROR:
			fprintf(stderr, "error: ");
			break;
		case PGCC_FATAL:
			fprintf(stderr, "fatal: ");
			break;
	}

	if (sev == PGCC_NOTICE)
		++notices;
	if (sev == PGCC_WARNING)
		++warnings;
	if (sev == PGCC_ERROR)
		++errors;

	if (sev >= highest_message_severity)
		highest_message_severity = sev;

	return true;
}