1 /* 2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 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 <string> 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 static FILE* logFP = nullptr; 43 44 void initializeFileLogger(char * suffix) { 45 const char* var = "JAVA_ACCESSBRIDGE_LOGFILE"; 46 const char* envfilePath = getenv(var); 47 if (envfilePath != nullptr) { 48 std::string filePath = std::string(envfilePath); 49 if (suffix != nullptr) { 50 auto indx = filePath.find_last_of("."); 51 filePath = indx != std::string::npos ? 52 filePath.substr(0, indx) + suffix + filePath.substr(indx) : 53 filePath + suffix + ".log"; 54 } 55 logFP = fopen(filePath.c_str(), "w"); 56 if (logFP == nullptr) { 57 PrintDebugString("couldnot open file %s", filePath); 58 } 59 } 60 } 61 62 void finalizeFileLogger() { 63 if (logFP) { 64 fclose(logFP); 65 logFP = nullptr; 66 } 67 } 68 69 auto getTimeStamp() -> long long { 70 using namespace std::chrono; 71 auto timeNow = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()); 72 73 return timeNow.count(); 74 } 75 76 /** 77 * print a GetLastError message 78 */ 79 char *printError(char *msg) { 80 LPVOID lpMsgBuf = nullptr; 81 static char retbuf[256] = {0}; 82 83 if (msg != nullptr) { 84 strncpy((char *)retbuf, msg, sizeof(retbuf)); 85 // if msg text is >= 256 ensure buffer is null terminated 86 retbuf[255] = '\0'; 87 } 88 if (!FormatMessage( 89 FORMAT_MESSAGE_ALLOCATE_BUFFER | 90 FORMAT_MESSAGE_FROM_SYSTEM | 91 FORMAT_MESSAGE_IGNORE_INSERTS, 92 nullptr, 93 GetLastError(), 94 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 95 (LPTSTR) &lpMsgBuf, 96 0, 97 nullptr)) 98 { 99 PrintDebugString(" %s: FormatMessage failed", msg); 100 } else { 101 PrintDebugString(" %s: %s", msg, (char *)lpMsgBuf); 102 } 103 if (lpMsgBuf != nullptr) { 104 strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1); 105 strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1); 106 LocalFree(lpMsgBuf); 107 } 108 return (char *)retbuf; 109 } 110 111 112 /** 113 * Send debugging info to the appropriate place 114 */ 115 void PrintDebugString(char *msg, ...) { 116 #ifdef DEBUGGING_ON 117 char buf[1024] = {0}; 118 va_list argprt; 119 120 va_start(argprt, msg); // set up argptr 121 vsprintf(buf, msg, argprt); 122 #ifdef SEND_TO_OUTPUT_DEBUG_STRING 123 OutputDebugString(buf); 124 #endif 125 #ifdef SEND_TO_CONSOLE 126 printf(buf); 127 printf("\r\n"); 128 #endif 129 #endif 130 if (logFP) { 131 fprintf(logFP, "[%lldu] ", getTimeStamp()); 132 va_list args; 133 va_start(args, msg); 134 vfprintf(logFP, msg, args); 135 va_end(args); 136 fprintf(logFP, "\r\n"); 137 } 138 } 139 140 /** 141 * Send Java debugging info to the appropriate place 142 */ 143 void PrintJavaDebugString2(char *msg, ...) { 144 #ifdef JAVA_DEBUGGING_ON 145 char buf[1024] = {0}; 146 va_list argprt; 147 148 va_start(argprt, msg); // set up argptr 149 vsprintf(buf, msg, argprt); 150 #ifdef SEND_TO_OUTPUT_DEBUG_STRING 151 OutputDebugString(buf); 152 #endif 153 #ifdef SEND_TO_CONSOLE 154 printf(buf); 155 printf("\r\n"); 156 #endif 157 #endif 158 if (logFP) { 159 fprintf(logFP, "[%llu] ", getTimeStamp()); 160 va_list args; 161 va_start(args, msg); 162 vfprintf(logFP, msg, args); 163 va_end(args); 164 fprintf(logFP, "\r\n"); 165 } 166 } 167 /** 168 * Wide version of the method to send debugging info to the appropriate place 169 */ 170 void wPrintDebugString(wchar_t *msg, ...) { 171 #ifdef DEBUGGING_ON 172 char buf[1024] = {0}; 173 char charmsg[256]; 174 va_list argprt; 175 176 va_start(argprt, msg); // set up argptr 177 sprintf(charmsg, "%ls", msg); // convert format string to multi-byte 178 vsprintf(buf, charmsg, argprt); 179 #ifdef SEND_TO_OUTPUT_DEBUG_STRING 180 OutputDebugString(buf); 181 #endif 182 #ifdef SEND_TO_CONSOLE 183 printf(buf); 184 printf("\r\n"); 185 #endif 186 #endif 187 if (logFP) { 188 fprintf(logFP, "[%llu] ", getTimeStamp()); 189 va_list args; 190 va_start(args, msg); 191 vfwprintf(logFP, msg, args); 192 va_end(args); 193 fprintf(logFP, "\r\n"); 194 } 195 } 196 197 /** 198 * Wide version of the method to send Java debugging info to the appropriate place 199 */ 200 void wPrintJavaDebugString(wchar_t *msg, ...) { 201 #ifdef JAVA_DEBUGGING_ON 202 char buf[1024] = {0}; 203 char charmsg[256] = {0}; 204 va_list argprt; 205 206 va_start(argprt, msg); // set up argptr 207 sprintf(charmsg, "%ls", msg); // convert format string to multi-byte 208 vsprintf(buf, charmsg, argprt); 209 #ifdef SEND_TO_OUTPUT_DEBUG_STRING 210 OutputDebugString(buf); 211 #endif 212 #ifdef SEND_TO_CONSOLE 213 printf(buf); 214 printf("\r\n"); 215 #endif 216 #endif 217 if (logFP) { 218 fprintf(logFP, "[%llu] ", getTimeStamp()); 219 va_list args; 220 va_start(args, msg); 221 vfwprintf(logFP, msg, args); 222 va_end(args); 223 fprintf(logFP, "\r\n"); 224 } 225 } 226 #ifdef __cplusplus 227 } 228 #endif