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 <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 |