Log the warn/error messages coming from libpq in a correct format
This commit is contained in:
@@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
#include <logger/logger.hpp>
|
#include <logger/logger.hpp>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
PostgresqlEngine::PostgresqlEngine(PGconn*const conn):
|
PostgresqlEngine::PostgresqlEngine(PGconn*const conn):
|
||||||
conn(conn)
|
conn(conn)
|
||||||
{}
|
{}
|
||||||
@@ -20,6 +22,15 @@ PostgresqlEngine::~PostgresqlEngine()
|
|||||||
PQfinish(this->conn);
|
PQfinish(this->conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void logging_notice_processor(void*, const char* original)
|
||||||
|
{
|
||||||
|
if (original && std::strlen(original) > 0)
|
||||||
|
{
|
||||||
|
std::string message{original, std::strlen(original) - 1};
|
||||||
|
log_warning("PostgreSQL: ", message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::unique_ptr<DatabaseEngine> PostgresqlEngine::open(const std::string& conninfo)
|
std::unique_ptr<DatabaseEngine> PostgresqlEngine::open(const std::string& conninfo)
|
||||||
{
|
{
|
||||||
PGconn* con = PQconnectdb(conninfo.data());
|
PGconn* con = PQconnectdb(conninfo.data());
|
||||||
@@ -37,6 +48,7 @@ std::unique_ptr<DatabaseEngine> PostgresqlEngine::open(const std::string& connin
|
|||||||
PQfinish(con);
|
PQfinish(con);
|
||||||
throw std::runtime_error("failed to open connection.");
|
throw std::runtime_error("failed to open connection.");
|
||||||
}
|
}
|
||||||
|
PQsetNoticeProcessor(con, &logging_notice_processor, nullptr);
|
||||||
return std::make_unique<PostgresqlEngine>(con);
|
return std::make_unique<PostgresqlEngine>(con);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user