1 /*
   2  * Copyright (c) 2005, 2013, 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     }
  49     if (!FormatMessage(
  50                        FORMAT_MESSAGE_ALLOCATE_BUFFER |
  51                        FORMAT_MESSAGE_FROM_SYSTEM |
  52                        FORMAT_MESSAGE_IGNORE_INSERTS,
  53                        NULL,
  54                        GetLastError(),
  55                        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  56                        (LPTSTR) &lpMsgBuf,
  57                        0,
  58                        NULL ))
  59         {
  60             PrintDebugString("  %s: FormatMessage failed", msg);
  61         } else {
  62             PrintDebugString("  %s: %s", msg, (char *)lpMsgBuf);
  63         }
  64     if (lpMsgBuf != NULL) {
  65         strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1);
  66         strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1);
  67     }
  68     return (char *)retbuf;
  69 }
  70 
  71 
  72     /**
  73      * Send debugging info to the appropriate place
  74      */
  75     void PrintDebugString(char *msg, ...) {
  76 #ifdef DEBUGGING_ON
  77         char buf[1024];
  78         va_list argprt;
  79 
  80         va_start(argprt, msg);     // set up argptr
  81         vsprintf(buf, msg, argprt);
  82 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
  83         OutputDebugString(buf);
  84 #endif
  85 #ifdef SEND_TO_CONSOLE
  86         printf(buf);
  87         printf("\r\n");
  88 #endif
  89 #endif
  90     }
  91 
  92     /**
  93      * Send Java debugging info to the appropriate place
  94      */
  95     void PrintJavaDebugString2(char *msg, ...) {
  96 #ifdef JAVA_DEBUGGING_ON
  97         char buf[1024];
  98         va_list argprt;
  99 
 100         va_start(argprt, msg);     // set up argptr
 101         vsprintf(buf, msg, argprt);
 102 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 103         OutputDebugString(buf);
 104 #endif
 105 #ifdef SEND_TO_CONSOLE
 106         printf(buf);
 107         printf("\r\n");
 108 #endif
 109 #endif
 110     }
 111     /**
 112      * Wide version of the method to send debugging info to the appropriate place
 113      */
 114     void wPrintDebugString(wchar_t *msg, ...) {
 115 #ifdef DEBUGGING_ON
 116         char buf[1024];
 117         char charmsg[256];
 118         va_list argprt;
 119 
 120         va_start(argprt, msg);          // set up argptr
 121         sprintf(charmsg, "%ls", msg);  // convert format string to multi-byte
 122         vsprintf(buf, charmsg, argprt);
 123 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 124         OutputDebugString(buf);
 125 #endif
 126 #ifdef SEND_TO_CONSOLE
 127         printf(buf);
 128         printf("\r\n");
 129 #endif
 130 #endif
 131     }
 132 
 133     /**
 134      * Wide version of the method to send Java debugging info to the appropriate place
 135      */
 136     void wPrintJavaDebugString(wchar_t *msg, ...) {
 137 #ifdef JAVA_DEBUGGING_ON
 138         char buf[1024];
 139         char charmsg[256];
 140         va_list argprt;
 141 
 142         va_start(argprt, msg);          // set up argptr
 143         sprintf(charmsg, "%ls", msg);  // convert format string to multi-byte
 144         vsprintf(buf, charmsg, argprt);
 145 #ifdef SEND_TO_OUTPUT_DEBUG_STRING
 146         OutputDebugString(buf);
 147 #endif
 148 #ifdef SEND_TO_CONSOLE
 149         printf(buf);
 150         printf("\r\n");
 151 #endif
 152 #endif
 153     }
 154 #ifdef __cplusplus
 155 }
 156 #endif