CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Log.cpp
Views: 1401
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
#endif
35
36
#define LOG_BUF_SIZE 2048
37
38
static bool hitAnyAsserts = false;
39
40
static std::mutex g_extraAssertInfoMutex;
41
static std::string g_extraAssertInfo = "menu";
42
static double g_assertInfoTime = 0.0;
43
static bool g_exitOnAssert;
44
45
void SetExtraAssertInfo(const char *info) {
46
std::lock_guard<std::mutex> guard(g_extraAssertInfoMutex);
47
g_extraAssertInfo = info ? info : "menu";
48
g_assertInfoTime = time_now_d();
49
}
50
51
void SetCleanExitOnAssert() {
52
g_exitOnAssert = true;
53
}
54
55
bool HandleAssert(const char *function, const char *file, int line, const char *expression, const char* format, ...) {
56
// Read message and write it to the log
57
char text[LOG_BUF_SIZE];
58
const char *caption = "Critical";
59
va_list args;
60
va_start(args, format);
61
vsnprintf(text, sizeof(text), format, args);
62
va_end(args);
63
64
// Secondary formatting. Wonder if this can be combined into the vsnprintf somehow.
65
char formatted[LOG_BUF_SIZE + 128];
66
{
67
std::lock_guard<std::mutex> guard(g_extraAssertInfoMutex);
68
double delta = time_now_d() - g_assertInfoTime;
69
snprintf(formatted, sizeof(formatted), "(%s:%s:%d): [%s] (%s, %0.1fs) %s", file, function, line, expression, g_extraAssertInfo.c_str(), delta, text);
70
}
71
72
// Normal logging (will also log to Android log)
73
ERROR_LOG(Log::System, "%s", formatted);
74
// Also do a simple printf for good measure, in case logging of System is disabled (should we disallow that?)
75
fprintf(stderr, "%s\n", formatted);
76
77
hitAnyAsserts = true;
78
79
#if defined(USING_WIN_UI)
80
// Avoid hanging on CI.
81
if (!getenv("CI")) {
82
int msgBoxStyle = MB_ICONINFORMATION | MB_YESNO;
83
std::wstring wtext = ConvertUTF8ToWString(formatted) + L"\n\nTry to continue?";
84
std::wstring wcaption = ConvertUTF8ToWString(std::string(caption) + " " + GetCurrentThreadName());
85
OutputDebugString(wtext.c_str());
86
printf("%s\n", formatted);
87
if (IDYES != MessageBox(0, wtext.c_str(), wcaption.c_str(), msgBoxStyle)) {
88
if (g_exitOnAssert) {
89
// Hard exit.
90
ExitProcess(1);
91
return false;
92
}
93
} else {
94
return true;
95
}
96
}
97
return false;
98
#elif PPSSPP_PLATFORM(ANDROID)
99
__android_log_assert(expression, "PPSSPP", "%s", formatted);
100
// Doesn't matter what we return here.
101
return false;
102
#else
103
OutputDebugStringUTF8(text);
104
return false;
105
#endif
106
}
107
108
// These are mainly used for unit testing.
109
bool HitAnyAsserts() {
110
return hitAnyAsserts;
111
}
112
void ResetHitAnyAsserts() {
113
hitAnyAsserts = false;
114
}
115
116