all repos — mgba @ 4e39875e7b3b84ca48cb9c9ae1d8c2006b2e6724

mGBA Game Boy Advance Emulator

src/platform/qt/LogController.cpp (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "LogController.h"
  7
  8#include "ConfigController.h"
  9
 10using namespace QGBA;
 11
 12LogController LogController::s_global(mLOG_ALL);
 13
 14LogController::LogController(int levels, QObject* parent)
 15	: QObject(parent)
 16{
 17	mLogFilterInit(&m_filter);
 18	mLogFilterSet(&m_filter, "gba.bios", mLOG_STUB | mLOG_FATAL);
 19	mLogFilterSet(&m_filter, "core.status", mLOG_ALL & ~mLOG_DEBUG);
 20	m_filter.defaultLevels = levels;
 21
 22	if (this != &s_global) {
 23		connect(&s_global, &LogController::logPosted, this, &LogController::postLog);
 24		connect(this, static_cast<void (LogController::*)(int)>(&LogController::levelsSet), &s_global, static_cast<void (LogController::*)(int)>(&LogController::setLevels));
 25		connect(this, static_cast<void (LogController::*)(int)>(&LogController::levelsEnabled), &s_global, static_cast<void (LogController::*)(int)>(&LogController::enableLevels));
 26		connect(this, static_cast<void (LogController::*)(int)>(&LogController::levelsDisabled), &s_global, static_cast<void (LogController::*)(int)>(&LogController::disableLevels));
 27	}
 28}
 29
 30LogController::~LogController() {
 31	mLogFilterDeinit(&m_filter);
 32}
 33
 34int LogController::levels(int category) const {
 35	return mLogFilterLevels(&m_filter, category);
 36}
 37
 38LogController::Stream LogController::operator()(int category, int level) {
 39	return Stream(this, category, level);
 40}
 41
 42void LogController::load(const ConfigController* config) {
 43	mLogFilterLoad(&m_filter, config->config());
 44}
 45
 46void LogController::save(ConfigController* config) const {
 47	mLogFilterSave(&m_filter, config->config());
 48}
 49
 50void LogController::postLog(int level, int category, const QString& string) {
 51	if (!mLogFilterTest(&m_filter, category, static_cast<mLogLevel>(level))) {
 52		return;
 53	}
 54	emit logPosted(level, category, string);
 55}
 56
 57void LogController::setLevels(int levels) {
 58	m_filter.defaultLevels = levels;
 59	emit levelsSet(levels);
 60}
 61
 62void LogController::enableLevels(int levels) {
 63	m_filter.defaultLevels |= levels;
 64	emit levelsEnabled(levels);
 65}
 66
 67void LogController::disableLevels(int levels) {
 68	m_filter.defaultLevels &= ~levels;
 69	emit levelsDisabled(levels);
 70}
 71
 72void LogController::setLevels(int levels, int category) {
 73	auto id = mLogCategoryId(category);
 74	mLogFilterSet(&m_filter, id, levels);
 75	emit levelsSet(levels, category);
 76}
 77
 78void LogController::enableLevels(int levels, int category) {
 79	auto id = mLogCategoryId(category);
 80	int newLevels = mLogFilterLevels(&m_filter, category) | levels;
 81	mLogFilterSet(&m_filter, id, newLevels);
 82	emit levelsEnabled(levels, category);
 83}
 84
 85void LogController::disableLevels(int levels, int category) {
 86	auto id = mLogCategoryId(category);
 87	int newLevels = mLogFilterLevels(&m_filter, category) & ~levels;
 88	mLogFilterSet(&m_filter, id, newLevels);
 89	emit levelsDisabled(levels, category);
 90}
 91
 92void LogController::clearLevels(int category) {
 93	auto id = mLogCategoryId(category);
 94	mLogFilterReset	(&m_filter, id);
 95}
 96
 97LogController* LogController::global() {
 98	return &s_global;
 99}
100
101QString LogController::toString(int level) {
102	switch (level) {
103	case mLOG_DEBUG:
104		return tr("DEBUG");
105	case mLOG_STUB:
106		return tr("STUB");
107	case mLOG_INFO:
108		return tr("INFO");
109	case mLOG_WARN:
110		return tr("WARN");
111	case mLOG_ERROR:
112		return tr("ERROR");
113	case mLOG_FATAL:
114		return tr("FATAL");
115	case mLOG_GAME_ERROR:
116		return tr("GAME ERROR");
117	}
118	return QString();
119}
120
121LogController::Stream::Stream(LogController* controller, int level, int category)
122	: m_level(level)
123	, m_category(category)
124	, m_log(controller)
125{
126}
127
128LogController::Stream::~Stream() {
129	m_log->postLog(m_level, m_category, m_queue.join(" "));
130}
131
132LogController::Stream& LogController::Stream::operator<<(const QString& string) {
133	m_queue.append(string);
134	return *this;
135}