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/LogReporting.cpp
Views: 1401
1
// Copyright (c) 2012- PPSSPP 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 git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <cstdarg>
19
#include <cstdio>
20
#include <mutex>
21
#include <unordered_map>
22
#include "Common/LogReporting.h"
23
24
namespace Reporting {
25
26
// Keeps track of report-only-once identifiers. Since they're always constants, a pointer is okay.
27
static std::unordered_map<const char *, int> logNTimes;
28
static std::mutex logNTimesLock;
29
30
AllowedCallback allowedCallback = nullptr;
31
MessageCallback messageCallback = nullptr;
32
33
bool ShouldLogNTimes(const char *identifier, int count) {
34
// True if it wasn't there already -> so yes, log.
35
std::lock_guard<std::mutex> lock(logNTimesLock);
36
auto iter = logNTimes.find(identifier);
37
if (iter == logNTimes.end()) {
38
logNTimes.emplace(identifier, 1);
39
return true;
40
} else {
41
if (iter->second >= count) {
42
return false;
43
} else {
44
iter->second++;
45
return true;
46
}
47
}
48
}
49
50
void ResetCounts() {
51
std::lock_guard<std::mutex> lock(logNTimesLock);
52
logNTimes.clear();
53
}
54
55
void SetupCallbacks(AllowedCallback allowed, MessageCallback message) {
56
allowedCallback = allowed;
57
messageCallback = message;
58
}
59
60
void ReportMessage(const char *message, ...) {
61
if (!allowedCallback || !messageCallback) {
62
ERROR_LOG(Log::System, "Reporting not initialized, skipping: %s", message);
63
return;
64
}
65
66
if (!allowedCallback())
67
return;
68
69
const int MESSAGE_BUFFER_SIZE = 65536;
70
char temp[MESSAGE_BUFFER_SIZE];
71
72
va_list args;
73
va_start(args, message);
74
vsnprintf(temp, MESSAGE_BUFFER_SIZE - 1, message, args);
75
temp[MESSAGE_BUFFER_SIZE - 1] = '\0';
76
va_end(args);
77
78
messageCallback(message, temp);
79
}
80
81
void ReportMessageFormatted(const char *message, const char *formatted) {
82
if (!allowedCallback || !messageCallback) {
83
ERROR_LOG(Log::System, "Reporting not initialized, skipping: %s", message);
84
return;
85
}
86
87
if (!allowedCallback())
88
return;
89
messageCallback(message, formatted);
90
}
91
92
} // namespace
93
94