all repos — mgba @ 893fdd383f3f4f59de31ac617bc8a28d3f8059c6

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