Skip to content

Commit

Permalink
add a feature/flag for slightly more precise timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Dejonghe committed Feb 12, 2023
1 parent 172f05c commit 30d2457
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,9 @@ void pkcs11_logger_log(const char* message, ...)
// Logs separator line
void pkcs11_logger_log_separator(void)
{
char str_time[20];
pkcs11_logger_utils_get_current_time_str(str_time, sizeof(str_time));
unsigned long enable_usecs = (pkcs11_logger_globals.flags & PKCS11_LOGGER_FLAG_ENABLE_USECS) == PKCS11_LOGGER_FLAG_ENABLE_USECS;
char str_time[27];
pkcs11_logger_utils_get_current_time_str(str_time, sizeof(str_time), enable_usecs);
pkcs11_logger_log("****************************** %s ***", str_time);
}

Expand Down
4 changes: 3 additions & 1 deletion src/pkcs11-logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ PKCS11_LOGGER_GLOBALS;
#define PKCS11_LOGGER_FLAG_ENABLE_STDERR 0x00000020
// Flag that enables reopening of log file
#define PKCS11_LOGGER_FLAG_ENABLE_FCLOSE 0x00000040
// Flag that enabled more precise timestamping
#define PKCS11_LOGGER_FLAG_ENABLE_USECS 0x00000080

// Library name
#define PKCS11_LOGGER_NAME "PKCS11-LOGGER"
Expand Down Expand Up @@ -212,6 +214,6 @@ const char* pkcs11_logger_translate_ck_attribute(CK_ATTRIBUTE_TYPE type);

// utils.c - declaration of functions
int pkcs11_logger_utils_str_to_long(const char *str, unsigned long *val);
void pkcs11_logger_utils_get_current_time_str(char* buff, int buff_len);
void pkcs11_logger_utils_get_current_time_str(char* buff, int buff_len, unsigned long enable_usecs);
unsigned long pkcs11_logger_utils_get_thread_id(void);
int pkcs11_logger_utils_get_process_id(void);
31 changes: 25 additions & 6 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ int pkcs11_logger_utils_str_to_long(const char *str, unsigned long *val)


// Gets current system time as string
void pkcs11_logger_utils_get_current_time_str(char* buff, int buff_len)
void pkcs11_logger_utils_get_current_time_str(char* buff, int buff_len, unsigned long enable_usecs)
{
#ifdef _WIN32

Expand All @@ -54,8 +54,17 @@ void pkcs11_logger_utils_get_current_time_str(char* buff, int buff_len)
memset(buff, 0, buff_len * sizeof(char));

GetLocalTime(&systemtime);
GetDateFormatA(LOCALE_SYSTEM_DEFAULT, 0, &systemtime, "yyyy-MM-dd ", buff, buff_len);
GetTimeFormatA(LOCALE_SYSTEM_DEFAULT, 0, &systemtime, "HH:mm:ss", buff + 11, buff_len - 11);
if (enable_usecs) {
char locBuf [buff_len];
memset(locBuf, 0, buff_len * sizeof(char))
GetDateFormatA(LOCALE_SYSTEM_DEFAULT, 0, &systemtime, "yyyy-MM-dd ", locBuf, buff_len);
GetTimeFormatA(LOCALE_SYSTEM_DEFAULT, 0, &systemtime, "HH:mm:ss", locBuf + 11, buff_len - 11);
snprintf(buff, buff_len-1, "%s.03%d", locBuf, systemtime.wMilliseconds);
}
else {
GetDateFormatA(LOCALE_SYSTEM_DEFAULT, 0, &systemtime, "yyyy-MM-dd ", buff, buff_len);
GetTimeFormatA(LOCALE_SYSTEM_DEFAULT, 0, &systemtime, "HH:mm:ss", buff + 11, buff_len - 11);
}

#else

Expand All @@ -64,9 +73,19 @@ void pkcs11_logger_utils_get_current_time_str(char* buff, int buff_len)

memset(buff, 0, buff_len * sizeof(char));

if (gettimeofday(&tv, NULL) == 0)
if (localtime_r(&tv.tv_sec, &tm) != NULL)
strftime(buff, buff_len, "%Y-%m-%d %H:%M:%S", &tm);
if (gettimeofday(&tv, NULL) == 0) {
if (localtime_r(&tv.tv_sec, &tm) != NULL) {
if (enable_usecs) {
char locBuf [buff_len];
memset(locBuf, 0, buff_len * sizeof(char));
strftime(locBuf, buff_len, "%Y-%m-%d %H:%M:%S", &tm);
snprintf(buff, buff_len-1, "%s.%06u", locBuf, tv.tv_usec);
}
else {
strftime(buff, buff_len, "%Y-%m-%d %H:%M:%S", &tm);
}
}
}

#endif
}
Expand Down

0 comments on commit 30d2457

Please sign in to comment.