Skip to content

Commit 65385e7

Browse files
committed
logger: restore Logger and logging callback on integ. test teardown
When integration test gets constructed, it can set global `cb_` and `data_` to temporaries. After the test finishes and temporaries are destroyed, the next tests ctor may try to refer to them (before it sets the globals by itself). This happened in the ctor of `Random`, which logged a message in case of failure. Sometimes this manifested as segfault, sometimes as a deadlock. This fix restores the old logger values on test teardown - most likely to stderr. Fixes #13
1 parent a536865 commit 65385e7

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

include/cassandra.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11298,6 +11298,15 @@ CASS_EXPORT void
1129811298
cass_log_set_callback(CassLogCallback callback,
1129911299
void* data);
1130011300

11301+
/**
11302+
* Analogous getter - useful for restoring logger to previous values.
11303+
*
11304+
* @param[out] callback_out Current logging callback
11305+
* @param[out] data_out Current Logger instance
11306+
*/
11307+
CASS_EXPORT void
11308+
cass_log_get_callback_and_data(CassLogCallback& callback_out, void** data_out);
11309+
1130111310
/**
1130211311
* Sets the log queue size.
1130311312
*

src/logger.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ void cass_log_set_callback(CassLogCallback callback, void* data) {
3030
Logger::set_callback(callback, data);
3131
}
3232

33+
void cass_log_get_callback_and_data(CassLogCallback& callback_out, void** data_out) {
34+
Logger::get_callback_and_data(callback_out, data_out);
35+
}
36+
3337
void cass_log_set_queue_size(size_t queue_size) {
3438
// Deprecated
3539
}
@@ -66,3 +70,8 @@ void Logger::set_callback(CassLogCallback cb, void* data) {
6670
cb_ = cb == NULL ? noop_log_callback : cb;
6771
data_ = data;
6872
}
73+
74+
void Logger::get_callback_and_data(CassLogCallback& cb_out, void** data_out) {
75+
cb_out = cb_;
76+
*data_out = data_;
77+
}

src/logger.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Logger {
3131
public:
3232
static void set_log_level(CassLogLevel level);
3333
static void set_callback(CassLogCallback cb, void* data);
34+
static void get_callback_and_data(CassLogCallback& cb_out, void** data_out);
3435

3536
#if defined(__GNUC__) || defined(__clang__)
3637
#define ATTR_FORMAT(string, first) __attribute__((__format__(__printf__, string, first)))

tests/src/integration/logger.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ Logger::~Logger() {
4444
if (output_.is_open()) {
4545
output_.close();
4646
}
47+
if (restore_old_logger_) {
48+
// `initialize` has beed called, so let's restore global logger
49+
cass_log_set_callback(old_log_callback_, old_data_);
50+
}
4751
}
4852

4953
void Logger::initialize(const std::string& test_case, const std::string& test_name) {
@@ -72,6 +76,10 @@ void Logger::initialize(const std::string& test_case, const std::string& test_na
7276

7377
// Set the maximum driver log level to capture all logs messages
7478
cass_log_set_level(CASS_LOG_TRACE);
79+
80+
// Remember the old logger before setting it (to be restored in dtor)
81+
cass_log_get_callback_and_data(old_log_callback_, &old_data_);
82+
restore_old_logger_ = true;
7583
cass_log_set_callback(Logger::log, this);
7684
}
7785

tests/src/integration/logger.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ class Logger {
9797
* Number of log messages that match the search criteria
9898
*/
9999
size_t count_;
100+
101+
/** We need these to restore the globals on teardown. */
102+
bool restore_old_logger_ = false;
103+
CassLogCallback old_log_callback_;
104+
void* old_data_;
100105

101106
/**
102107
* Log the message from the driver (callback)

0 commit comments

Comments
 (0)