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
|
|
|
|
|
|
|
#include "Log.h"
|
2013-05-03 06:22:15 +00:00
|
|
|
#include "StringUtils.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include "FileUtil.h"
|
2013-04-18 09:58:54 +00:00
|
|
|
#include "file/ini_file.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
#include <set>
|
2013-11-12 17:52:12 +00:00
|
|
|
#include "StdMutex.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
#define MAX_MESSAGES 8000
|
|
|
|
#define MAX_MSGLEN 1024
|
|
|
|
|
2013-03-11 05:25:03 +00:00
|
|
|
extern const char *hleCurrentThreadName;
|
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() {}
|
|
|
|
|
|
|
|
virtual void Log(LogTypes::LOG_LEVELS, const char *msg) = 0;
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
void Log(LogTypes::LOG_LEVELS, const char *msg);
|
|
|
|
|
2013-02-04 19:36:43 +00:00
|
|
|
bool IsValid() { if (!m_logfile) return false; else return true; }
|
2012-11-01 15:19:01 +00:00
|
|
|
bool IsEnabled() const { return m_enable; }
|
|
|
|
void SetEnable(bool enable) { m_enable = enable; }
|
|
|
|
|
|
|
|
const char* GetName() const { return "file"; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::mutex m_log_lock;
|
|
|
|
std::ofstream m_logfile;
|
|
|
|
bool m_enable;
|
|
|
|
};
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
class DebuggerLogListener : public LogListener {
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
|
|
|
void Log(LogTypes::LOG_LEVELS, const char *msg);
|
|
|
|
};
|
|
|
|
|
2015-01-05 00:23:03 +00:00
|
|
|
class RingbufferLogListener : public LogListener {
|
|
|
|
public:
|
|
|
|
RingbufferLogListener() : curMessage_(0), count_(0), enabled_(false) {}
|
|
|
|
void Log(LogTypes::LOG_LEVELS, const char *msg);
|
|
|
|
|
|
|
|
bool IsEnabled() const { return enabled_; }
|
|
|
|
void SetEnable(bool enable) { enabled_ = enable; }
|
|
|
|
|
|
|
|
int GetCount() const { return count_ < MAX_LOGS ? count_ : MAX_LOGS; }
|
|
|
|
const char *TextAt(int i) { return messages_[(curMessage_ - i - 1) & (MAX_LOGS - 1)]; }
|
|
|
|
LogTypes::LOG_LEVELS LevelAt(int i) { return (LogTypes::LOG_LEVELS)levels_[(curMessage_ - i - 1) & (MAX_LOGS - 1)]; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
enum { MAX_LOGS = 128 };
|
|
|
|
char messages_[MAX_LOGS][1024];
|
|
|
|
u8 levels_[MAX_LOGS];
|
|
|
|
int curMessage_;
|
|
|
|
int count_;
|
|
|
|
bool enabled_;
|
|
|
|
};
|
|
|
|
|
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 { ... }
|
|
|
|
|
|
|
|
class LogChannel {
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
2013-09-07 11:38:37 +00:00
|
|
|
LogChannel(const char* shortName, const char* fullName, bool enable = false);
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
const char* GetShortName() const { return m_shortName; }
|
|
|
|
const char* GetFullName() const { return m_fullName; }
|
|
|
|
|
|
|
|
void AddListener(LogListener* listener);
|
|
|
|
void RemoveListener(LogListener* listener);
|
|
|
|
|
|
|
|
void Trigger(LogTypes::LOG_LEVELS, const char *msg);
|
|
|
|
|
2013-09-07 11:38:37 +00:00
|
|
|
bool IsEnabled() const { return enable_; }
|
|
|
|
void SetEnable(bool enable) { enable_ = enable; }
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-09-07 11:38:37 +00:00
|
|
|
LogTypes::LOG_LEVELS GetLevel() const { return (LogTypes::LOG_LEVELS)level_; }
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-09-07 11:38:37 +00:00
|
|
|
void SetLevel(LogTypes::LOG_LEVELS level) { level_ = level; }
|
2014-03-03 06:03:02 +00:00
|
|
|
bool HasListeners() const { return m_hasListeners; }
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-09-07 11:38:37 +00:00
|
|
|
// Although not elegant, easy to set with a PopupMultiChoice...
|
|
|
|
int level_;
|
|
|
|
bool enable_;
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
private:
|
|
|
|
char m_fullName[128];
|
|
|
|
char m_shortName[32];
|
|
|
|
std::mutex m_listeners_lock;
|
|
|
|
std::set<LogListener*> m_listeners;
|
2014-03-03 06:03:02 +00:00
|
|
|
bool m_hasListeners;
|
2012-11-01 15:19:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class ConsoleListener;
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
class LogManager : NonCopyable {
|
2012-11-01 15:19:01 +00:00
|
|
|
private:
|
2013-09-07 11:38:37 +00:00
|
|
|
LogChannel* log_[LogTypes::NUMBER_OF_LOGS];
|
|
|
|
FileLogListener *fileLog_;
|
|
|
|
ConsoleListener *consoleLog_;
|
|
|
|
DebuggerLogListener *debuggerLog_;
|
2015-01-05 00:23:03 +00:00
|
|
|
RingbufferLogListener *ringLog_;
|
2013-09-07 11:38:37 +00:00
|
|
|
static LogManager *logManager_; // Singleton. Ugh.
|
|
|
|
std::mutex log_lock_;
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
LogManager();
|
|
|
|
~LogManager();
|
2013-09-07 11:38:37 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
public:
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2013-09-07 11:38:37 +00:00
|
|
|
LogChannel *GetLogChannel(LogTypes::LOG_TYPE type) {
|
|
|
|
return log_[type];
|
|
|
|
}
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
void SetLogLevel(LogTypes::LOG_TYPE type, LogTypes::LOG_LEVELS level) {
|
2013-09-07 11:38:37 +00:00
|
|
|
log_[type]->SetLevel(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) {
|
|
|
|
log_[i]->SetLevel(level);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
void SetEnable(LogTypes::LOG_TYPE type, bool enable) {
|
2013-09-07 11:38:37 +00:00
|
|
|
log_[type]->SetEnable(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) {
|
2013-09-07 11:38:37 +00:00
|
|
|
return log_[type]->GetLevel();
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
void AddListener(LogTypes::LOG_TYPE type, LogListener *listener) {
|
2013-09-07 11:38:37 +00:00
|
|
|
log_[type]->AddListener(listener);
|
2012-11-01 15:19:01 +00:00
|
|
|
}
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
void RemoveListener(LogTypes::LOG_TYPE type, LogListener *listener) {
|
2013-09-07 11:38:37 +00:00
|
|
|
log_[type]->RemoveListener(listener);
|
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
|
|
|
}
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
DebuggerLogListener *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_;
|
|
|
|
}
|
|
|
|
|
2013-09-07 10:34:19 +00:00
|
|
|
static 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
|
|
|
}
|
|
|
|
|
|
|
|
static void Init();
|
|
|
|
static void Shutdown();
|
|
|
|
|
2012-12-22 17:49:29 +00:00
|
|
|
void ChangeFileLog(const char *filename);
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
void SaveConfig(IniFile::Section *section);
|
|
|
|
void LoadConfig(IniFile::Section *section);
|
|
|
|
};
|