43#define NB_TEST_EVENTS 10
60 "Syntax is: ex_kafka -v -c config_file [-s event or -f eventfile] -o event_log_file -V LOGLEVEL\n"
61 " -v version: print version and exit\n"
62 " -c config_file: [required] Kproducer config file\n"
63 " -s : string of the event to send\n"
64 " -f : file containing the event to send\n"
65 " -C : start a consumer (default output received in terminal)"
66 " -P : start a producer and produce event"
67 " -o : optionnal, set a log file instead of default (stderr/stdout)\n"
68 " -p : optionnal, set a log prefix\n"
69 " -V verbosity: specify a log level for console output\n"
70 " Supported: LOG_ EMERG,ALERT,CRIT,ERR,WARNING,NOTICE,INFO,DEBUG\n");
79int main(
int argc,
char* argv[]) {
84 rd_kafka_headers_t* headers = NULL;
87 while ((
getoptret = getopt(argc, argv,
"vhCPH:c:s:f:V:o:p:")) != -1) {
90 fprintf(stderr,
" Version compiled on %s at %s\n", __DATE__, __TIME__);
100 fprintf(stderr,
"-C and -P can not be used at the ame time!");
108 fprintf(stderr,
"-C and -P can not be used at the ame time!");
113 char *name = NULL, *val = NULL;
114 ssize_t name_sz = -1;
117 val = strchr(name,
'=');
119 name_sz = val - name;
124 headers = rd_kafka_headers_new(16);
126 int err = rd_kafka_header_add(headers, name, name_sz, val, -1);
129 "%% Failed to add header %s: %s\n",
130 name, rd_kafka_err2str(err));
151 if (!strncmp(
"LOG_NULL", optarg, 8)) {
153 }
else if (!strncmp(
"LOG_NOTICE", optarg, 10)) {
155 }
else if (!strncmp(
"LOG_INFO", optarg, 8)) {
157 }
else if (!strncmp(
"LOG_ERR", optarg, 7)) {
159 }
else if (!strncmp(
"LOG_DEBUG", optarg, 9)) {
162 fprintf(stderr,
"%s n'est pas un niveau de log valide.\n", optarg);
167 if (optopt ==
'c' || optopt ==
's' || optopt ==
'f') {
168 fprintf(stderr,
"Option -%c need a parameter\n", optopt);
219 size_t nb_queued = 0;
220 size_t nb_waiting = 0;
234 if (headers && event) {
235 event->rd_kafka_headers = rd_kafka_headers_copy(headers);
237 rd_kafka_headers_destroy(headers);
241 n_log(
LOG_ERR,
"n_kafka_produce returned an error for event %p", event);
243 n_log(
LOG_INFO,
"n_kafka_produce returned OK for event %p", event);
250 n_log(
LOG_DEBUG,
"polling kafka handle, status: %d, %d in queue, %d waiting for ack, %d on error", poll_status, nb_queued, nb_waiting, nb_error);
255 if (nb_queued == 0 && nb_waiting == 0 && nb_error == 0)
261 n_log(
LOG_INFO,
"received event schema id %d string:\n%s", event->schema_id,
_str(event->event_string->data + 4));
263 n_log(
LOG_INFO,
"received event string:\n%s", event->event_string->data);
274 }
while (
run && poll_status > 0);
276 n_log(
LOG_INFO,
"kafka_handle: %d queued, %d waiting ack, %d on error", nb_queued, nb_waiting, nb_error);
277 if (nb_error > 0 || nb_waiting > 0) {
278 n_log(
LOG_ERR,
"kafka_handle: %d events are still waiting for ack, and %d are on error !", nb_waiting, nb_error);
285 if (unprocessed_event) {
static void stop(int sig)
#define FALL_THROUGH
set windows if true
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
#define __n_assert(__ptr, __ret)
macro to assert things
#define _str(__PTR)
define true
#define _nstr(__PTR)
N_STR or "NULL" string for logging purposes.
#define list_foreach(__ITEM_, __LIST_)
ForEach macro helper, safe for node removal during iteration.
FILE * get_log_file(void)
return the current log_file
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
#define LOG_DEBUG
debug-level messages
#define LOG_ERR
error conditions
int set_log_file(char *file)
Set the logging to a file instead of stderr.
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
#define LOG_NOTICE
normal but significant condition
#define LOG_NULL
no log output
#define LOG_INFO
informational
int schema_id
kafka schema_id
LIST * received_events
list of received N_KAFKA_EVENT
N_STR * event_string
string containing the topic id + payload
int schema_id
kafka schema id in network order
int n_kafka_dump_unprocessed(N_KAFKA *kafka, char *directory)
dump unprocessed/unset events
N_KAFKA_EVENT * n_kafka_get_event(N_KAFKA *kafka)
get a received event from the N_KAFKA kafka handle
N_KAFKA * n_kafka_load_config(char *config_file, int mode)
load a kafka configuration from a file
int n_kafka_get_status(N_KAFKA *kafka, size_t *nb_queued, size_t *nb_waiting, size_t *nb_error)
return the queues status
int n_kafka_start_polling_thread(N_KAFKA *kafka)
start the polling thread of a kafka handle
N_KAFKA_EVENT * n_kafka_new_event_from_char(const char *string, size_t written, int schema_id)
make a new event from a char *string
int n_kafka_event_destroy(N_KAFKA_EVENT **event)
destroy a kafka event and set it's pointer to NULL
int n_kafka_produce(N_KAFKA *kafka, N_KAFKA_EVENT *event)
put an event in the events_to_send list
N_KAFKA_EVENT * n_kafka_new_event_from_file(char *filename, int schema_id)
make a new event from a N_STR *string
void n_kafka_delete(N_KAFKA *kafka)
delete a N_KAFKA handle
structure of a KAFKA consumer or producer handle
structure of a KAFKA message
#define local_strdup(__src_)
Do tar(1) matching rules, which ignore a trailing slash?
A box including a string and his lenght.
Kafka generic produce and consume event header.