Skip to content

Commit f3fc502

Browse files
authored
The C API supports setting the log level. (#158)
### Motivation #137 ### Modifications - Add interface `pulsar_client_configuration_set_logger_and_level` to support set the log level.
1 parent d04ba21 commit f3fc502

4 files changed

Lines changed: 107 additions & 6 deletions

File tree

examples/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ set(SAMPLE_KEY_VALUE_SCHEMA_PRODUCER
6868
SampleKeyValueSchemaProducer.cc
6969
)
7070

71+
set(SAMPLE_CUSTOM_LOGGER_CAPI
72+
SampleCustomLoggerCApi.c
73+
)
74+
7175
add_executable(SampleAsyncProducer ${SAMPLE_ASYNC_PRODUCER_SOURCES})
7276
add_executable(SampleConsumer ${SAMPLE_CONSUMER_SOURCES})
7377
add_executable(SampleConsumerListener ${SAMPLE_CONSUMER_LISTENER_SOURCES})
@@ -80,6 +84,7 @@ add_executable(SampleConsumerListenerCApi ${SAMPLE_CONSUMER_LISTENER
8084
add_executable(SampleReaderCApi ${SAMPLE_READER_C_SOURCES})
8185
add_executable(SampleKeyValueSchemaConsumer ${SAMPLE_KEY_VALUE_SCHEMA_CONSUMER})
8286
add_executable(SampleKeyValueSchemaProducer ${SAMPLE_KEY_VALUE_SCHEMA_PRODUCER})
87+
add_executable(SampleCustomLoggerCApi ${SAMPLE_CUSTOM_LOGGER_CAPI})
8388

8489
target_link_libraries(SampleAsyncProducer ${CLIENT_LIBS} pulsarShared)
8590
target_link_libraries(SampleConsumer ${CLIENT_LIBS} pulsarShared)
@@ -93,3 +98,4 @@ target_link_libraries(SampleConsumerListenerCApi ${CLIENT_LIBS} pulsarShar
9398
target_link_libraries(SampleReaderCApi ${CLIENT_LIBS} pulsarShared)
9499
target_link_libraries(SampleKeyValueSchemaConsumer ${CLIENT_LIBS} pulsarShared)
95100
target_link_libraries(SampleKeyValueSchemaProducer ${CLIENT_LIBS} pulsarShared)
101+
target_link_libraries(SampleCustomLoggerCApi ${CLIENT_LIBS} pulsarShared)

examples/SampleCustomLoggerCApi.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include <pulsar/c/client.h>
21+
#include <stdio.h>
22+
#include <stdlib.h>
23+
#include <string.h>
24+
#include <time.h>
25+
26+
char *current_time() {
27+
char *time_str = malloc(128);
28+
struct tm *p;
29+
time_t now = time(0);
30+
p = gmtime(&now);
31+
strftime(time_str, 128, "%Y-%m-%d %H:%M:%S", p);
32+
return time_str;
33+
}
34+
35+
void custom_logger(pulsar_logger_level_t level, const char *file, int line, const char *message, void *ctx) {
36+
char *time_str = current_time();
37+
printf("[%s] [%u] [%s] [%d] [%s] \n", time_str, level, file, line, message);
38+
free(time_str);
39+
}
40+
41+
int main() {
42+
pulsar_client_configuration_t *conf = pulsar_client_configuration_create();
43+
44+
pulsar_client_configuration_set_logger_and_level(conf, custom_logger, pulsar_DEBUG, NULL);
45+
pulsar_client_configuration_set_memory_limit(conf, 64 * 1024 * 1024);
46+
pulsar_client_t *client = pulsar_client_create("pulsar://localhost:6650", conf);
47+
48+
pulsar_producer_configuration_t *producer_conf = pulsar_producer_configuration_create();
49+
pulsar_producer_configuration_set_batching_enabled(producer_conf, 1);
50+
pulsar_producer_t *producer;
51+
52+
pulsar_result err = pulsar_client_create_producer(client, "my-topic", producer_conf, &producer);
53+
if (err != pulsar_result_Ok) {
54+
printf("Failed to create producer: %s\n", pulsar_result_str(err));
55+
return 1;
56+
}
57+
58+
for (int i = 0; i < 10; i++) {
59+
const char *data = "my-content";
60+
pulsar_message_t *message = pulsar_message_create();
61+
pulsar_message_set_content(message, data, strlen(data));
62+
63+
err = pulsar_producer_send(producer, message);
64+
if (err == pulsar_result_Ok) {
65+
printf("Sent message %d\n", i);
66+
} else {
67+
printf("Failed to publish message: %s\n", pulsar_result_str(err));
68+
return 1;
69+
}
70+
71+
pulsar_message_free(message);
72+
}
73+
74+
// Cleanup
75+
pulsar_producer_close(producer);
76+
pulsar_producer_free(producer);
77+
pulsar_producer_configuration_free(producer_conf);
78+
79+
pulsar_client_close(client);
80+
pulsar_client_free(client);
81+
pulsar_client_configuration_free(conf);
82+
}

include/pulsar/c/client_configuration.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ PULSAR_PUBLIC int pulsar_client_configuration_get_concurrent_lookup_request(
134134
PULSAR_PUBLIC void pulsar_client_configuration_set_logger(pulsar_client_configuration_t *conf,
135135
pulsar_logger logger, void *ctx);
136136

137+
PULSAR_PUBLIC void pulsar_client_configuration_set_logger_and_level(pulsar_client_configuration_t *conf,
138+
pulsar_logger logger,
139+
pulsar_logger_level_t level, void *ctx);
140+
137141
PULSAR_PUBLIC void pulsar_client_configuration_set_use_tls(pulsar_client_configuration_t *conf, int useTls);
138142

139143
PULSAR_PUBLIC int pulsar_client_configuration_is_use_tls(pulsar_client_configuration_t *conf);

lib/c/c_ClientConfiguration.cc

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,14 @@ int pulsar_client_configuration_get_concurrent_lookup_request(pulsar_client_conf
7272
class PulsarCLogger : public pulsar::Logger {
7373
std::string file_;
7474
pulsar_logger logger_;
75+
pulsar_logger_level_t level_;
7576
void *ctx_;
7677

7778
public:
78-
PulsarCLogger(const std::string &file, pulsar_logger logger, void *ctx)
79-
: file_(file), logger_(logger), ctx_(ctx) {}
79+
PulsarCLogger(const std::string &file, pulsar_logger logger, pulsar_logger_level_t level, void *ctx)
80+
: file_(file), logger_(logger), level_(level), ctx_(ctx) {}
8081

81-
bool isEnabled(Level level) { return level >= pulsar::Logger::LEVEL_INFO; }
82+
bool isEnabled(Level level) { return (pulsar_logger_level_t)level >= level_; }
8283

8384
void log(Level level, int line, const std::string &message) {
8485
logger_((pulsar_logger_level_t)level, file_.c_str(), line, message.c_str(), ctx_);
@@ -87,19 +88,27 @@ class PulsarCLogger : public pulsar::Logger {
8788

8889
class PulsarCLoggerFactory : public pulsar::LoggerFactory {
8990
pulsar_logger logger_;
91+
pulsar_logger_level_t level_;
9092
void *ctx_;
9193

9294
public:
93-
PulsarCLoggerFactory(pulsar_logger logger, void *ctx) : logger_(logger), ctx_(ctx) {}
95+
PulsarCLoggerFactory(pulsar_logger logger, pulsar_logger_level_t level, void *ctx)
96+
: logger_(logger), level_(level), ctx_(ctx) {}
9497

9598
pulsar::Logger *getLogger(const std::string &fileName) {
96-
return new PulsarCLogger(fileName, logger_, ctx_);
99+
return new PulsarCLogger(fileName, logger_, level_, ctx_);
97100
}
98101
};
99102

100103
void pulsar_client_configuration_set_logger(pulsar_client_configuration_t *conf, pulsar_logger logger,
101104
void *ctx) {
102-
conf->conf.setLogger(new PulsarCLoggerFactory(logger, ctx));
105+
conf->conf.setLogger(new PulsarCLoggerFactory(logger, pulsar_logger_level_t::pulsar_INFO, ctx));
106+
}
107+
108+
void pulsar_client_configuration_set_logger_and_level(pulsar_client_configuration_t *conf,
109+
pulsar_logger logger, pulsar_logger_level_t level,
110+
void *ctx) {
111+
conf->conf.setLogger(new PulsarCLoggerFactory(logger, level, ctx));
103112
}
104113

105114
void pulsar_client_configuration_set_use_tls(pulsar_client_configuration_t *conf, int useTls) {

0 commit comments

Comments
 (0)