updated example

This commit is contained in:
Hans Petter Dalsklev 2023-03-22 16:17:33 +01:00
parent 423737a870
commit 7303899c5d

View File

@ -5,20 +5,22 @@ extern const char *level_strings[];
void callback1(log_Event *ev)
{
char logline[128];
snprintf(logline, 128, "%-5s %s:%d: ",
level_strings[ev->level], ev->file, ev->line);
vsnprintf(logline, 128, ev->fmt, ev->ap);
printf("%s: %s\n", __func__, logline);
char buf[16];
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
fprintf(stdout, "callback 1 %s %-5s %s:%d: ", buf, level_strings[ev->level], ev->file, ev->line);
vfprintf(stdout, ev->fmt, ev->ap);
fprintf(stdout, "\n");
fflush(stdout);
}
void callback2(log_Event *ev)
{
char logline[128];
snprintf(logline, 128, "%-5s %s:%d: ",
level_strings[ev->level], ev->file, ev->line);
vsnprintf(logline, 128, ev->fmt, ev->ap);
printf("%s: %s\n", __func__, logline);
char buf[16];
buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
fprintf(stdout, "callback 2 %s %-5s %s:%d: ", buf, level_strings[ev->level], ev->file, ev->line);
vfprintf(stdout, ev->fmt, ev->ap);
fprintf(stdout, "\n");
fflush(stdout);
}
int main()
@ -29,7 +31,7 @@ int main()
log_warn("hello");
log_error("hello");
log_fatal("hello");
log_debug("============== update loglevel to FATAL =============");
printf("============== update loglevel to FATAL =============\n");
log_set_level(LOG_FATAL);
log_trace("hello");
log_debug("hello");
@ -37,7 +39,7 @@ int main()
log_warn("hello");
log_error("hello");
log_fatal("hello");
log_debug("===================== init log =====================");
printf("===================== add callback =================\n");
log_add_callback(callback1, "progname", LOG_TRACE);
log_add_callback(callback2, "progname", LOG_TRACE);
log_trace("hello");
@ -46,8 +48,14 @@ int main()
log_warn("hello");
log_error("hello");
log_fatal("hello");
log_debug("============ update loglevels of callbacks ========");
printf("============ update loglevels of callbacks ========\n");
log_set_level_callback(callback1, LOG_FATAL);
log_set_level_callback(callback2, LOG_WARN);
log_trace("hello");
log_debug("hello");
log_info("hello");
log_warn("hello");
log_error("hello");
log_fatal("hello");
return 0;
}