Skip to content

Commit 6e524a0

Browse files
committed
added trace logs
1 parent 64f2049 commit 6e524a0

File tree

3 files changed

+55
-27
lines changed

3 files changed

+55
-27
lines changed

postgres_asio/postgres_asio.cpp

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,22 @@ namespace postgres_asio
2525
{
2626
inline static boost::shared_ptr<PGresult> make_shared(PGresult* p) { return boost::shared_ptr<PGresult>(p, PQclear); }
2727

28-
connection::connection(boost::asio::io_service& fg, boost::asio::io_service& bg) :
28+
connection::connection(boost::asio::io_service& fg, boost::asio::io_service& bg, std::string trace_id) :
2929
_fg_ios(fg),
3030
_bg_ios(bg),
3131
_socket(fg),
3232
_pg_conn(NULL),
33-
_warn_timeout(60000)
33+
_warn_timeout(60000),
34+
_trace_id(trace_id)
3435
{
35-
_log_id = to_string(boost::uuids::random_generator()());
36+
if (!_trace_id.size())
37+
_trace_id = to_string(boost::uuids::random_generator()());
38+
BOOST_LOG_TRIVIAL(trace) << _trace_id << ", " << BOOST_CURRENT_FUNCTION;
3639
}
3740

3841
connection::~connection()
3942
{
40-
BOOST_LOG_TRIVIAL(debug) << _log_id << ", postgres diconnecting";
43+
BOOST_LOG_TRIVIAL(trace) << _trace_id << ", " << BOOST_CURRENT_FUNCTION;
4144
PQfinish(_pg_conn);
4245
}
4346

@@ -51,11 +54,10 @@ namespace postgres_asio
5154
uint32_t connection::backend_pid() const { return (uint32_t)PQbackendPID(_pg_conn); }
5255
int connection::socket() const { return PQsocket(_pg_conn); }
5356
bool connection::set_client_encoding(std::string s) { return (PQsetClientEncoding(_pg_conn, s.c_str()) == 0); }
54-
void connection::set_log_id(std::string id) { _log_id = id; }
55-
std::string connection::get_log_id() const { return _log_id; }
57+
std::string connection::trace_id() const { return _trace_id; }
5658
void connection::set_warning_timout(uint32_t ms) { _warn_timeout = ms; }
5759

58-
// connect is a blocking thingh - pass this to bg thread pool
60+
// connect is a blocking thing - pass this to bg thread pool
5961
void connection::connect(std::string connect_string, on_connect_callback cb)
6062
{
6163
auto self(shared_from_this()); // keeps connection alive until cb is done
@@ -103,33 +105,34 @@ namespace postgres_asio
103105
{
104106
if (duration > _warn_timeout)
105107
{
106-
BOOST_LOG_TRIVIAL(warning) << _log_id << ", postgres::connect - took long time, t=" << duration;
108+
BOOST_LOG_TRIVIAL(warning) << _trace_id << ", postgres::connect - took long time, t=" << duration;
107109
}
108110

109-
BOOST_LOG_TRIVIAL(info) << _log_id << ", postgres::connect PQconnectdb complete, t=" << duration;
111+
BOOST_LOG_TRIVIAL(info) << _trace_id << ", postgres::connect PQconnectdb complete, t=" << duration;
110112
_socket.assign(boost::asio::ip::tcp::v4(), socket());
111113
_fg_ios.post([this, self, cb](){ cb(0); });
112114
return;
113115
}
114-
BOOST_LOG_TRIVIAL(error) << _log_id << ", postgres::connect PQconnectdb failed, status=" << status << ", " << last_error() << ", t=" << duration;
116+
BOOST_LOG_TRIVIAL(error) << _trace_id << ", postgres::connect PQconnectdb failed, status=" << status << ", " << last_error() << ", t=" << duration;
115117
_fg_ios.post([this, self, status, cb](){ cb(status); });
116118
}
117119

118120
void connection::exec(std::string statement, on_query_callback cb)
119121
{
120-
BOOST_LOG_TRIVIAL(trace) << _log_id << ", " << BOOST_CURRENT_FUNCTION << ", s=" << statement.substr(0, STATEMENT_LOG_BYTES);
122+
BOOST_LOG_TRIVIAL(trace) << _trace_id << ", " << BOOST_CURRENT_FUNCTION << ", s=" << statement.substr(0, STATEMENT_LOG_BYTES);
121123
auto self(shared_from_this()); // keeps connection alive until cb is done
122124
_start_ts = now();
123125
_current_statement = statement;
124126
if (PQsendQuery(_pg_conn, statement.c_str()) == 0) // 1 os good, 0 is bad...
125127
{
126-
BOOST_LOG_TRIVIAL(error) << _log_id << ", postgres::exec PQsendQuery failed fast: s=" << statement.substr(0, STATEMENT_LOG_BYTES);
128+
BOOST_LOG_TRIVIAL(error) << _trace_id << ", postgres::exec PQsendQuery failed fast: s=" << statement.substr(0, STATEMENT_LOG_BYTES);
127129
_fg_ios.post([this, self, cb](){ cb(PGRES_FATAL_ERROR, NULL); });
128130
return;
129131
}
130132
_socket.async_read_some(boost::asio::null_buffers(), boost::bind(&connection::_fg_socket_rx_cb, this, boost::asio::placeholders::error, self, cb));
131133
}
132134

135+
133136
std::pair<int, boost::shared_ptr<PGresult>> connection::exec(std::string statement)
134137
{
135138
std::promise<std::pair<int, boost::shared_ptr<PGresult>>> p;
@@ -145,18 +148,18 @@ namespace postgres_asio
145148

146149
void connection::_fg_socket_rx_cb(const boost::system::error_code& ec, boost::shared_ptr<connection> self, on_query_callback cb)
147150
{
148-
BOOST_LOG_TRIVIAL(trace) << _log_id << ", " << BOOST_CURRENT_FUNCTION;
151+
BOOST_LOG_TRIVIAL(trace) << _trace_id << ", " << BOOST_CURRENT_FUNCTION;
149152
if (ec)
150153
{
151-
BOOST_LOG_TRIVIAL(warning) << _log_id << ", postgres::exec asio ec:" << ec.message();
154+
BOOST_LOG_TRIVIAL(warning) << _trace_id << ", postgres::exec asio ec:" << ec.message();
152155
cb(ec.value(), NULL);
153156
return;
154157
}
155158

156159
int res = PQconsumeInput(_pg_conn);
157160
if (!res)
158161
{
159-
BOOST_LOG_TRIVIAL(warning) << _log_id << ", postgres::exec PQconsumeInput read error";
162+
BOOST_LOG_TRIVIAL(warning) << _trace_id << ", postgres::exec PQconsumeInput read error";
160163
cb(PGRES_FATAL_ERROR, NULL); // we reuse a error code here...
161164
return;
162165
}
@@ -169,11 +172,11 @@ namespace postgres_asio
169172
{
170173
if (PQisBusy(_pg_conn))
171174
{
172-
BOOST_LOG_TRIVIAL(trace) << _log_id << ", " << BOOST_CURRENT_FUNCTION << ", PQisBusy() - reading more";
175+
BOOST_LOG_TRIVIAL(trace) << _trace_id << ", " << BOOST_CURRENT_FUNCTION << ", PQisBusy() - reading more";
173176
_socket.async_read_some(boost::asio::null_buffers(), boost::bind(&connection::_fg_socket_rx_cb, this, boost::asio::placeholders::error, self, cb));
174177
return;
175178
}
176-
BOOST_LOG_TRIVIAL(trace) << _log_id << ", " << BOOST_CURRENT_FUNCTION << ", parsing result";
179+
BOOST_LOG_TRIVIAL(trace) << _trace_id << ", " << BOOST_CURRENT_FUNCTION << ", parsing result";
177180
auto r = make_shared(PQgetResult(_pg_conn));
178181
if (r.get() == NULL)
179182
break;
@@ -187,7 +190,7 @@ namespace postgres_asio
187190

188191
if (_results.size() == 0)
189192
{
190-
BOOST_LOG_TRIVIAL(error) << _log_id << ", postgres::exec returned no result, t=" << duration << ", s=" << _current_statement.substr(0, STATEMENT_LOG_BYTES);
193+
BOOST_LOG_TRIVIAL(error) << _trace_id << ", postgres::exec returned no result, t=" << duration << ", s=" << _current_statement.substr(0, STATEMENT_LOG_BYTES);
191194
cb(PGRES_FATAL_ERROR, NULL); // we reuse a error code here...
192195
return;
193196
}
@@ -206,24 +209,39 @@ namespace postgres_asio
206209
case PGRES_NONFATAL_ERROR:
207210
case PGRES_COPY_BOTH:
208211
case PGRES_SINGLE_TUPLE:
209-
BOOST_LOG_TRIVIAL(debug) << _log_id << ", postgres::exec complete, t=" << duration << ", s=" << _current_statement.substr(0, STATEMENT_LOG_BYTES);
212+
BOOST_LOG_TRIVIAL(debug) << _trace_id << ", postgres::exec complete, t=" << duration << ", s=" << _current_statement.substr(0, STATEMENT_LOG_BYTES);
210213
if (duration > _warn_timeout)
211214
{
212-
BOOST_LOG_TRIVIAL(warning) << _log_id << ", postgres::exec complete - took long time, t=" << duration << ", s = " << _current_statement.substr(0, STATEMENT_LOG_BYTES);
215+
BOOST_LOG_TRIVIAL(warning) << _trace_id << ", postgres::exec complete - took long time, t=" << duration << ", s = " << _current_statement.substr(0, STATEMENT_LOG_BYTES);
213216
}
214217
cb(0, std::move(last_result));
215218
break;
216219
case PGRES_BAD_RESPONSE:
217220
case PGRES_FATAL_ERROR:
218-
BOOST_LOG_TRIVIAL(error) << _log_id << ", postgres::exec failed " << last_error() << ", t=" << duration << ", s=" << _current_statement.substr(0, STATEMENT_LOG_BYTES);
221+
BOOST_LOG_TRIVIAL(error) << _trace_id << ", postgres::exec failed " << last_error() << ", t=" << duration << ", s=" << _current_statement.substr(0, STATEMENT_LOG_BYTES);
219222
cb(status, std::move(last_result));
220223
break;
221224
default:
222-
BOOST_LOG_TRIVIAL(warning) << _log_id << ", postgres::exec unknown status code, t=" << duration << ", s=" << _current_statement.substr(0, STATEMENT_LOG_BYTES);
225+
BOOST_LOG_TRIVIAL(warning) << _trace_id << ", postgres::exec unknown status code, t=" << duration << ", s=" << _current_statement.substr(0, STATEMENT_LOG_BYTES);
223226
cb(status, std::move(last_result));
224227
break;
225228
}
226229
}
227230

231+
232+
//connection_pool::connection_pool(boost::asio::io_service& fg, boost::asio::io_service& bg) : _fg_ios(fg), _bg_ios(bg)
233+
//{
234+
//}
235+
236+
////TBD reuse connections.
237+
//boost::shared_ptr<postgres_asio::connection> connection_pool::create()
238+
//{
239+
// return boost::make_shared<postgres_asio::connection>(_fg_ios, _bg_ios);
240+
//}
241+
//
242+
//// TBD
243+
//void connection_pool::release(boost::shared_ptr<postgres_asio::connection>)
244+
//{
245+
//}
228246
};
229247

postgres_asio/postgres_asio.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace postgres_asio
2929
typedef boost::function<void(int ec)> on_connect_callback;
3030
typedef boost::function<void(int ec, boost::shared_ptr<PGresult>)> on_query_callback;
3131

32-
connection(boost::asio::io_service& fg, boost::asio::io_service& bg);
32+
connection(boost::asio::io_service& fg, boost::asio::io_service& bg, std::string trace_id="");
3333
~connection();
3434

3535
/*
@@ -63,8 +63,7 @@ namespace postgres_asio
6363
uint32_t backend_pid() const;
6464

6565
bool set_client_encoding(std::string s);
66-
void set_log_id(std::string id);
67-
std::string get_log_id() const;
66+
std::string trace_id() const;
6867
void set_warning_timout(uint32_t ms);
6968

7069
//async exec
@@ -81,10 +80,21 @@ namespace postgres_asio
8180
boost::asio::io_service& _bg_ios;
8281
boost::asio::ip::tcp::socket _socket;
8382
PGconn* _pg_conn;
84-
std::string _log_id;
83+
std::string _trace_id;
8584
int64_t _start_ts;
8685
int32_t _warn_timeout;
8786
std::string _current_statement;
8887
std::deque<boost::shared_ptr<PGresult>> _results;
8988
};
89+
90+
/* class connection_pool
91+
{
92+
public:
93+
connection_pool(boost::asio::io_service& fg, boost::asio::io_service& bg);
94+
boost::shared_ptr<postgres_asio::connection> create();
95+
void release(boost::shared_ptr<postgres_asio::connection>);
96+
protected:
97+
boost::asio::io_service& _fg_ios;
98+
boost::asio::io_service& _bg_ios;
99+
};*/
90100
};

samples/insert_sample/insert_sample.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main(int argc, char *argv[])
3333
for (int i = 0; i != 50; ++i)
3434
{
3535
auto connection = boost::make_shared<postgres_asio::connection>(fg_ios, bg_ios);
36-
connection->set_log_id("xxxx-xxxx-xx" + std::to_string(i));
36+
std::string trace_id = "xxxx-xxxx-xx" + std::to_string(i);
3737
connection->connect(connect_string, [connection](int ec)
3838
{
3939
if (!ec)

0 commit comments

Comments
 (0)