< prev index next >
src/jdk.accessibility/windows/native/common/AccessBridgeDebug.cpp
Print this page
*** 29,71 ****
#include "AccessBridgeDebug.h"
#include <stdarg.h>
#include <stdio.h>
#include <windows.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* print a GetLastError message
*/
char *printError(char *msg) {
! LPVOID lpMsgBuf = NULL;
! static char retbuf[256];
! if (msg != NULL) {
strncpy((char *)retbuf, msg, sizeof(retbuf));
// if msg text is >= 256 ensure buffer is null terminated
retbuf[255] = '\0';
}
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
! NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
! NULL ))
{
PrintDebugString(" %s: FormatMessage failed", msg);
} else {
PrintDebugString(" %s: %s", msg, (char *)lpMsgBuf);
}
! if (lpMsgBuf != NULL) {
strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1);
strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1);
LocalFree(lpMsgBuf);
}
return (char *)retbuf;
--- 29,108 ----
#include "AccessBridgeDebug.h"
#include <stdarg.h>
#include <stdio.h>
#include <windows.h>
+ #include <cstdlib>
+ #include <chrono>
+ #include <string>
#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<milliseconds>(steady_clock::now().time_since_epoch());
+
+ return timeNow.count();
+ }
+
/**
* print a GetLastError message
*/
char *printError(char *msg) {
! LPVOID lpMsgBuf = nullptr;
! static char retbuf[256] = {0};
! if (msg != nullptr) {
strncpy((char *)retbuf, msg, sizeof(retbuf));
// if msg text is >= 256 ensure buffer is null terminated
retbuf[255] = '\0';
}
if (!FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
! nullptr,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
! nullptr))
{
PrintDebugString(" %s: FormatMessage failed", msg);
} else {
PrintDebugString(" %s: %s", msg, (char *)lpMsgBuf);
}
! if (lpMsgBuf != nullptr) {
strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1);
strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1);
LocalFree(lpMsgBuf);
}
return (char *)retbuf;
*** 75,85 ****
/**
* Send debugging info to the appropriate place
*/
void PrintDebugString(char *msg, ...) {
#ifdef DEBUGGING_ON
! char buf[1024];
va_list argprt;
va_start(argprt, msg); // set up argptr
vsprintf(buf, msg, argprt);
#ifdef SEND_TO_OUTPUT_DEBUG_STRING
--- 112,122 ----
/**
* Send debugging info to the appropriate place
*/
void PrintDebugString(char *msg, ...) {
#ifdef DEBUGGING_ON
! char buf[1024] = {0};
va_list argprt;
va_start(argprt, msg); // set up argptr
vsprintf(buf, msg, argprt);
#ifdef SEND_TO_OUTPUT_DEBUG_STRING
*** 88,105 ****
#ifdef SEND_TO_CONSOLE
printf(buf);
printf("\r\n");
#endif
#endif
}
/**
* Send Java debugging info to the appropriate place
*/
void PrintJavaDebugString2(char *msg, ...) {
#ifdef JAVA_DEBUGGING_ON
! char buf[1024];
va_list argprt;
va_start(argprt, msg); // set up argptr
vsprintf(buf, msg, argprt);
#ifdef SEND_TO_OUTPUT_DEBUG_STRING
--- 125,150 ----
#ifdef SEND_TO_CONSOLE
printf(buf);
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");
+ }
}
/**
* Send Java debugging info to the appropriate place
*/
void PrintJavaDebugString2(char *msg, ...) {
#ifdef JAVA_DEBUGGING_ON
! char buf[1024] = {0};
va_list argprt;
va_start(argprt, msg); // set up argptr
vsprintf(buf, msg, argprt);
#ifdef SEND_TO_OUTPUT_DEBUG_STRING
*** 108,124 ****
#ifdef SEND_TO_CONSOLE
printf(buf);
printf("\r\n");
#endif
#endif
}
/**
* 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 charmsg[256];
va_list argprt;
va_start(argprt, msg); // set up argptr
sprintf(charmsg, "%ls", msg); // convert format string to multi-byte
--- 153,177 ----
#ifdef SEND_TO_CONSOLE
printf(buf);
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] = {0};
char charmsg[256];
va_list argprt;
va_start(argprt, msg); // set up argptr
sprintf(charmsg, "%ls", msg); // convert format string to multi-byte
*** 129,147 ****
#ifdef SEND_TO_CONSOLE
printf(buf);
printf("\r\n");
#endif
#endif
}
/**
* Wide version of the method to send Java debugging info to the appropriate place
*/
void wPrintJavaDebugString(wchar_t *msg, ...) {
#ifdef JAVA_DEBUGGING_ON
! char buf[1024];
! char charmsg[256];
va_list argprt;
va_start(argprt, msg); // set up argptr
sprintf(charmsg, "%ls", msg); // convert format string to multi-byte
vsprintf(buf, charmsg, argprt);
--- 182,208 ----
#ifdef SEND_TO_CONSOLE
printf(buf);
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");
+ }
}
/**
* Wide version of the method to send Java debugging info to the appropriate place
*/
void wPrintJavaDebugString(wchar_t *msg, ...) {
#ifdef JAVA_DEBUGGING_ON
! char buf[1024] = {0};
! char charmsg[256] = {0};
va_list argprt;
va_start(argprt, msg); // set up argptr
sprintf(charmsg, "%ls", msg); // convert format string to multi-byte
vsprintf(buf, charmsg, argprt);
*** 151,159 ****
--- 212,228 ----
#ifdef SEND_TO_CONSOLE
printf(buf);
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
}
#endif
< prev index next >