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 
  36 #ifdef __cplusplus
  37 extern "C" {
  38 #endif
  39 
  40 static FILE* logFP = nullptr;
  41 static char* filePath = nullptr;
  42 
  43 void initializeFileLogger() {
  44         const char* var = "JAVA_ACCESSBRIDGE_LOGFILE";
  45         filePath = getenv(var);
  46         if (filePath != nullptr) {
  47                 logFP = fopen(filePath, "w");
  48                 if (logFP == nullptr) {
  49                         PrintDebugString("couldnot open file %s", filePath);
  50                 }
  51         }
  52 }
  53 
  54 void finalizeFileLogger() {
  55         if (logFP) {
  56                 fclose(logFP);
  57                 logFP = nullptr;
  58         }
  59 }
  60 /**
  61  * print a GetLastError message
  62  */
  63 char *printError(char *msg) {
  64     LPVOID lpMsgBuf = NULL;
  65     static char retbuf[256];
  66 
  67     if (msg != NULL) {
  68         strncpy((char *)retbuf, msg, sizeof(retbuf));
  69         // if msg text is >= 256 ensure buffer is null terminated
  70         retbuf[255] = '\0';
  71     }
  72     if (!FormatMessage(
  73                        FORMAT_MESSAGE_ALLOCATE_BUFFER |
  74                        FORMAT_MESSAGE_FROM_SYSTEM |
  75                        FORMAT_MESSAGE_IGNORE_INSERTS,
  76                        NULL,
  77                        GetLastError(),
  78                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  79                        (LPTSTR) &lpMsgBuf,
  80                        0,
  81                        NULL ))
  82         {
  83             PrintDebugString("  %s: FormatMessage failed", msg);
  84         } else {
  85             PrintDebugString("  %s: %s", msg, (char *)lpMsgBuf);
  86         }
  87     if (lpMsgBuf != NULL) {
  88         strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1);
  89         strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1);
  90     }
  91     return (char *)retbuf;
  92 }
  93 
  94 
  95     /**
  96      * Send debugging info to the appropriate place
  97      */
  98     void PrintDebugString(char *msg, ...) {
  99 #ifdef 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         if (logFP) {
 114                 va_list args;
 115                 va_start(args, msg);
 116                 vfprintf(logFP, msg, args);
 117                 va_end(args);
 118         }
 119     }
 120 
 121     /**
 122      * Send Java debugging info to the appropriate place
 123      */
 124     void PrintJavaDebugString2(char *msg, ...) {
 125 #ifdef JAVA_DEBUGGING_ON
 126         char buf[1024];
 127         va_list argprt;
 128 
 129         va_start(argprt, msg);     // set up argptr
 130         vsprintf(buf, msg, argprt);
 131 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 132         OutputDebugString(buf);
 133 #endif
 134 #ifdef SEND_TO_CONSOLE
 135         printf(buf);
 136         printf("\r\n");
 137 #endif
 138 #endif
 139         if (logFP) {
 140                 va_list args;
 141                 va_start(args, msg);
 142                 vfprintf(logFP, msg, args);
 143                 va_end(args);
 144         }
 145     }
 146     /**
 147      * Wide version of the method to send debugging info to the appropriate place
 148      */
 149     void wPrintDebugString(wchar_t *msg, ...) {
 150 #ifdef DEBUGGING_ON
 151         char buf[1024];
 152         char charmsg[256];
 153         va_list argprt;
 154 
 155         va_start(argprt, msg);          // set up argptr
 156         sprintf(charmsg, "%ls", msg);  // convert format string to multi-byte
 157         vsprintf(buf, charmsg, argprt);
 158 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 159         OutputDebugString(buf);
 160 #endif
 161 #ifdef SEND_TO_CONSOLE
 162         printf(buf);
 163         printf("\r\n");
 164 #endif
 165 #endif
 166         if (logFP) {
 167                 va_list args;
 168                 va_start(args, msg);
 169                 vfwprintf(logFP, msg, args);
 170                 va_end(args);
 171         }
 172     }
 173 
 174     /**
 175      * Wide version of the method to send Java debugging info to the appropriate place
 176      */
 177     void wPrintJavaDebugString(wchar_t *msg, ...) {
 178 #ifdef JAVA_DEBUGGING_ON
 179         char buf[1024];
 180         char charmsg[256];
 181         va_list argprt;
 182 
 183         va_start(argprt, msg);          // set up argptr
 184         sprintf(charmsg, "%ls", msg);  // convert format string to multi-byte
 185         vsprintf(buf, charmsg, argprt);
 186 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 187         OutputDebugString(buf);
 188 #endif
 189 #ifdef SEND_TO_CONSOLE
 190         printf(buf);
 191         printf("\r\n");
 192 #endif
 193 #endif
 194         if (logFP){
 195                 va_list args;
 196                 va_start(args, msg);
 197                 vfwprintf(logFP, msg, args);
 198                 va_end(args);
 199         }
 200     }
 201 #ifdef __cplusplus
 202 }
 203 #endif