Skip to content

Commit 64f2049

Browse files
committed
more log info, fixed broken program exit
1 parent daacd79 commit 64f2049

File tree

3 files changed

+46
-42
lines changed

3 files changed

+46
-42
lines changed

samples/cursor_sample/cursor_sample.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ void handle_fetch1000(boost::shared_ptr<postgres_asio::connection> connection, s
1515

1616
int tuples_in_batch = PQntuples(res.get());
1717
total_count += tuples_in_batch;
18-
std::cerr << "got " << tuples_in_batch << ", total: " << total_count << std::endl;
18+
BOOST_LOG_TRIVIAL(info) << "got " << tuples_in_batch << ", total: " << total_count;
1919
if (tuples_in_batch == 0)
2020
{
21-
connection->exec("CLOSE mycursor; COMMIT", [connection](int ec, boost::shared_ptr<PGresult> res) { });
21+
BOOST_LOG_TRIVIAL(info) << "calling async_exec CLOSE mycursor; COMMIT....";
22+
connection->exec("CLOSE mycursor; COMMIT", [connection](int ec, boost::shared_ptr<PGresult> res)
23+
{
24+
BOOST_LOG_TRIVIAL(info) << "async processing done";
25+
});
2226
return;
2327
}
28+
BOOST_LOG_TRIVIAL(info) << "calling async_exec FETCH 1000 in mycursor....";
2429
connection->exec("FETCH 1000 in mycursor", [connection, total_count](int ec, boost::shared_ptr<PGresult> res) { handle_fetch1000(connection, total_count, ec, std::move(res)); });
2530
}
2631

2732
int main(int argc, char *argv[])
2833
{
2934
std::string host;
30-
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info);
35+
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::debug);
3136
std::string connect_string = "user=postgres password=postgres dbname=test";
3237

3338
if (argc > 1)
@@ -37,8 +42,8 @@ int main(int argc, char *argv[])
3742

3843
boost::asio::io_service fg_ios;
3944
boost::asio::io_service bg_ios;
40-
std::auto_ptr<boost::asio::io_service::work> work2(new boost::asio::io_service::work(fg_ios));
41-
std::auto_ptr<boost::asio::io_service::work> work1(new boost::asio::io_service::work(bg_ios));
45+
std::auto_ptr<boost::asio::io_service::work> fg_work(new boost::asio::io_service::work(fg_ios)); // this keeps the fg_ios alive
46+
std::auto_ptr<boost::asio::io_service::work> bg_work(new boost::asio::io_service::work(bg_ios)); // this keeps the bg_ios alive
4247
boost::thread fg(boost::bind(&boost::asio::io_service::run, &fg_ios));
4348
boost::thread bg(boost::bind(&boost::asio::io_service::run, &bg_ios));
4449

@@ -48,35 +53,36 @@ int main(int argc, char *argv[])
4853
{
4954
if (!ec)
5055
{
56+
BOOST_LOG_TRIVIAL(info) << "calling async_exec BEGIN";
5157
connection->exec("BEGIN", [connection](int ec, boost::shared_ptr<PGresult> res)
5258
{
5359
if (ec)
5460
{
55-
std::cerr << "BEGIN failed ec:" << ec << " last_error: " << connection->last_error() << std::endl;
61+
BOOST_LOG_TRIVIAL(error) << "BEGIN failed ec:" << ec << " last_error: " << connection->last_error();
5662
return;
5763
}
64+
BOOST_LOG_TRIVIAL(info) << "calling async_exec DECLARE mycursor....";
5865
connection->exec("DECLARE mycursor CURSOR FOR SELECT * from postgres_asio_sample", [connection](int ec, boost::shared_ptr<PGresult> res)
5966
{
6067
if (ec)
6168
{
62-
std::cerr << "DECLARE mycursor... failed ec:" << ec << " last_error: " << connection->last_error() << std::endl;
69+
BOOST_LOG_TRIVIAL(error) << "DECLARE mycursor... failed ec:" << ec << " last_error: " << connection->last_error();
6370
return;
6471
}
72+
BOOST_LOG_TRIVIAL(info) << "calling async_exec FETCH 1000 in mycursor....";
6573
connection->exec("FETCH 1000 in mycursor", [connection](int ec, boost::shared_ptr<PGresult> res){ handle_fetch1000(connection, 0, ec, std::move(res)); });
6674
});
6775
});
6876
}
6977
});
7078

71-
while (true)
72-
{
73-
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
74-
}
79+
BOOST_LOG_TRIVIAL(debug) << "work reset";
80+
bg_work.reset();
81+
BOOST_LOG_TRIVIAL(debug) << "bg join";
82+
bg.join();
7583

