Path: blob/master/src/jdk.accessibility/windows/native/common/AccessBridgeDebug.cpp
40957 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* A class to manage AccessBridge debugging27*/2829#include "AccessBridgeDebug.h"30#include <stdarg.h>31#include <stdio.h>32#include <windows.h>33#include <cstdlib>34#include <cstring>3536#ifdef __cplusplus37extern "C" {38#endif3940static FILE* logFP = nullptr;4142void initializeFileLogger(char * fileName) {43auto var = "JAVA_ACCESSBRIDGE_LOGDIR";44const auto envfilePath = getenv(var);45if (envfilePath != nullptr && fileName != nullptr) {46auto envFilePathLength = strlen(envfilePath);47auto fileNameLength = strlen(fileName);48auto filePathSize = envFilePathLength + 1 + fileNameLength + 5; //1 for "/", 5 for ".log" and 0;49auto filePath = new char[filePathSize];50memset(filePath, 0, filePathSize*sizeof(char));51memcpy(filePath, envfilePath, envFilePathLength*sizeof(char));52filePath[envFilePathLength] = '/';53memcpy(filePath + envFilePathLength + 1, fileName, fileNameLength*sizeof(char));54memcpy(filePath + envFilePathLength + 1 + fileNameLength, ".log", 4*sizeof(char));5556logFP = fopen(filePath, "w");57if (logFP == nullptr) {58printf("\n%s\n", filePath);59PrintDebugString("Could not open file %s", filePath);60}6162delete [] filePath;63}64}6566void finalizeFileLogger() {67if (logFP) {68fclose(logFP);69logFP = nullptr;70}71}7273unsigned long long getTimeStamp() {74FILETIME ft;75ULARGE_INTEGER uli;76GetSystemTimeAsFileTime(&ft);77uli.LowPart = ft.dwLowDateTime;78uli.HighPart = ft.dwHighDateTime;79return (uli.QuadPart / 10000ULL) - 11644473600000ULL; // Rebase Epoch from 1601 to 197080}8182/**83* print a GetLastError message84*/85char *printError(char *msg) {86LPVOID lpMsgBuf = nullptr;87static char retbuf[256] = {0};8889if (msg != nullptr) {90strncpy((char *)retbuf, msg, sizeof(retbuf));91// if msg text is >= 256 ensure buffer is null terminated92retbuf[255] = '\0';93}94if (!FormatMessage(95FORMAT_MESSAGE_ALLOCATE_BUFFER |96FORMAT_MESSAGE_FROM_SYSTEM |97FORMAT_MESSAGE_IGNORE_INSERTS,98nullptr,99GetLastError(),100MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language101(LPTSTR) &lpMsgBuf,1020,103nullptr))104{105PrintDebugString(" %s: FormatMessage failed", msg);106} else {107PrintDebugString(" %s: %s", msg, (char *)lpMsgBuf);108}109if (lpMsgBuf != nullptr) {110strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1);111strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1);112LocalFree(lpMsgBuf);113}114return (char *)retbuf;115}116117118/**119* Send debugging info to the appropriate place120*/121void PrintDebugString(char *msg, ...) {122#ifdef DEBUGGING_ON123char buf[1024] = {0};124va_list argprt;125126va_start(argprt, msg); // set up argptr127vsprintf(buf, msg, argprt);128#ifdef SEND_TO_OUTPUT_DEBUG_STRING129OutputDebugString(buf);130#endif131#ifdef SEND_TO_CONSOLE132printf(buf);133printf("\r\n");134#endif135#endif136if (logFP) {137fprintf(logFP, "[%llu] ", getTimeStamp());138va_list args;139va_start(args, msg);140vfprintf(logFP, msg, args);141va_end(args);142fprintf(logFP, "\r\n");143}144}145146/**147* Send Java debugging info to the appropriate place148*/149void PrintJavaDebugString2(char *msg, ...) {150#ifdef JAVA_DEBUGGING_ON151char buf[1024] = {0};152va_list argprt;153154va_start(argprt, msg); // set up argptr155vsprintf(buf, msg, argprt);156#ifdef SEND_TO_OUTPUT_DEBUG_STRING157OutputDebugString(buf);158#endif159#ifdef SEND_TO_CONSOLE160printf(buf);161printf("\r\n");162#endif163#endif164if (logFP) {165fprintf(logFP, "[%llu] ", getTimeStamp());166va_list args;167va_start(args, msg);168vfprintf(logFP, msg, args);169va_end(args);170fprintf(logFP, "\r\n");171}172}173/**174* Wide version of the method to send debugging info to the appropriate place175*/176void wPrintDebugString(wchar_t *msg, ...) {177#ifdef DEBUGGING_ON178char buf[1024] = {0};179char charmsg[256];180va_list argprt;181182va_start(argprt, msg); // set up argptr183sprintf(charmsg, "%ls", msg); // convert format string to multi-byte184vsprintf(buf, charmsg, argprt);185#ifdef SEND_TO_OUTPUT_DEBUG_STRING186OutputDebugString(buf);187#endif188#ifdef SEND_TO_CONSOLE189printf(buf);190printf("\r\n");191#endif192#endif193if (logFP) {194fprintf(logFP, "[%llu] ", getTimeStamp());195va_list args;196va_start(args, msg);197vfwprintf(logFP, msg, args);198va_end(args);199fprintf(logFP, "\r\n");200}201}202203/**204* Wide version of the method to send Java debugging info to the appropriate place205*/206void wPrintJavaDebugString(wchar_t *msg, ...) {207#ifdef JAVA_DEBUGGING_ON208char buf[1024] = {0};209char charmsg[256] = {0};210va_list argprt;211212va_start(argprt, msg); // set up argptr213sprintf(charmsg, "%ls", msg); // convert format string to multi-byte214vsprintf(buf, charmsg, argprt);215#ifdef SEND_TO_OUTPUT_DEBUG_STRING216OutputDebugString(buf);217#endif218#ifdef SEND_TO_CONSOLE219printf(buf);220printf("\r\n");221#endif222#endif223if (logFP) {224fprintf(logFP, "[%llu] ", getTimeStamp());225va_list args;226va_start(args, msg);227vfwprintf(logFP, msg, args);228va_end(args);229fprintf(logFP, "\r\n");230}231}232#ifdef __cplusplus233}234#endif235236237