all repos — mgba @ 56c6e3b497b45e59982f2cae19740d5fbe18bedc

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);
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::Stream LogController::operator()(int category, int level) {
29	return Stream(this, category, level);
30}
31
32void LogController::postLog(int level, int category, const QString& string) {
33	if (!mLogFilterTest(&m_filter, category, static_cast<mLogLevel>(level))) {
34		return;
35	}
36	emit logPosted(level, category, string);
37}
38
39void LogController::setLevels(int levels) {
40	m_filter.defaultLevels = levels;
41	emit levelsSet(levels);
42}
43
44void LogController::enableLevels(int levels) {
45	m_filter.defaultLevels |= levels;
46	emit levelsEnabled(levels);
47}
48
49void LogController::disableLevels(int levels) {
50	m_filter.defaultLevels &= ~levels;
51	emit levelsDisabled(levels);
52}
53
54LogController* LogController::global() {
55	return &s_global;
56}
57
58QString LogController::toString(int level) {
59	switch (level) {
60	case mLOG_DEBUG:
61		return tr("DEBUG");
62	case mLOG_STUB:
63		return tr("STUB");
64	case mLOG_INFO:
65		return tr("INFO");
66	case mLOG_WARN:
67		return tr("WARN");
68	case mLOG_ERROR:
69		return tr("ERROR");
70	case mLOG_FATAL:
71		return tr("FATAL");
72	case mLOG_GAME_ERROR:
73		return tr("GAME ERROR");
74	}
75	return QString();
76}
77
78LogController::Stream::Stream(LogController* controller, int level, int category)
79	: m_level(level)
80	, m_category(category)
81	, m_log(controller)
82{
83}
84
85LogController::Stream::~Stream() {
86	m_log->postLog(m_level, m_category, m_queue.join(" "));
87}
88
89LogController::Stream& LogController::Stream::operator<<(const QString& string) {
90	m_queue.append(string);
91	return *this;
92}