Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a feature/flag for slightly more precise timestamps #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
35 changes: 29 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 [27];
memset(locBuf, 0, buff_len * sizeof(char));
GetDateFormatA(LOCALE_SYSTEM_DEFAULT, 0, &systemtime, "yyyy-MM-dd ", locBuf, 27);
GetTimeFormatA(LOCALE_SYSTEM_DEFAULT, 0, &systemtime, "HH:mm:ss", locBuf + 11, 27 - 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,23 @@ 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);
#ifdef __APPLE__
snprintf(buff, buff_len-1, "%s.%06d", locBuf, tv.tv_usec);
#else
snprintf(buff, buff_len-1, "%s.%06ld", locBuf, tv.tv_usec);
#endif
}
else {
strftime(buff, buff_len, "%Y-%m-%d %H:%M:%S", &tm);
}
}
}

#endif
}
Expand Down