2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
2013-04-18 09:58:54 +00:00
|
|
|
#pragma once
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-02-28 00:47:13 +00:00
|
|
|
#include "ppsspp_config.h"
|
|
|
|
|
2017-02-27 20:57:46 +00:00
|
|
|
#include <mutex>
|
2020-08-10 02:20:07 +00:00
|
|
|
#include <vector>
|
2020-12-04 20:24:18 +00:00
|
|
|
#include <cstdarg>
|
2021-05-09 22:56:38 +00:00
|
|
|
#include <cstdio>
|
2017-03-17 17:00:24 +00:00
|
|
|
|
2020-10-03 22:25:21 +00:00
|
|
|
#include "Common/Data/Format/IniFile.h"
|
2020-08-16 12:11:56 +00:00
|
|
|
#include "Common/CommonFuncs.h"
|
2020-08-10 02:40:30 +00:00
|
|
|
#include "Common/Log.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
#define MAX_MESSAGES 8000
|
|
|
|
|
2013-03-11 05:25:03 +00:00
|
|
|
extern const char *hleCurrentThreadName;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2017-03-18 09:47:10 +00:00
|
|
|
// Struct that listeners can output how they want. For example, on Android we don't want to add
|
|
|
|
// timestamp or write the level as a string, those already exist.
|
|
|
|
struct LogMessage {
|
2018-04-22 00:31:29 +00:00
|
|
|
char timestamp[16];
|
|
|
|
char header[64]; // Filename/thread/etc. in front.
|
2017-03-18 09:47:10 +00:00
|
|
|
LogTypes::LOG_LEVELS level;
|
|
|
|
const char *log;
|
|
|
|
std::string msg; // The actual log message.
|
|
|
|
};
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// pure virtual interface
|
2013-09-07 10:34:19 +00:00
|
|
|
class LogListener {
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
|
|
|
virtual ~LogListener() {}
|
|
|
|
|
2017-03-18 09:47:10 +00:00
|
|
|
virtual void Log(const LogMessage &msg) = 0;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
class FileLogListener : public LogListener {
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
|
|
|
FileLogListener(const char *filename);
|
2021-05-09 22:56:38 +00:00
|
|
|
~FileLogListener();
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2022-12-11 04:32:12 +00:00
|
|
|
void Log(const LogMessage &msg) override;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2021-05-09 22:56:38 +00:00
|
|
|
bool IsValid() { if (!fp_) return false; else return true; }
|
2012-11-01 15:19:01 +00:00
|
|
|
bool IsEnabled() const { return m_enable; }
|
2017-03-18 09:47:10 +00:00
|
|
|
void SetEnabled(bool enable) { m_enable = enable; }
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2021-05-09 22:56:38 +00:00
|
|
|
const char *GetName() const { return "file"; }
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
private:
|
2017-02-27 20:57:46 +00:00
|
|
|
std::mutex m_log_lock;
|
2021-05-09 22:56:38 +00:00
|
|
|
FILE *fp_ = nullptr;
|
2012-11-01 15:19:01 +00:00
|
|
|
bool m_enable;
|
|
|
|
};
|
|
|
|
|
2017-03-08 13:20:15 +00:00
|
|
|
class OutputDebugStringLogListener : public LogListener {
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
2022-12-11 04:32:12 +00:00
|
|
|
void Log(const LogMessage &msg) override;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
2015-01-05 00:23:03 +00:00
|
|
|
class RingbufferLogListener : public LogListener {
|
|
|
|
public:
|
2022-12-11 04:32:12 +00:00
|
|
|
void Log(const LogMessage &msg) override;
|
2015-01-05 00:23:03 +00:00
|
|
|
|
|
|
|
bool IsEnabled() const { return enabled_; }
|
2017-03-18 09:47:10 +00:00
|
|
|
void SetEnabled(bool enable) { enabled_ = enable; }
|
2015-01-05 00:23:03 +00:00
|
|
|
|
|
|
|
int GetCount() const { return count_ < MAX_LOGS ? count_ : MAX_LOGS; }
|
2017-03-18 09:47:10 +00:00
|
|
|
const char *TextAt(int i) const { return messages_[(curMessage_ - i - 1) & (MAX_LOGS - 1)].msg.c_str(); }
|
|
|
|
LogTypes::LOG_LEVELS LevelAt(int i) const { return messages_[(curMessage_ - i - 1) & (MAX_LOGS - 1)].level; }
|
2015-01-05 00:23:03 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
enum { MAX_LOGS = 128 };
|
2017-03-18 09:47:10 +00:00
|
|
|
LogMessage messages_[MAX_LOGS];
|
2022-12-11 04:32:12 +00:00
|
|
|
int curMessage_ = 0;
|
|
|
|
int count_ = 0;
|
|
|
|
bool enabled_ = false;
|
2015-01-05 00:23:03 +00:00
|
|
|
};
|
|
|
|
|
2013-09-07 11:38:37 +00:00
|
|
|
// TODO: A simple buffered log that can be used to display the log in-window
|
|
|
|
// on Android etc.
|
|
|
|
// class BufferedLogListener { ... }
|
|
|
|
|
2017-03-18 09:47:10 +00:00
|
|
|
struct LogChannel {
|
|
|
|
char m_shortName[32]{};
|
|
|
|
LogTypes::LOG_LEVELS level;
|
|
|
|
bool enabled;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ConsoleListener;
|
|
|
|
|
2017-08-31 15:13:18 +00:00
|
|
|
class LogManager {
|
2017-03-08 13:20:15 +00:00
|
|
|
private:
|
2020-08-15 16:40:50 +00:00
|
|
|
LogManager(bool *enabledSetting);
|
2017-03-08 13:20:15 +00:00
|
|
|
~LogManager();
|
|
|
|
|
2017-08-31 15:13:18 +00:00
|
|
|
// Prevent copies.
|
|
|
|
LogManager(const LogManager &) = delete;
|
|
|
|
void operator=(const LogManager &) = delete;
|
|
|
|
|
2017-03-08 13:20:15 +00:00
|
|
|
LogChannel log_[LogTypes::NUMBER_OF_LOGS];
|
|
|
|
FileLogListener *fileLog_ = nullptr;
|
|
|
|
ConsoleListener *consoleLog_ = nullptr;
|
|
|
|
OutputDebugStringLogListener *debuggerLog_ = nullptr;
|
|
|
|
RingbufferLogListener *ringLog_ = nullptr;
|
|
|
|
static LogManager *logManager_; // Singleton. Ugh.
|
|
|
|
|
|
|
|
std::mutex listeners_lock_;
|
|
|
|
std::vector<LogListener*> listeners_;
|
2020-08-15 16:40:50 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
2017-03-18 09:47:10 +00:00
|
|
|
void AddListener(LogListener *listener);
|
|
|
|
void RemoveListener(LogListener *listener);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
static u32 GetMaxLevel() { return MAX_LOGLEVEL; }
|
2013-09-07 11:38:37 +00:00
|
|
|
static int GetNumChannels() { return LogTypes::NUMBER_OF_LOGS; }
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
void Log(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type,
|
|
|
|
const char *file, int line, const char *fmt, va_list args);
|
2015-03-22 07:12:08 +00:00
|
|
|
bool IsEnabled(LogTypes::LOG_LEVELS level, LogTypes::LOG_TYPE type);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-09-07 11:38:37 +00:00
|
|
|
LogChannel *GetLogChannel(LogTypes::LOG_TYPE type) {
|
2017-03-18 09:47:10 +00:00
|
|
|
return &log_[type];
|
2013-09-07 11:38:37 +00:00
|
|
|
}
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
void SetLogLevel(LogTypes::LOG_TYPE type, LogTypes::LOG_LEVELS level) {
|
2017-03-18 09:47:10 +00:00
|
|
|
log_[type].level = level;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-01 02:44:05 +00:00
|
|
|
void SetAllLogLevels(LogTypes::LOG_LEVELS level) {
|
|
|
|
for (int i = 0; i < LogTypes::NUMBER_OF_LOGS; ++i) {
|
2017-03-18 09:47:10 +00:00
|
|
|
log_[i].level = level;
|
2014-04-01 02:44:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 09:47:10 +00:00
|
|
|
void SetEnabled(LogTypes::LOG_TYPE type, bool enable) {
|
|
|
|
log_[type].enabled = enable;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
LogTypes::LOG_LEVELS GetLogLevel(LogTypes::LOG_TYPE type) {
|
2017-03-18 09:47:10 +00:00
|
|
|
return log_[type].level;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
ConsoleListener *GetConsoleListener() const {
|
2013-09-07 11:38:37 +00:00
|
|
|
return consoleLog_;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:20:15 +00:00
|
|
|
OutputDebugStringLogListener *GetDebuggerListener() const {
|
2013-09-07 11:38:37 +00:00
|
|
|
return debuggerLog_;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2015-01-05 00:23:03 +00:00
|
|
|
RingbufferLogListener *GetRingbufferListener() const {
|
|
|
|
return ringLog_;
|
|
|
|
}
|
|
|
|
|
2015-03-22 07:12:08 +00:00
|
|
|
static inline LogManager* GetInstance() {
|
2013-09-07 11:38:37 +00:00
|
|
|
return logManager_;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
static void SetInstance(LogManager *logManager) {
|
2013-09-07 11:38:37 +00:00
|
|
|
logManager_ = logManager;
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 16:40:50 +00:00
|
|
|
static void Init(bool *enabledSetting);
|
2012-11-01 15:19:01 +00:00
|
|
|
static void Shutdown();
|
|
|
|
|
2012-12-22 17:49:29 +00:00
|
|
|
void ChangeFileLog(const char *filename);
|
|
|
|
|
2020-08-10 13:53:52 +00:00
|
|
|
void SaveConfig(Section *section);
|
2022-12-11 05:02:44 +00:00
|
|
|
void LoadConfig(const Section *section, bool debugDefaults);
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|