/* * * Copyright 2023 Oleg Borodin * */ #include #include #include #include #include #include #include #include #include #define MAX_TS_LEN 256 void log_error(const char* format, ...) { time_t now = time(NULL); if (now < 0) { return; } struct tm* ptm = localtime(&now); if (ptm == NULL) { return; } struct timespec tv; char timestamp[MAX_TS_LEN]; clock_gettime(CLOCK_REALTIME_PRECISE, &tv); memset(timestamp, 0, MAX_TS_LEN); sprintf(timestamp, "%04d-%02d-%02dT%02d:%02d:%02d.%ld+%s", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, tv.tv_nsec, ptm->tm_zone); va_list args; fprintf(stderr, "%s error: ", timestamp); va_start(args, format); vfprintf(stderr, format, args); va_end(args); fprintf(stderr, "\n"); } void log_debug(const char* format, ...) { time_t now = time(NULL); if (now < 0) { return; } struct tm* ptm = localtime(&now); if (ptm == NULL) { return; } struct timespec tv; char timestamp[MAX_TS_LEN]; clock_gettime(CLOCK_REALTIME_PRECISE, &tv); memset(timestamp, 0, MAX_TS_LEN); sprintf(timestamp, "%04d-%02d-%02dT%02d:%02d:%02d.%ld+%s", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, tv.tv_nsec, ptm->tm_zone); va_list args; fprintf(stderr, "%s debug: ", timestamp); va_start(args, format); vfprintf(stderr, format, args); va_end(args); fprintf(stderr, "\n"); }