--- old/src/jdk.accessibility/windows/native/common/AccessBridgeDebug.cpp 2018-12-11 16:47:23.731347200 +0530 +++ new/src/jdk.accessibility/windows/native/common/AccessBridgeDebug.cpp 2018-12-11 16:47:18.759127700 +0530 @@ -31,19 +31,72 @@ #include #include #include +#include +#include +#include #ifdef __cplusplus extern "C" { #endif +static FILE* logFP = nullptr; + +void initializeFileLogger(char * suffix) { + auto var = "JAVA_ACCESSBRIDGE_LOGFILE"; + const auto envfilePath = getenv(var); + if (envfilePath != nullptr) { + auto ext = const_cast(strrchr(envfilePath, '.')); + auto filePath = static_cast(nullptr); + auto len = strlen(envfilePath); + auto suffixlen = suffix != nullptr ? strlen(suffix) : (decltype(strlen(nullptr)))0; + + if (ext == nullptr) { + filePath = new char[len + suffixlen + 5]; + memset(filePath, 0, len + suffixlen + 5); + memcpy(filePath, envfilePath, len); + memcpy(filePath + len, suffix, suffixlen); + memcpy(filePath + len + suffixlen, ".log", 4); + } else { + auto extLen = strlen(ext); + + filePath = new char[len + suffixlen + 1]; + memset(filePath, 0, len + suffixlen + 1); + memcpy(filePath, envfilePath, len - extLen); + memcpy(filePath + len - extLen, suffix, suffixlen); + memcpy(filePath + len + suffixlen - extLen, ext, extLen); + } + + logFP = fopen(filePath, "w"); + if (logFP == nullptr) { + PrintDebugString("couldnot open file %s", filePath); + } + + delete [] 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 +105,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 +130,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 +143,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 +158,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 +171,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 +200,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 +215,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 +230,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 }