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