ConfD User Community

Confd_trace log file

Can we enable the timestamp on the confd_trace log file ?

The example below output the libconfd log to both stderr and a ./libconfd.log file where the output to the file has a timestamp with millisecond precision. See the confd_lib_lib(3) man page section “SYSLOG AND DEBUG” for more details.

#include <time.h>
#include <sys/time.h>
#include <confd_lib.h> 

static FILE *estream;

static void libconfd_logger(int syslogprio, const char *fmt, va_list ap) { 
  char buf[BUFSIZ];
  struct timeval curTime;
  gettimeofday(&curTime, NULL);
  int milli = curTime.tv_usec / 1000;

  char tbuf[80];
  strftime(tbuf, 80, "%Y-%m-%d %H:%M:%S", localtime(&curTime.tv_sec));

  char currentTime[84] = "";
  sprintf(currentTime, "%s:%03d", tbuf, milli);
  sprintf(buf, "%s ", currentTime);
  strcat(buf, fmt);
  vfprintf(estream, buf, ap);
}

int main(int argc, char *argv[])
{
...

  if ((estream = fopen("./libconfd.log", "w")) == NULL) {
    fprintf(stderr, "couldn't open logfile %s\n", log_fname);
    exit(1);
  }
  confd_user_log_hook = libconfd_logger;
  confd_init("my-app-name", stderr, debuglevel); 
...
}

Example output with timestamp:

$ head -n 5 libconfd.log 
2020-07-01 15:52:18:392 TRACE Connected (maapi) to ConfD
2020-07-01 15:52:18:392 TRACE MAAPI_LOAD_ALL_NS
2020-07-01 15:52:18:407 TRACE MAAPI_LOAD_MNS_MAPS
2020-07-01 15:52:18:408 TRACE MAAPI_LOAD_HASH_DB
2020-07-01 15:52:18:411 TRACE Connected (dp) to ConfD

Thanks cohult for the suggestion. Will check in this approach.

BTW, I incorporated the example above into the data provider performance demo when I updated it a couple of days ago.