CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/LogReporting.cpp
Views: 1401
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <cstdarg>18#include <cstdio>19#include <mutex>20#include <unordered_map>21#include "Common/LogReporting.h"2223namespace Reporting {2425// Keeps track of report-only-once identifiers. Since they're always constants, a pointer is okay.26static std::unordered_map<const char *, int> logNTimes;27static std::mutex logNTimesLock;2829AllowedCallback allowedCallback = nullptr;30MessageCallback messageCallback = nullptr;3132bool ShouldLogNTimes(const char *identifier, int count) {33// True if it wasn't there already -> so yes, log.34std::lock_guard<std::mutex> lock(logNTimesLock);35auto iter = logNTimes.find(identifier);36if (iter == logNTimes.end()) {37logNTimes.emplace(identifier, 1);38return true;39} else {40if (iter->second >= count) {41return false;42} else {43iter->second++;44return true;45}46}47}4849void ResetCounts() {50std::lock_guard<std::mutex> lock(logNTimesLock);51logNTimes.clear();52}5354void SetupCallbacks(AllowedCallback allowed, MessageCallback message) {55allowedCallback = allowed;56messageCallback = message;57}5859void ReportMessage(const char *message, ...) {60if (!allowedCallback || !messageCallback) {61ERROR_LOG(Log::System, "Reporting not initialized, skipping: %s", message);62return;63}6465if (!allowedCallback())66return;6768const int MESSAGE_BUFFER_SIZE = 65536;69char temp[MESSAGE_BUFFER_SIZE];7071va_list args;72va_start(args, message);73vsnprintf(temp, MESSAGE_BUFFER_SIZE - 1, message, args);74temp[MESSAGE_BUFFER_SIZE - 1] = '\0';75va_end(args);7677messageCallback(message, temp);78}7980void ReportMessageFormatted(const char *message, const char *formatted) {81if (!allowedCallback || !messageCallback) {82ERROR_LOG(Log::System, "Reporting not initialized, skipping: %s", message);83return;84}8586if (!allowedCallback())87return;88messageCallback(message, formatted);89}9091} // namespace929394