< prev index next >

src/jdk.accessibility/windows/native/common/AccessBridgeDebug.cpp

Print this page

        

@@ -29,43 +29,81 @@
 
 #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;
+static std::string filePath;
+
+void initializeFileLogger(char * suffix) {
+    const char* var = "JAVA_ACCESSBRIDGE_LOGFILE";
+    const char* envfilePath = getenv(var);
+    if (envfilePath != nullptr) {
+        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 = 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';
     }
     if (!FormatMessage(
                        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);
     }
     return (char *)retbuf;

@@ -75,11 +113,11 @@
     /**
      * Send debugging info to the appropriate place
      */
     void PrintDebugString(char *msg, ...) {
 #ifdef DEBUGGING_ON
-        char buf[1024];
+        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,18 +126,26 @@
 #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];
+        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,17 +154,25 @@
 #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];
+        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,19 +183,27 @@
 #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];
-        char charmsg[256];
+        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,9 +213,17 @@
 #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 >