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 #pragma once
7 
8 #include "GBAApp.h"
9 
10 #include <mgba/core/log.h>
11 
12 #include <QObject>
13 #include <QStringList>
14 #include <QTextStream>
15 #include <memory>
16 
17 namespace QGBA {
18 
19 class ConfigController;
20 
21 class LogController : public QObject {
22 Q_OBJECT
23 
24 private:
25 	class Stream {
26 	public:
27 		Stream(LogController* controller, int level, int category);
28 		~Stream();
29 
30 		Stream& operator<<(const QString&);
31 
32 	private:
33 		int m_level;
34 		int m_category;
35 		LogController* m_log;
36 
37 		QStringList m_queue;
38 	};
39 
40 public:
41 	LogController(int levels, QObject* parent = nullptr);
42 	~LogController();
43 
levels()44 	int levels() const { return m_filter.defaultLevels; }
45 	int levels(int category) const;
filter()46 	mLogFilter* filter() { return &m_filter; }
47 
48 	Stream operator()(int category, int level);
49 
50 	static LogController* global();
51 	static QString toString(int level);
52 	static int categoryId(const char*);
53 
54 	void load(const ConfigController*);
55 	void save(ConfigController*) const;
56 
57 signals:
58 	void logPosted(int level, int category, const QString& log);
59 	void levelsSet(int levels);
60 	void levelsEnabled(int levels);
61 	void levelsDisabled(int levels);
62 	void levelsSet(int levels, int category);
63 	void levelsEnabled(int levels, int category);
64 	void levelsDisabled(int levels, int category);
65 
66 public slots:
67 	void postLog(int level, int category, const QString& string);
68 	void setLevels(int levels);
69 	void enableLevels(int levels);
70 	void disableLevels(int levels);
71 	void setLevels(int levels, int category);
72 	void enableLevels(int levels, int category);
73 	void disableLevels(int levels, int category);
74 	void clearLevels(int category);
75 
76 	void logToFile(bool);
77 	void logToStdout(bool);
78 	void setLogFile(const QString&);
79 
80 private:
81 	mLogFilter m_filter;
82 	bool m_logToFile;
83 	bool m_logToStdout;
84 	std::unique_ptr<QFile> m_logFile;
85 	std::unique_ptr<QTextStream> m_logStream;
86 
87 	static LogController s_global;
88 	static int s_qtCat;
89 };
90 
91 #define LOG(C, L) (*LogController::global())(mLOG_ ## L, _mLOG_CAT_ ## C)
92 
93 }
94