--- old/src/jdk.accessibility/windows/native/common/AccessBridgeDebug.cpp 2018-12-07 20:03:36.575385400 +0530 +++ new/src/jdk.accessibility/windows/native/common/AccessBridgeDebug.cpp 2018-12-07 20:03:32.417334400 +0530 @@ -31,19 +31,56 @@ #include #include #include +#include +#include +#include #ifdef __cplusplus extern "C" { #endif +static FILE* logFP = nullptr; + +void initializeFileLogger(char * suffix) { + const char* var = "JAVA_ACCESSBRIDGE_LOGFILE"; + const char* envfilePath = getenv(var); + if (envfilePath != nullptr) { + std::string filePath = std::string(envfilePath); + if (suffix != nullptr) { + auto indx = filePath.find_last_of("."); + filePath = indx != std::string::npos ? + filePath.substr(0, indx) + suffix + filePath.substr(indx) : + filePath + suffix + ".log"; + } + logFP = fopen(filePath.c_str(), "w"); + if (logFP == nullptr) { + PrintDebugString("couldnot open file %s", filePath); + } + } +} + +void finalizeFileLogger() { + if (logFP) { + fclose(logFP); + logFP = nullptr; + } +} + +auto getTimeStamp() -> long long { + using namespace std::chrono; + auto timeNow = duration_cast(steady_clock::now().time_since_epoch()); + + return timeNow.count(); +} + /** * print a GetLastError message */ char *printError(char *msg) { - LPVOID lpMsgBuf = NULL; - static char retbuf[256]; + LPVOID lpMsgBuf = nullptr; + static char retbuf[256] = {0}; - if (msg != NULL) { + if (msg != nullptr) { strncpy((char *)retbuf, msg, sizeof(retbuf)); // if msg text is >= 256 ensure buffer is null terminated retbuf[255] = '\0'; @@ -52,18 +89,18 @@ FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, + nullptr, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpMsgBuf, 0, - NULL )) + nullptr)) { PrintDebugString(" %s: FormatMessage failed", msg); } else { PrintDebugString(" %s: %s", msg, (char *)lpMsgBuf); } - if (lpMsgBuf != NULL) { + if (lpMsgBuf != nullptr) { strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1); strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1); LocalFree(lpMsgBuf); @@ -77,7 +114,7 @@ */ void PrintDebugString(char *msg, ...) { #ifdef DEBUGGING_ON - char buf[1024]; + char buf[1024] = {0}; va_list argprt; va_start(argprt, msg); // set up argptr @@ -90,6 +127,14 @@ printf("\r\n"); #endif #endif + if (logFP) { + fprintf(logFP, "[%lldu] ", getTimeStamp()); + va_list args; + va_start(args, msg); + vfprintf(logFP, msg, args); + va_end(args); + fprintf(logFP, "\r\n"); + } } /** @@ -97,7 +142,7 @@ */ void PrintJavaDebugString2(char *msg, ...) { #ifdef JAVA_DEBUGGING_ON - char buf[1024]; + char buf[1024] = {0}; va_list argprt; va_start(argprt, msg); // set up argptr @@ -110,13 +155,21 @@ printf("\r\n"); #endif #endif + if (logFP) { + fprintf(logFP, "[%llu] ", getTimeStamp()); + va_list args; + va_start(args, msg); + vfprintf(logFP, msg, args); + va_end(args); + fprintf(logFP, "\r\n"); + } } /** * Wide version of the method to send debugging info to the appropriate place */ void wPrintDebugString(wchar_t *msg, ...) { #ifdef DEBUGGING_ON - char buf[1024]; + char buf[1024] = {0}; char charmsg[256]; va_list argprt; @@ -131,6 +184,14 @@ printf("\r\n"); #endif #endif + if (logFP) { + fprintf(logFP, "[%llu] ", getTimeStamp()); + va_list args; + va_start(args, msg); + vfwprintf(logFP, msg, args); + va_end(args); + fprintf(logFP, "\r\n"); + } } /** @@ -138,8 +199,8 @@ */ void wPrintJavaDebugString(wchar_t *msg, ...) { #ifdef JAVA_DEBUGGING_ON - char buf[1024]; - char charmsg[256]; + char buf[1024] = {0}; + char charmsg[256] = {0}; va_list argprt; va_start(argprt, msg); // set up argptr @@ -153,6 +214,14 @@ printf("\r\n"); #endif #endif + if (logFP) { + fprintf(logFP, "[%llu] ", getTimeStamp()); + va_list args; + va_start(args, msg); + vfwprintf(logFP, msg, args); + va_end(args); + fprintf(logFP, "\r\n"); + } } #ifdef __cplusplus }