Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/jdk.accessibility/windows/native/common/AccessBridgeDebug.cpp
40957 views
1
/*
2
* Copyright (c) 2005, 2021, 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
#include <cstring>
36
37
#ifdef __cplusplus
38
extern "C" {
39
#endif
40
41
static FILE* logFP = nullptr;
42
43
void initializeFileLogger(char * fileName) {
44
auto var = "JAVA_ACCESSBRIDGE_LOGDIR";
45
const auto envfilePath = getenv(var);
46
if (envfilePath != nullptr && fileName != nullptr) {
47
auto envFilePathLength = strlen(envfilePath);
48
auto fileNameLength = strlen(fileName);
49
auto filePathSize = envFilePathLength + 1 + fileNameLength + 5; //1 for "/", 5 for ".log" and 0;
50
auto filePath = new char[filePathSize];
51
memset(filePath, 0, filePathSize*sizeof(char));
52
memcpy(filePath, envfilePath, envFilePathLength*sizeof(char));
53
filePath[envFilePathLength] = '/';
54
memcpy(filePath + envFilePathLength + 1, fileName, fileNameLength*sizeof(char));
55
memcpy(filePath + envFilePathLength + 1 + fileNameLength, ".log", 4*sizeof(char));
56
57
logFP = fopen(filePath, "w");
58
if (logFP == nullptr) {
59
printf("\n%s\n", filePath);
60
PrintDebugString("Could not open file %s", filePath);
61
}
62
63
delete [] filePath;
64
}
65
}
66
67
void finalizeFileLogger() {
68
if (logFP) {
69
fclose(logFP);
70
logFP = nullptr;
71
}
72
}
73
74
unsigned long long getTimeStamp() {
75
FILETIME ft;
76
ULARGE_INTEGER uli;
77
GetSystemTimeAsFileTime(&ft);
78
uli.LowPart = ft.dwLowDateTime;
79
uli.HighPart = ft.dwHighDateTime;
80
return (uli.QuadPart / 10000ULL) - 11644473600000ULL; // Rebase Epoch from 1601 to 1970
81
}
82
83
/**
84
* print a GetLastError message
85
*/
86
char *printError(char *msg) {
87
LPVOID lpMsgBuf = nullptr;
88
static char retbuf[256] = {0};
89
90
if (msg != nullptr) {
91
strncpy((char *)retbuf, msg, sizeof(retbuf));
92
// if msg text is >= 256 ensure buffer is null terminated
93
retbuf[255] = '\0';
94
}
95
if (!FormatMessage(
96
FORMAT_MESSAGE_ALLOCATE_BUFFER |
97
FORMAT_MESSAGE_FROM_SYSTEM |
98
FORMAT_MESSAGE_IGNORE_INSERTS,
99
nullptr,
100
GetLastError(),
101
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
102
(LPTSTR) &lpMsgBuf,
103
0,
104
nullptr))
105
{
106
PrintDebugString(" %s: FormatMessage failed", msg);
107
} else {
108
PrintDebugString(" %s: %s", msg, (char *)lpMsgBuf);
109
}
110
if (lpMsgBuf != nullptr) {
111
strncat((char *)retbuf, ": ", sizeof(retbuf) - strlen(retbuf) - 1);
112
strncat((char *)retbuf, (char *)lpMsgBuf, sizeof(retbuf) - strlen(retbuf) - 1);
113
LocalFree(lpMsgBuf);
114
}
115
return (char *)retbuf;
116
}
117
118
119
/**
120
* Send debugging info to the appropriate place
121
*/
122
void PrintDebugString(char *msg, ...) {
123
#ifdef DEBUGGING_ON
124
char buf[1024] = {0};
125
va_list argprt;
126
127
va_start(argprt, msg); // set up argptr
128
vsprintf(buf, msg, argprt);
129
#ifdef SEND_TO_OUTPUT_DEBUG_STRING
130
OutputDebugString(buf);
131
#endif
132
#ifdef SEND_TO_CONSOLE
133
printf(buf);
134
printf("\r\n");
135
#endif
136
#endif
137
if (logFP) {
138
fprintf(logFP, "[%llu] ", getTimeStamp());
139
va_list args;
140
va_start(args, msg);
141
vfprintf(logFP, msg, args);
142
va_end(args);
143
fprintf(logFP, "\r\n");
144
}
145
}
146
147
/**
148
* Send Java debugging info to the appropriate place
149
*/
150
void PrintJavaDebugString2(char *msg, ...) {
151
#ifdef JAVA_DEBUGGING_ON
152
char buf[1024] = {0};
153
va_list argprt;
154
155
va_start(argprt, msg); // set up argptr
156
vsprintf(buf, msg, argprt);
157
#ifdef SEND_TO_OUTPUT_DEBUG_STRING
158
OutputDebugString(buf);
159
#endif
160
#ifdef SEND_TO_CONSOLE
161
printf(buf);
162
printf("\r\n");
163
#endif
164
#endif
165
if (logFP) {
166
fprintf(logFP, "[%llu] ", getTimeStamp());
167
va_list args;
168
va_start(args, msg);
169
vfprintf(logFP, msg, args);
170
va_end(args);
171
fprintf(logFP, "\r\n");
172
}
173
}
174
/**
175
* Wide version of the method to send debugging info to the appropriate place
176
*/
177
void wPrintDebugString(wchar_t *msg, ...) {
178
#ifdef DEBUGGING_ON
179
char buf[1024] = {0};
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
fprintf(logFP, "[%llu] ", getTimeStamp());
196
va_list args;
197
va_start(args, msg);
198
vfwprintf(logFP, msg, args);
199
va_end(args);
200
fprintf(logFP, "\r\n");
201
}
202
}
203
204
/**
205
* Wide version of the method to send Java debugging info to the appropriate place
206
*/
207
void wPrintJavaDebugString(wchar_t *msg, ...) {
208
#ifdef JAVA_DEBUGGING_ON
209
char buf[1024] = {0};
210
char charmsg[256] = {0};
211
va_list argprt;
212
213
va_start(argprt, msg); // set up argptr
214
sprintf(charmsg, "%ls", msg); // convert format string to multi-byte
215
vsprintf(buf, charmsg, argprt);
216
#ifdef SEND_TO_OUTPUT_DEBUG_STRING
217
OutputDebugString(buf);
218
#endif
219
#ifdef SEND_TO_CONSOLE
220
printf(buf);
221
printf("\r\n");
222
#endif
223
#endif
224
if (logFP) {
225
fprintf(logFP, "[%llu] ", getTimeStamp());
226
va_list args;
227
va_start(args, msg);
228
vfwprintf(logFP, msg, args);
229
va_end(args);
230
fprintf(logFP, "\r\n");
231
}
232
}
233
#ifdef __cplusplus
234
}
235
#endif
236
237