all repos — mgba @ 3be77c162247f5380bd1ec226ab4bf7ab800efcb

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
 8using namespace QGBA;
 9
10LogController LogController::s_global(mLOG_ALL);
11
12LogController::LogController(int levels, QObject* parent)
13	: QObject(parent)
14{
15	mLogFilterInit(&m_filter);
16	mLogFilterSet(&m_filter, "gba.bios", mLOG_STUB | mLOG_FATAL);
17	mLogFilterSet(&m_filter, "core.status", mLOG_ALL & ~mLOG_DEBUG);
18	m_filter.defaultLevels = levels;
19
20	if (this != &s_global) {
21		connect(&s_global, &LogController::logPosted, this, &LogController::postLog);
22		connect(this, &LogController::levelsSet, &s_global, &LogController::setLevels);
23		connect(this, &LogController::levelsEnabled, &s_global, &LogController::enableLevels);
24		connect(this, &LogController::levelsDisabled, &s_global, &LogController::disableLevels);
25	}
26}
27
28LogController::~LogController() {
29	mLogFilterDeinit(&m_filter);
30}
31
32LogController::Stream LogController::operator()(int category, int level) {
33	return Stream(this, category, level);
34}
35
36void LogController::postLog(int level, int category, const QString& string) {
37	if (!mLogFilterTest(&m_filter, category, static_cast<mLogLevel>(level))) {
38		return;
39	}
40	emit logPosted(level, category, string);
41}
42
43void LogController::setLevels(int levels) {
44	m_filter.defaultLevels = levels;
45	emit levelsSet(levels);
46}
47
48void LogController::enableLevels(int levels) {
49	m_filter.defaultLevels |= levels;
50	emit levelsEnabled(levels);
51}
52
53void LogController::disableLevels(int levels) {
54	m_filter.defaultLevels &= ~levels;
55	emit levelsDisabled(levels);
56}
57
58LogController* LogController::global() {
59	return &s_global;
60}
61
62QString LogController::toString(int level) {
63	switch (level) {
64	case mLOG_DEBUG:
65		return tr("DEBUG");
66	case mLOG_STUB:
67		return tr("STUB");
68	case mLOG_INFO:
69		return tr("INFO");
70	case mLOG_WARN:
71		return tr("WARN");
72	case mLOG_ERROR:
73		return tr("ERROR");
74	case mLOG_FATAL:
75		return tr("FATAL");
76	case mLOG_GAME_ERROR:
77		return tr("GAME ERROR");
78	}
79	return QString();
80}
81
82LogController::Stream::Stream(LogController* controller, int level, int category)
83	: m_level(level)
84	, m_category(category)
85	, m_log(controller)
86{
87}
88
89LogController::Stream::~Stream() {
90	m_log->postLog(m_level, m_category, m_queue.join(" "));
91}
92
93LogController::Stream& LogController::Stream::operator<<(const QString& string) {
94	m_queue.append(string);
95	return *this;
96}