1
0
mirror of https://github.com/postgrespro/pg_probackup.git synced 2024-11-27 09:21:18 +02:00

Added pgut/logger.h and pgut/logger.c for message logging

This commit is contained in:
Arthur Zakirov 2017-04-25 19:44:49 +03:00
parent eb97837356
commit 69b41b2d94
6 changed files with 145 additions and 81 deletions

View File

@ -20,7 +20,8 @@ OBJS = backup.o \
xlogreader.o \
streamutil.o \
receivelog.o \
pgut/pgut.o
pgut/pgut.o \
pgut/logger.o
EXTRA_CLEAN = datapagemap.c datapagemap.h xlogreader.c receivelog.c receivelog.h streamutil.c streamutil.h logging.h

View File

@ -15,22 +15,25 @@
#include <limits.h>
#include "libpq-fe.h"
#include "pgut/pgut.h"
#include "access/xlogdefs.h"
#include "access/xlog_internal.h"
#include "catalog/pg_control.h"
#include "utils/pg_crc.h"
#include "parray.h"
#include "datapagemap.h"
#include "storage/bufpage.h"
#include "storage/block.h"
#include "storage/checksum.h"
#include "access/timeline.h"
#ifndef WIN32
#include <sys/mman.h>
#endif
#include "access/timeline.h"
#include "access/xlogdefs.h"
#include "access/xlog_internal.h"
#include "catalog/pg_control.h"
#include "storage/block.h"
#include "storage/bufpage.h"
#include "storage/checksum.h"
#include "utils/pg_crc.h"
#include "pgut/pgut.h"
#include "datapagemap.h"
#include "parray.h"
/* Directory/File names */
#define DATABASE_DIR "database"
#define BACKUPS_DIR "backups"

82
pgut/logger.c Normal file
View File

@ -0,0 +1,82 @@
/*-------------------------------------------------------------------------
*
* logger.c: - log events into csv-file or stderr.
*
* Portions Copyright (c) 2017-2017, Postgres Professional
*
*-------------------------------------------------------------------------
*/
#include "postgres_fe.h"
#include "logger.h"
/* Logger parameters */
int log_destination = LOG_DESTINATION_STDERR;
int log_level = INFO;
bool quiet = false;
char *log_filename = NULL;
char *log_error_filename = NULL;
char *log_directory = NULL;
int log_rotation_size = 0;
int log_rotation_age = 0;
/*
* elog - log to stderr or to log file and exit if ERROR or FATAL
*/
void
elog(int elevel, const char *fmt, ...)
{
va_list args;
/* Do not log message if severity level is less than log_level */
if (elevel < log_level)
{
/* But exit with code it severity level is higher than WARNING */
if (elevel > WARNING)
exit(elevel);
return;
}
if (quiet && elevel < WARNING)
return;
switch (elevel)
{
case LOG:
fputs("LOG: ", stderr);
break;
case INFO:
fputs("INFO: ", stderr);
break;
case NOTICE:
fputs("NOTICE: ", stderr);
break;
case WARNING:
fputs("WARNING: ", stderr);
break;
case FATAL:
fputs("FATAL: ", stderr);
break;
case PANIC:
fputs("PANIC: ", stderr);
break;
default:
if (elevel >= ERROR)
fputs("ERROR: ", stderr);
break;
}
va_start(args, fmt);
vfprintf(stderr, fmt, args);
fputc('\n', stderr);
fflush(stderr);
va_end(args);
if (elevel > WARNING)
exit(elevel);
}

43
pgut/logger.h Normal file
View File

@ -0,0 +1,43 @@
/*-------------------------------------------------------------------------
*
* logger.h: - prototypes of logger functions.
*
* Portions Copyright (c) 2017-2017, Postgres Professional
*
*-------------------------------------------------------------------------
*/
#ifndef LOGGER_H
#define LOGGER_H
/* Log destination bitmap */
#define LOG_DESTINATION_STDERR 1
#define LOG_DESTINATION_LOGFILE 2
/* Log level */
#define VERBOSE (-5)
#define LOG (-4)
#define INFO (-3)
#define NOTICE (-2)
#define WARNING (-1)
#define ERROR 1
#define FATAL 2
#define PANIC 3
/* Logger parameters */
extern int log_destination;
extern int log_level;
extern bool quiet;
extern char *log_filename;
extern char *log_error_filename;
extern char *log_directory;
extern int log_rotation_size;
extern int log_rotation_age;
#undef elog
extern void elog(int elevel, const char *fmt, ...) pg_attribute_printf(2, 3);
#endif /* LOGGER_H */

View File

@ -15,6 +15,7 @@
#include <time.h>
#include <unistd.h>
#include "logger.h"
#include "pgut.h"
/* old gcc doesn't have LLONG_MAX. */
@ -34,7 +35,6 @@ const char *port = NULL;
const char *username = NULL;
char *password = NULL;
bool verbose = false;
bool quiet = false;
bool prompt_password = true;
/* Database connections */
@ -1032,55 +1032,6 @@ pgut_wait(int num, PGconn *connections[], struct timeval *timeout)
return -1;
}
/*
* elog - log to stderr and exit if ERROR or FATAL
*/
void
elog(int elevel, const char *fmt, ...)
{
va_list args;
if (!verbose && elevel <= LOG)
return;
if (quiet && elevel < WARNING)
return;
switch (elevel)
{
case LOG:
fputs("LOG: ", stderr);
break;
case INFO:
fputs("INFO: ", stderr);
break;
case NOTICE:
fputs("NOTICE: ", stderr);
break;
case WARNING:
fputs("WARNING: ", stderr);
break;
case FATAL:
fputs("FATAL: ", stderr);
break;
case PANIC:
fputs("PANIC: ", stderr);
break;
default:
if (elevel >= ERROR)
fputs("ERROR: ", stderr);
break;
}
va_start(args, fmt);
vfprintf(stderr, fmt, args);
fputc('\n', stderr);
fflush(stderr);
va_end(args);
if (elevel > 0)
exit_or_abort(elevel);
}
void pg_log(eLogType type, const char *fmt, ...)
{
va_list args;

View File

@ -16,6 +16,8 @@
#include <assert.h>
#include <sys/time.h>
#include "logger.h"
#if !defined(C_H) && !defined(__cplusplus)
#ifndef bool
typedef char bool;
@ -84,7 +86,6 @@ extern const char *port;
extern const char *username;
extern char *password;
extern bool verbose;
extern bool quiet;
extern bool prompt_password;
extern bool interrupted;
@ -126,23 +127,6 @@ extern char *strdup_trim(const char *str);
*/
extern FILE *pgut_fopen(const char *path, const char *mode, bool missing_ok);
/*
* elog
*/
#define VERBOSE (-5)
#define LOG (-4)
#define INFO (-3)
#define NOTICE (-2)
#define WARNING (-1)
#define ERROR 1
#define FATAL 2
#define PANIC 3
#undef elog
extern void
elog(int elevel, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
/*
* Assert
*/