< prev index next >

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

Print this page




  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * A class to manage AccessBridge debugging
  28  */
  29 
  30 #include "AccessBridgeDebug.h"
  31 #include <stdarg.h>
  32 #include <stdio.h>
  33 #include <windows.h>



  34 
  35 #ifdef __cplusplus
  36 extern "C" {
  37 #endif
  38 


















































  39 /**
  40  * print a GetLastError message
  41  */
  42 char *printError(char *msg) {
  43     LPVOID lpMsgBuf = NULL;
  44     static char retbuf[256];
  45 
  46     if (msg != NULL) {
  47         strncpy((char *)retbuf, msg, sizeof(retbuf));
  48         // if msg text is >= 256 ensure buffer is null terminated
  49         retbuf[255] = '\0';
  50     }
  51     if (!FormatMessage(
  52                        FORMAT_MESSAGE_ALLOCATE_BUFFER |
  53                        FORMAT_MESSAGE_FROM_SYSTEM |
  54                        FORMAT_MESSAGE_IGNORE_INSERTS,
  55                        NULL,
  56                        GetLastError(),
  57                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  58                        (LPTSTR) &lpMsgBuf,
  59                        0,
  60                        NULL ))
  61         {
  62             PrintDebugString("  %s: FormatMessage failed", msg);
  63         } else {
  64             PrintDebugString("  %s: %s", msg, (char *)lpMsgBuf);
  65         }
  66     if (lpMsgBuf != NULL) {
  67         strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1);
  68         strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1);
  69         LocalFree(lpMsgBuf);
  70     }
  71     return (char *)retbuf;
  72 }
  73 
  74 
  75     /**
  76      * Send debugging info to the appropriate place
  77      */
  78     void PrintDebugString(char *msg, ...) {
  79 #ifdef DEBUGGING_ON
  80         char buf[1024];
  81         va_list argprt;
  82 
  83         va_start(argprt, msg);     // set up argptr
  84         vsprintf(buf, msg, argprt);
  85 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
  86         OutputDebugString(buf);
  87 #endif
  88 #ifdef SEND_TO_CONSOLE
  89         printf(buf);
  90         printf("\r\n");
  91 #endif
  92 #endif








  93     }
  94 
  95     /**
  96      * Send Java debugging info to the appropriate place
  97      */
  98     void PrintJavaDebugString2(char *msg, ...) {
  99 #ifdef JAVA_DEBUGGING_ON
 100         char buf[1024];
 101         va_list argprt;
 102 
 103         va_start(argprt, msg);     // set up argptr
 104         vsprintf(buf, msg, argprt);
 105 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 106         OutputDebugString(buf);
 107 #endif
 108 #ifdef SEND_TO_CONSOLE
 109         printf(buf);
 110         printf("\r\n");
 111 #endif
 112 #endif








 113     }
 114     /**
 115      * Wide version of the method to send debugging info to the appropriate place
 116      */
 117     void wPrintDebugString(wchar_t *msg, ...) {
 118 #ifdef DEBUGGING_ON
 119         char buf[1024];
 120         char charmsg[256];
 121         va_list argprt;
 122 
 123         va_start(argprt, msg);          // set up argptr
 124         sprintf(charmsg, "%ls", msg);  // convert format string to multi-byte
 125         vsprintf(buf, charmsg, argprt);
 126 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 127         OutputDebugString(buf);
 128 #endif
 129 #ifdef SEND_TO_CONSOLE
 130         printf(buf);
 131         printf("\r\n");
 132 #endif
 133 #endif








 134     }
 135 
 136     /**
 137      * Wide version of the method to send Java debugging info to the appropriate place
 138      */
 139     void wPrintJavaDebugString(wchar_t *msg, ...) {
 140 #ifdef JAVA_DEBUGGING_ON
 141         char buf[1024];
 142         char charmsg[256];
 143         va_list argprt;
 144 
 145         va_start(argprt, msg);          // set up argptr
 146         sprintf(charmsg, "%ls", msg);  // convert format string to multi-byte
 147         vsprintf(buf, charmsg, argprt);
 148 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 149         OutputDebugString(buf);
 150 #endif
 151 #ifdef SEND_TO_CONSOLE
 152         printf(buf);
 153         printf("\r\n");
 154 #endif
 155 #endif








 156     }
 157 #ifdef __cplusplus
 158 }
 159 #endif


  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * A class to manage AccessBridge debugging
  28  */
  29 
  30 #include "AccessBridgeDebug.h"
  31 #include <stdarg.h>
  32 #include <stdio.h>
  33 #include <windows.h>
  34 #include <cstdlib>
  35 #include <chrono>
  36 #include <cstring>
  37 
  38 #ifdef __cplusplus
  39 extern "C" {
  40 #endif
  41 
  42 static FILE* logFP = nullptr;
  43 
  44 void initializeFileLogger(char * suffix) {
  45     auto var = "JAVA_ACCESSBRIDGE_LOGFILE";
  46     const auto envfilePath = getenv(var);
  47     if (envfilePath != nullptr) {
  48         auto ext = const_cast<char*>(strrchr(envfilePath, '.'));
  49         auto filePath = static_cast<char*>(nullptr);
  50         auto len = strlen(envfilePath);
  51         auto suffixlen = suffix != nullptr ? strlen(suffix) : (decltype(strlen(nullptr)))0;
  52 
  53         if (ext == nullptr) {
  54             filePath = new char[len + suffixlen + 5];
  55             memset(filePath, 0, len + suffixlen + 5);
  56             memcpy(filePath, envfilePath, len);
  57             memcpy(filePath + len, suffix, suffixlen);
  58             memcpy(filePath + len + suffixlen, ".log", 4);
  59         } else {
  60             auto extLen = strlen(ext);
  61 
  62             filePath = new char[len + suffixlen + 1];
  63             memset(filePath, 0, len + suffixlen + 1);
  64             memcpy(filePath, envfilePath, len - extLen);
  65             memcpy(filePath + len - extLen, suffix, suffixlen);
  66             memcpy(filePath + len + suffixlen - extLen, ext, extLen);
  67         }
  68 
  69         logFP = fopen(filePath, "w");
  70         if (logFP == nullptr) {
  71             PrintDebugString("couldnot open file %s", filePath);
  72         }
  73 
  74         delete [] filePath;
  75     }
  76 }
  77 
  78 void finalizeFileLogger() {
  79     if (logFP) {
  80         fclose(logFP);
  81         logFP = nullptr;
  82     }
  83 }
  84 
  85 auto getTimeStamp() -> long long {
  86     using namespace std::chrono;
  87     auto timeNow = duration_cast<milliseconds>(steady_clock::now().time_since_epoch());
  88 
  89     return timeNow.count();
  90 }
  91 
  92 /**
  93  * print a GetLastError message
  94  */
  95 char *printError(char *msg) {
  96     LPVOID lpMsgBuf = nullptr;
  97     static char retbuf[256] = {0};
  98 
  99     if (msg != nullptr) {
 100         strncpy((char *)retbuf, msg, sizeof(retbuf));
 101         // if msg text is >= 256 ensure buffer is null terminated
 102         retbuf[255] = '\0';
 103     }
 104     if (!FormatMessage(
 105                        FORMAT_MESSAGE_ALLOCATE_BUFFER |
 106                        FORMAT_MESSAGE_FROM_SYSTEM |
 107                        FORMAT_MESSAGE_IGNORE_INSERTS,
 108                        nullptr,
 109                        GetLastError(),
 110                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
 111                        (LPTSTR) &lpMsgBuf,
 112                        0,
 113                        nullptr))
 114         {
 115             PrintDebugString("  %s: FormatMessage failed", msg);
 116         } else {
 117             PrintDebugString("  %s: %s", msg, (char *)lpMsgBuf);
 118         }
 119     if (lpMsgBuf != nullptr) {
 120         strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1);
 121         strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1);
 122         LocalFree(lpMsgBuf);
 123     }
 124     return (char *)retbuf;
 125 }
 126 
 127 
 128     /**
 129      * Send debugging info to the appropriate place
 130      */
 131     void PrintDebugString(char *msg, ...) {
 132 #ifdef DEBUGGING_ON
 133         char buf[1024] = {0};
 134         va_list argprt;
 135 
 136         va_start(argprt, msg);     // set up argptr
 137         vsprintf(buf, msg, argprt);
 138 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 139         OutputDebugString(buf);
 140 #endif
 141 #ifdef SEND_TO_CONSOLE
 142         printf(buf);
 143         printf("\r\n");
 144 #endif
 145 #endif
 146         if (logFP) {
 147             fprintf(logFP, "[%lldu] ", getTimeStamp());
 148             va_list args;
 149             va_start(args, msg);
 150             vfprintf(logFP, msg, args);
 151             va_end(args);
 152             fprintf(logFP, "\r\n");
 153         }
 154     }
 155 
 156     /**
 157      * Send Java debugging info to the appropriate place
 158      */
 159     void PrintJavaDebugString2(char *msg, ...) {
 160 #ifdef JAVA_DEBUGGING_ON
 161         char buf[1024] = {0};
 162         va_list argprt;
 163 
 164         va_start(argprt, msg);     // set up argptr
 165         vsprintf(buf, msg, argprt);
 166 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 167         OutputDebugString(buf);
 168 #endif
 169 #ifdef SEND_TO_CONSOLE
 170         printf(buf);
 171         printf("\r\n");
 172 #endif
 173 #endif
 174         if (logFP) {
 175             fprintf(logFP, "[%llu] ", getTimeStamp());
 176             va_list args;
 177             va_start(args, msg);
 178             vfprintf(logFP, msg, args);
 179             va_end(args);
 180             fprintf(logFP, "\r\n");
 181         }
 182     }
 183     /**
 184      * Wide version of the method to send debugging info to the appropriate place
 185      */
 186     void wPrintDebugString(wchar_t *msg, ...) {
 187 #ifdef DEBUGGING_ON
 188         char buf[1024] = {0};
 189         char charmsg[256];
 190         va_list argprt;
 191 
 192         va_start(argprt, msg);          // set up argptr
 193         sprintf(charmsg, "%ls", msg);  // convert format string to multi-byte
 194         vsprintf(buf, charmsg, argprt);
 195 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 196         OutputDebugString(buf);
 197 #endif
 198 #ifdef SEND_TO_CONSOLE
 199         printf(buf);
 200         printf("\r\n");
 201 #endif
 202 #endif
 203         if (logFP) {
 204             fprintf(logFP, "[%llu] ", getTimeStamp());
 205             va_list args;
 206             va_start(args, msg);
 207             vfwprintf(logFP, msg, args);
 208             va_end(args);
 209             fprintf(logFP, "\r\n");
 210         }
 211     }
 212 
 213     /**
 214      * Wide version of the method to send Java debugging info to the appropriate place
 215      */
 216     void wPrintJavaDebugString(wchar_t *msg, ...) {
 217 #ifdef JAVA_DEBUGGING_ON
 218         char buf[1024] = {0};
 219         char charmsg[256] = {0};
 220         va_list argprt;
 221 
 222         va_start(argprt, msg);          // set up argptr
 223         sprintf(charmsg, "%ls", msg);  // convert format string to multi-byte
 224         vsprintf(buf, charmsg, argprt);
 225 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 226         OutputDebugString(buf);
 227 #endif
 228 #ifdef SEND_TO_CONSOLE
 229         printf(buf);
 230         printf("\r\n");
 231 #endif
 232 #endif
 233         if (logFP) {
 234             fprintf(logFP, "[%llu] ", getTimeStamp());
 235             va_list args;
 236             va_start(args, msg);
 237             vfwprintf(logFP, msg, args);
 238             va_end(args);
 239             fprintf(logFP, "\r\n");
 240         }
 241     }
 242 #ifdef __cplusplus
 243 }
 244 #endif
< prev index next >