76-
work1.reset();
77-
work2.reset();
78-
//bg_ios.stop();
79-
//fg_ios.stop();
80-
bg.join();
81-
fg.join();
84+
fg_work.reset();
85+
BOOST_LOG_TRIVIAL(debug) << "fg join";
86+
fg.join();
87+
BOOST_LOG_TRIVIAL(debug) << "done";
8288
}

samples/insert_sample/insert_sample.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,11 @@ int main(int argc, char *argv[])
6262
}
6363

6464
BOOST_LOG_TRIVIAL(debug) << "work reset";
65-
fg_work.reset();
6665
bg_work.reset();
6766
BOOST_LOG_TRIVIAL(debug) << "bg join";
6867
bg.join();
68+
69+
fg_work.reset();
6970
BOOST_LOG_TRIVIAL(debug) << "fg join";
7071
fg.join();
7172
BOOST_LOG_TRIVIAL(debug) << "done";

samples/query_sample/query_sample.cpp

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
int main(int argc, char *argv[])
1111
{
12-
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::info);
12+
boost::log::core::get()->set_filter(boost::log::trivial::severity >= boost::log::trivial::debug);
1313
std::string connect_string = "user=postgres password=postgres dbname=test";
1414

1515
if (argc > 1)
@@ -19,55 +19,52 @@ int main(int argc, char *argv[])
1919

2020
boost::asio::io_service fg_ios;
2121
boost::asio::io_service bg_ios;
22-
std::auto_ptr<boost::asio::io_service::work> work2(new boost::asio::io_service::work(fg_ios));
23-
std::auto_ptr<boost::asio::io_service::work> work1(new boost::asio::io_service::work(bg_ios));
22+
std::auto_ptr<boost::asio::io_service::work> fg_work(new boost::asio::io_service::work(fg_ios)); // this keeps the fg_ios alive
23+
std::auto_ptr<boost::asio::io_service::work> bg_work(new boost::asio::io_service::work(bg_ios)); // this keeps the bg_ios alive
2424
boost::thread fg(boost::bind(&boost::asio::io_service::run, &fg_ios));
2525
boost::thread bg(boost::bind(&boost::asio::io_service::run, &bg_ios));
2626

2727
auto connection = boost::make_shared<postgres_asio::connection>(fg_ios, bg_ios);
2828
connection->connect(connect_string, [connection](int ec)
2929
{
30-
std::cerr << "connect_async : " << ec << std::endl;
30+
BOOST_LOG_TRIVIAL(info) << "connect_async ec : " << ec;
3131
if (!ec)
3232
{
33+
BOOST_LOG_TRIVIAL(info) << "calling select_async";
3334
connection->exec("select * from postgres_asio_sample;", [connection](int ec, boost::shared_ptr<PGresult> res)
3435
{
3536
if (ec)
3637
{
37-
std::cerr << "select failed " << ec << std::endl;
38+
BOOST_LOG_TRIVIAL(error) << "select failed, ec: " << ec;
3839
return;
3940
}
4041

41-
int nFields = PQnfields(res.get());
42-
for (int i = 0; i < nFields; i++)
43-
printf("%-15s", PQfname(res.get(), i));
42+
BOOST_LOG_TRIVIAL(info) << "select_async returned " << PQntuples(res.get()) << " rows";
4443

45-
printf("\n\n");
46-
printf("query returned %d rows", PQntuples(res.get()));
44+
//int nFields = PQnfields(res.get());
45+
//for (int i = 0; i < nFields; i++)
46+
// printf("%-15s", PQfname(res.get(), i));
47+
48+
//printf("\n\n");
49+
//printf("query returned %d rows", PQntuples(res.get()));
4750

4851
//for (int i = 0; i < PQntuples(res.get()); i++)
4952
//{
5053
// for (int j = 0; j < nFields; j++)
5154
// printf("%-15s", PQgetvalue(res.get(), i, j));
5255
// printf("\n");
5356
//}
54-
;
5557
});
5658
}
5759
});
5860

61+
BOOST_LOG_TRIVIAL(debug) << "work reset";
62+
bg_work.reset();
63+
BOOST_LOG_TRIVIAL(debug) << "bg join";
64+
bg.join();
5965

60-
61-
62-
while (true)
63-
{
64-
boost::this_thread::sleep(boost::posix_time::milliseconds(1000));
65-
}
66-
67-
work1.reset();
68-
work2.reset();
69-
//bg_ios.stop();
70-
//fg_ios.stop();
71-
bg.join();
72-
fg.join();
66+
fg_work.reset();
67+
BOOST_LOG_TRIVIAL(debug) << "fg join";
68+
fg.join();
69+
BOOST_LOG_TRIVIAL(debug) << "done";
7370
}

0 commit comments

Comments
 (0)