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 } 70 return (char *)retbuf; 71 } 72 73 74 /** 75 * Send debugging info to the appropriate place 76 */ 77 void PrintDebugString(char *msg, ...) { 78 #ifdef DEBUGGING_ON 79 char buf[1024]; 80 va_list argprt; 81 82 va_start(argprt, msg); // set up argptr 83 vsprintf(buf, msg, argprt); 84 #ifdef SEND_TO_OUTPUT_DEBUG_STRING 85 OutputDebugString(buf); 86 #endif 87 #ifdef SEND_TO_CONSOLE 88 printf(buf); 89 printf("\r\n"); 90 #endif 91 #endif 92 } 93 94 /** 95 * Send Java debugging info to the appropriate place 96 */ 97 void PrintJavaDebugString2(char *msg, ...) { 98 #ifdef JAVA_DEBUGGING_ON 99 char buf[1024]; 100 va_list argprt; 101 102 va_start(argprt, msg); // set up argptr 103 vsprintf(buf, msg, argprt); 104 #ifdef SEND_TO_OUTPUT_DEBUG_STRING 105 OutputDebugString(buf); 106 #endif 107 #ifdef SEND_TO_CONSOLE 108 printf(buf); 109 printf("\r\n"); 110 #endif 111 #endif 112 } 113 /** 114 * Wide version of the method to send debugging info to the appropriate place 115 */ 116 void wPrintDebugString(wchar_t *msg, ...) { 117 #ifdef DEBUGGING_ON 118 char buf[1024]; 119 char charmsg[256]; 120 va_list argprt; 121 122 va_start(argprt, msg); // set up argptr 123 sprintf(charmsg, "%ls", msg); // convert format string to multi-byte 124 vsprintf(buf, charmsg, argprt); 125 #ifdef SEND_TO_OUTPUT_DEBUG_STRING 126 OutputDebugString(buf); 127 #endif 128 #ifdef SEND_TO_CONSOLE 129 printf(buf); 130 printf("\r\n"); 131 #endif 132 #endif 133 } 134 135 /** 136 * Wide version of the method to send Java debugging info to the appropriate place 137 */ 138 void wPrintJavaDebugString(wchar_t *msg, ...) { 139 #ifdef JAVA_DEBUGGING_ON 140 char buf[1024]; 141 char charmsg[256]; 142 va_list argprt; 143 144 va_start(argprt, msg); // set up argptr 145 sprintf(charmsg, "%ls", msg); // convert format string to multi-byte 146 vsprintf(buf, charmsg, argprt); 147 #ifdef SEND_TO_OUTPUT_DEBUG_STRING 148 OutputDebugString(buf); 149 #endif 150 #ifdef SEND_TO_CONSOLE 151 printf(buf); 152 printf("\r\n"); 153 #endif 154 #endif 155 } 156 #ifdef __cplusplus 157 } 158 #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 static std::string filePath; 44 45 void initializeFileLogger(char * suffix) { 46 const char* var = "JAVA_ACCESSBRIDGE_LOGFILE"; 47 const char* envfilePath = getenv(var); 48 if (envfilePath != nullptr) { 49 filePath = std::string(envfilePath); 50 if (suffix != nullptr) { 51 auto indx = filePath.find_last_of("."); 52 filePath = indx != std::string::npos ? 53 filePath.substr(0, indx) + suffix + filePath.substr(indx) : 54 filePath + suffix; 55 } 56 logFP = fopen(filePath.c_str(), "w"); 57 if (logFP == nullptr) { 58 PrintDebugString("couldnot open file %s", filePath); 59 } 60 } 61 } 62 63 void finalizeFileLogger() { 64 if (logFP) { 65 fclose(logFP); 66 logFP = nullptr; 67 } 68 } 69 70 auto getTimeStamp() -> long long { 71 using namespace std::chrono; 72 auto timeNow = duration_cast<milliseconds>(steady_clock::now().time_since_epoch()); 73 74 return timeNow.count(); 75 } 76 77 /** 78 * print a GetLastError message 79 */ 80 char *printError(char *msg) { 81 LPVOID lpMsgBuf = nullptr; 82 static char retbuf[256] = {0}; 83 84 if (msg != nullptr) { 85 strncpy((char *)retbuf, msg, sizeof(retbuf)); 86 // if msg text is >= 256 ensure buffer is null terminated 87 retbuf[255] = '\0'; 88 } 89 if (!FormatMessage( 90 FORMAT_MESSAGE_ALLOCATE_BUFFER | 91 FORMAT_MESSAGE_FROM_SYSTEM | 92 FORMAT_MESSAGE_IGNORE_INSERTS, 93 nullptr, 94 GetLastError(), 95 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language 96 (LPTSTR) &lpMsgBuf, 97 0, 98 nullptr)) 99 { 100 PrintDebugString(" %s: FormatMessage failed", msg); 101 } else { 102 PrintDebugString(" %s: %s", msg, (char *)lpMsgBuf); 103 } 104 if (lpMsgBuf != nullptr) { 105 strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1); 106 strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1); 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 |