Skip to content

Commit d55edb2

Browse files
author
accelerated
committed
Log error in case consumer destructor throws
1 parent ee0c082 commit d55edb2

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

Diff for: include/cppkafka/exceptions.h

+12
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@
3838

3939
namespace cppkafka {
4040

41+
// Based on syslog.h levels
42+
enum LogLevel {
43+
LOG_EMERG = 0, /* system is unusable */
44+
LOG_ALERT = 1, /* action must be taken immediately */
45+
LOG_CRIT = 2, /* critical conditions */
46+
LOG_ERR = 3, /* error conditions */
47+
LOG_WARNING = 4, /* warning conditions */
48+
LOG_NOTICE = 5, /* normal but significant condition */
49+
LOG_INFO = 6, /* informational */
50+
LOG_DEBUG = 7 /* debug-level messages */
51+
};
52+
4153
/**
4254
* Base class for all cppkafka exceptions
4355
*/

Diff for: src/consumer.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2727
*
2828
*/
29-
29+
#include <sstream>
3030
#include "consumer.h"
3131
#include "exceptions.h"
3232
#include "configuration.h"
@@ -73,9 +73,17 @@ Consumer::~Consumer() {
7373
rebalance_error_callback_ = nullptr;
7474
close();
7575
}
76-
catch (const Exception&) {
77-
// If close throws just silently ignore until there's some
78-
// logging facility (if any)
76+
catch (const Exception& ex) {
77+
constexpr const char* library_name = "cppkafka";
78+
std::ostringstream error_msg;
79+
error_msg << "Failed to close consumer [" << get_name() << "]: " << ex.what();
80+
const auto& callback = get_configuration().get_log_callback();
81+
if (callback) {
82+
callback(*this, LogLevel::LOG_ERR, library_name, error_msg.str());
83+
}
84+
else {
85+
rd_kafka_log_print(get_handle(), LogLevel::LOG_ERR, library_name, error_msg.str().c_str());
86+
}
7987
}
8088
}
8189

0 commit comments

Comments
 (0)