Skip to content

Commit 0f996a7

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

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

Diff for: include/cppkafka/cppkafka.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <cppkafka/exceptions.h>
4141
#include <cppkafka/group_information.h>
4242
#include <cppkafka/kafka_handle_base.h>
43+
#include <cppkafka/logging.h>
4344
#include <cppkafka/macros.h>
4445
#include <cppkafka/message.h>
4546
#include <cppkafka/message_builder.h>

Diff for: include/cppkafka/logging.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) 2017, Matias Fontanini
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are
7+
* met:
8+
*
9+
* * Redistributions of source code must retain the above copyright
10+
* notice, this list of conditions and the following disclaimer.
11+
* * Redistributions in binary form must reproduce the above
12+
* copyright notice, this list of conditions and the following disclaimer
13+
* in the documentation and/or other materials provided with the
14+
* distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27+
*
28+
*/
29+
30+
#ifndef CPPKAFKA_LOGGING_H
31+
#define CPPKAFKA_LOGGING_H
32+
33+
namespace cppkafka {
34+
35+
// Based on syslog.h levels
36+
enum class LogLevel : int {
37+
LOG_EMERG = 0, /* system is unusable */
38+
LOG_ALERT = 1, /* action must be taken immediately */
39+
LOG_CRIT = 2, /* critical conditions */
40+
LOG_ERR = 3, /* error conditions */
41+
LOG_WARNING = 4, /* warning conditions */
42+
LOG_NOTICE = 5, /* normal but significant condition */
43+
LOG_INFO = 6, /* informational */
44+
LOG_DEBUG = 7 /* debug-level messages */
45+
};
46+
47+
} //cppkafka
48+
49+
#endif //CPPKAFKA_LOGGING_H

Diff for: src/consumer.cpp

+14-5
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@
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"
32+
#include "logging.h"
3233
#include "configuration.h"
3334
#include "topic_partition_list.h"
3435

3536
using std::vector;
3637
using std::string;
3738
using std::move;
3839
using std::make_tuple;
39-
40+
using std::ostringstream;
4041
using std::chrono::milliseconds;
4142

4243
namespace cppkafka {
@@ -73,9 +74,17 @@ Consumer::~Consumer() {
7374
rebalance_error_callback_ = nullptr;
7475
close();
7576
}
76-
catch (const Exception&) {
77-
// If close throws just silently ignore until there's some
78-
// logging facility (if any)
77+
catch (const Exception& ex) {
78+
constexpr const char* library_name = "cppkafka";
79+
ostringstream error_msg;
80+
error_msg << "Failed to close consumer [" << get_name() << "]: " << ex.what();
81+
const auto& callback = get_configuration().get_log_callback();
82+
if (callback) {
83+
callback(*this, static_cast<int>(LogLevel::LOG_ERR), library_name, error_msg.str());
84+
}
85+
else {
86+
rd_kafka_log_print(get_handle(), static_cast<int>(LogLevel::LOG_ERR), library_name, error_msg.str().c_str());
87+
}
7988
}
8089
}
8190

0 commit comments

Comments
 (0)