Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Log.cpp
3185 views
1
// Copyright (C) 2003 Dolphin Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official SVN repository and contact information can be found at
16
// http://code.google.com/p/dolphin-emu/
17
18
#include <string>
19
#include <mutex>
20
21
#include "ppsspp_config.h"
22
23
#include "Common/CommonTypes.h"
24
#include "Common/Log.h"
25
#include "StringUtils.h"
26
#include "Common/Data/Encoding/Utf8.h"
27
#include "Common/Thread/ThreadUtil.h"
28
#include "Common/TimeUtil.h"
29
30
#if PPSSPP_PLATFORM(ANDROID)
31
#include <android/log.h>
32
#elif PPSSPP_PLATFORM(WINDOWS)
33
#include "CommonWindows.h"
34
static HWND g_dialogParent;
35
#endif
36
37
#define LOG_BUF_SIZE 2048
38
39
static bool hitAnyAsserts = false;
40
41
static std::mutex g_extraAssertInfoMutex;
42
static std::string g_extraAssertInfo = "menu";
43
static double g_assertInfoTime = 0.0;
44
static bool g_exitOnAssert;
45
static AssertNoCallbackFunc g_assertCancelCallback = 0;
46
static void *g_assertCancelCallbackUserData = 0;
47
48
u8 g_debugCounters[8];
49
50
void SetDebugValue(DebugCounter counter, int value) {
51
if (value > 15)
52
value = 15;
53
if (value < 0)
54
value = 0;
55
g_debugCounters[(int)counter] = value;
56
}
57
58
void IncrementDebugCounter(DebugCounter counter) {
59
int value = g_debugCounters[(int)counter] + 1;
60
if (value > 15)
61
value = 15;
62
if (value < 0)
63
value = 0;
64
g_debugCounters[(int)counter] = value;
65
}
66
67
void SetAssertDialogParent(void *handle) {
68
#if PPSSPP_PLATFORM(WINDOWS)
69
// I thought this would be nice, but for some reason the dialog becomes invisible.
70
// g_dialogParent = (HWND)handle;
71
#endif
72
}
73
74
void SetExtraAssertInfo(const char *info) {
75
std::lock_guard<std::mutex> guard(g_extraAssertInfoMutex);
76
g_extraAssertInfo = info ? info : "menu";
77
g_assertInfoTime = time_now_d();
78
}
79
80
void SetAssertCancelCallback(AssertNoCallbackFunc callback, void *userdata) {
81
g_assertCancelCallback = callback;
82
g_assertCancelCallbackUserData = userdata;
83
}
84
85
void SetCleanExitOnAssert() {
86
g_exitOnAssert = true;
87
}
88
89
void BreakIntoPSPDebugger(const char *reason) {
90
if (g_assertCancelCallback) {
91
g_assertCancelCallback(reason, g_assertCancelCallbackUserData);
92
}
93
}
94
95
bool HandleAssert(const char *function, const char *file, int line, const char *expression, const char* format, ...) {
96
// Read message and write it to the log
97
char text[LOG_BUF_SIZE];
98
const char *caption = "Critical";
99
va_list args;
100
va_start(args, format);
101
vsnprintf(text, sizeof(text), format, args);
102
va_end(args);
103
104
// Secondary formatting. Wonder if this can be combined into the vsnprintf somehow.
105
char formatted[LOG_BUF_SIZE + 128];
106
{
107
std::lock_guard<std::mutex> guard(g_extraAssertInfoMutex);
108
double delta = time_now_d() - g_assertInfoTime;
109
u32 debugCounters = 0;
110
for (int i = 0; i < 8; i++) {
111
debugCounters |= g_debugCounters[7 - i] << (i * 4);
112
}
113
snprintf(formatted, sizeof(formatted), "(%s:%s:%d:%08x): [%s] (%s, %0.1fs) %s", file, function, line, debugCounters, expression, g_extraAssertInfo.c_str(), delta, text);
114
}
115
116
// Normal logging (will also log to Android log)
117
ERROR_LOG(Log::System, "%s", formatted);
118
// Also do a simple printf for good measure, in case logging of System is disabled (should we disallow that?)
119
fprintf(stderr, "%s\n", formatted);
120
121
hitAnyAsserts = true;
122
123
#if defined(USING_WIN_UI)
124
// Avoid hanging on CI.
125
if (!getenv("CI")) {
126
const int msgBoxStyle = MB_ICONINFORMATION | MB_YESNOCANCEL;
127
std::string text = formatted;
128
text += "\n\nTry to continue?";
129
if (IsDebuggerPresent()) {
130
text += "\n\nNo: break directly into the native debugger";
131
text += "\n\nCancel: skip and break into PPSSPP debugger";
132
} else {
133
text += "\n\nNo: exit";
134
text += "\n\nCancel: skip and break into PPSSPP debugger";
135
}
136
const char *threadName = GetCurrentThreadName();
137
OutputDebugStringA(formatted);
138
printf("%s\n", formatted);
139
std::wstring wcaption = ConvertUTF8ToWString(std::string(caption) + " " + (threadName ? threadName : "(unknown thread)"));
140
switch (MessageBox(g_dialogParent, ConvertUTF8ToWString(text).c_str(), wcaption.c_str(), msgBoxStyle)) {
141
case IDYES:
142
return true;
143
case IDNO:
144
if (g_exitOnAssert || !IsDebuggerPresent()) {
145
// Hard exit.
146
ExitProcess(1);
147
}
148
return false; // Break into the native debugger.
149
case IDCANCEL:
150
g_assertCancelCallback(formatted, g_assertCancelCallbackUserData);
151
return true; // don't crash!
152
}
153
}
154
return false;
155
#elif PPSSPP_PLATFORM(ANDROID)
156
__android_log_assert(expression, "PPSSPP", "%s", formatted);
157
// Doesn't matter what we return here.
158
return false;
159
#else
160
OutputDebugStringUTF8(text);
161
return false;
162
#endif
163
}
164
165
// These are mainly used for unit testing.
166
bool HitAnyAsserts() {
167
return hitAnyAsserts;
168
}
169
void ResetHitAnyAsserts() {
170
hitAnyAsserts = false;
171
}
172
173