Skip to content

Commit 4bec1be

Browse files
committed
feat: support redirecting logs to a file or disabling logs
1 parent 4fa50ba commit 4bec1be

File tree

4 files changed

+70
-52
lines changed

4 files changed

+70
-52
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,15 @@ topic count: 2
7878
[0] "my-topic-2" with 1 partition
7979
[1] "my-topic-1" with 10 partitions
8080
```
81+
82+
## Logging
83+
84+
By default, rdkafka will generate logs to the standard output. `snctl-cpp` can redirect the logs to a file. For example, with the following configs in `sncloud.ini`:
85+
86+
```toml
87+
[log]
88+
enabled = true
89+
path = /tmp/rdkafka.log
90+
```
91+
92+
The logs will be appended to the `/tmp/rdkafka.log` file. If you don't want to generate any log from rdkafka, you can configure `enabled` with `false`.

common.h

Lines changed: 0 additions & 50 deletions
This file was deleted.

main.cc

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
#include "common.h"
16+
#include "SimpleIni.h"
1717
#include "create_topic.h"
1818
#include "delete_topic.h"
1919
#include "describe_topic.h"
2020
#include "list_topics.h"
2121
#include <argparse/argparse.hpp>
2222
#include <array>
23+
#include <cstring>
2324
#include <librdkafka/rdkafka.h>
2425
#include <memory>
2526
#include <stdexcept>
@@ -66,14 +67,62 @@ int main(int argc, char *argv[]) {
6667
throw std::runtime_error("Failed to " + action + ": " + errstr.data());
6768
};
6869

69-
auto rk_conf_map = load_rdkafka_configs(program);
70+
const auto config_file = program.get("--config");
71+
CSimpleIni ini;
72+
if (auto rc = ini.LoadFile(config_file.c_str()); rc < 0) {
73+
throw std::runtime_error("Error loading config file " + config_file);
74+
}
75+
auto get_value = [&ini](const auto &key, bool required) {
76+
auto value = ini.GetValue("kafka", key, "");
77+
if (strlen(value) == 0 && required) {
78+
throw std::runtime_error("Error: " + std::string(key) +
79+
" not found in kafka section");
80+
}
81+
return std::string(value);
82+
};
83+
84+
std::unordered_map<std::string, std::string> rk_conf_map{
85+
{"bootstrap.servers", get_value("bootstrap.servers", true)}};
86+
if (auto token = get_value("token", false); !token.empty()) {
87+
rk_conf_map["sasl.mechanism"] = "PLAIN";
88+
rk_conf_map["security.protocol"] = "SASL_SSL";
89+
rk_conf_map["sasl.username"] = "user";
90+
rk_conf_map["sasl.password"] = "token:" + token;
91+
}
92+
if (auto client_id = program.present("--client-id")) {
93+
rk_conf_map["client.id"] = client_id.value();
94+
}
7095
for (auto &&[key, value] : rk_conf_map) {
7196
if (rd_kafka_conf_set(rk_conf, key.c_str(), value.c_str(), errstr.data(),
7297
errstr.size()) != RD_KAFKA_CONF_OK) {
7398
fail("set " + key + " => " + value);
7499
}
75100
}
76101

102+
std::unique_ptr<FILE, decltype(&fclose)> file{nullptr, &fclose};
103+
if (auto log_enabled = ini.GetValue("log", "enabled", "false");
104+
std::string(log_enabled) == "false") {
105+
// Disable logging in rdkafka
106+
rd_kafka_conf_set_log_cb(
107+
rk_conf, +[](const rd_kafka_t *rk, int level, const char *fac,
108+
const char *buf) {});
109+
} else {
110+
if (auto log_file = ini.GetValue("log", "path", "");
111+
strlen(log_file) == 0) {
112+
rd_kafka_conf_set_opaque(rk_conf, stdout);
113+
} else {
114+
file.reset(fopen(log_file, "a"));
115+
std::cout << "Opened log file " << log_file << std::endl;
116+
rd_kafka_conf_set_opaque(rk_conf, file.get());
117+
rd_kafka_conf_set_log_cb(
118+
rk_conf, +[](const rd_kafka_t *rk, int level, const char *fac,
119+
const char *buf) {
120+
auto file = static_cast<FILE *>(rd_kafka_opaque(rk));
121+
fprintf(file, "[%d] %s: %s\n", level, fac, buf);
122+
});
123+
}
124+
}
125+
77126
auto rk =
78127
rd_kafka_new(RD_KAFKA_PRODUCER, rk_conf, errstr.data(), errstr.size());
79128
if (!rk) {

sncloud.ini

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@
1515
[kafka]
1616
bootstrap.servers = localhost:9092
1717
token =
18+
19+
[log]
20+
# Whether to enable logging for rdkafka.
21+
enabled = true
22+
# The file to store logs from rdkafka. If it's empty, the logs will be written to
23+
# the standard output.
24+
path = /tmp/rdkafka.log

0 commit comments

Comments
 (0)