Skip to content

Commit

Permalink
Remove newline between date and msg in rdkafka log
Browse files Browse the repository at this point in the history
  • Loading branch information
BewareMyPower committed Jun 29, 2020
1 parent 67f29e6 commit 6cfebe9
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions cxx/src/qbus_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,24 @@ std::string QbusHelper::FormatTopicPartitionList(

void QbusHelper::RdKafkaLogger(const rd_kafka_t* rk, int level, const char* fac,
const char* buf) {
time_t timep;
time(&timep);

std::ofstream out;
out.open(kRdkafkaLog, std::ios::out | std::ios::app);
out << asctime(gmtime(&timep)) << " | RD_KAFKA_LOG"
<< " | level: " << level << " | fac: " << (NULL != fac ? fac : "")
<< " | msg: " << (NULL != buf ? buf : "") << std::endl;
out.close();
struct timeval tv;
gettimeofday(&tv, NULL);

char msgbuf[2048];
size_t n = strftime(msgbuf, sizeof(msgbuf), "%F %T", localtime(&tv.tv_sec));
snprintf(msgbuf + n, sizeof(msgbuf) - n,
"%.03ld level: %d | fac: %s | msg: %s",
static_cast<long>(tv.tv_usec) / 1000, level, (fac ? fac : ""),
(buf ? buf : ""));

// TODO: Use a singleton to write log, avoid frequent fopen() and fclose()
FILE* fp = fopen("./rdkafka.log", "a");
if (!fp) {
// TODO: print error msg
return;
}
fprintf(fp, "%s\n", msgbuf);
fclose(fp);
}

void QbusHelper::SetClientId(rd_kafka_conf_t* rd_kafka_conf,
Expand Down

0 comments on commit 6cfebe9

Please sign in to comment.