MyGUI 3.4.1
MyGUI_LogManager.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
8#include "MyGUI_LogManager.h"
12#include <ctime>
13
14namespace MyGUI
15{
16
17 LogManager* LogManager::msInstance = nullptr;
18
20 mConsole(nullptr),
21 mFile(nullptr),
22 mFilter(nullptr),
23 mDefaultSource(nullptr),
24 mLevel(LogLevel::Info),
25 mConsoleEnable(true)
26 {
27 msInstance = this;
28 }
29
31 {
32 flush();
33 close();
34
35 delete mDefaultSource;
36 mDefaultSource = nullptr;
37 delete mConsole;
38 mConsole = nullptr;
39 delete mFile;
40 mFile = nullptr;
41 delete mFilter;
42 mFilter = nullptr;
43
44 msInstance = nullptr;
45 }
46
48 {
49 MYGUI_ASSERT(nullptr != getInstancePtr(), "Singleton instance LogManager was not created");
50 return *msInstance;
51 }
52
54 {
55 return msInstance;
56 }
57
59 {
60 for (VectorLogSource::iterator item = mSources.begin(); item != mSources.end(); ++item)
61 (*item)->flush();
62 }
63
64 void LogManager::log(const std::string& _section, LogLevel _level, const std::string& _message, const char* _file, int _line)
65 {
66 time_t ctTime;
67 time(&ctTime);
68 struct tm* currentTime;
69 currentTime = localtime(&ctTime);
70
71 for (VectorLogSource::iterator item = mSources.begin(); item != mSources.end(); ++item)
72 (*item)->log(_section, _level, currentTime, _message, _file, _line);
73 }
74
75 void LogManager::close()
76 {
77 for (VectorLogSource::iterator item = mSources.begin(); item != mSources.end(); ++item)
78 (*item)->close();
79 }
80
82 {
83 mSources.push_back(_source);
84 }
85
86 void LogManager::createDefaultSource(const std::string& _logname)
87 {
88 mDefaultSource = new LogSource();
89
90 mConsole = new ConsoleLogListener();
91 mConsole->setEnabled(mConsoleEnable);
92 mDefaultSource->addLogListener(mConsole);
93
94#ifndef EMSCRIPTEN
95 mFile = new FileLogListener();
96 mFile->setFileName(_logname);
97 mDefaultSource->addLogListener(mFile);
98#endif
99
100 mFilter = new LevelLogFilter();
101 mFilter->setLoggingLevel(mLevel);
102 mDefaultSource->setLogFilter(mFilter);
103
104 mDefaultSource->open();
105
106 LogManager::getInstance().addLogSource(mDefaultSource);
107 }
108
110 {
111 mConsoleEnable = _value;
112
113 if (mConsole != nullptr)
114 mConsole->setEnabled(_value);
115 }
116
118 {
119 return mConsoleEnable;
120 }
121
123 {
124 mLevel = _value;
125
126 if (mFilter != nullptr)
127 mFilter->setLoggingLevel(_value);
128 }
129
131 {
132 return mLevel;
133 }
134
135} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
void setFileName(const std::string &_value)
void setLoggingLevel(LogLevel _value)
void setSTDOutputEnabled(bool _value)
void addLogSource(LogSource *_source)
static LogManager * getInstancePtr()
LogLevel getLoggingLevel() const
void createDefaultSource(const std::string &_logname)
void setLoggingLevel(LogLevel _value)
static LogManager & getInstance()
void log(const std::string &_section, LogLevel _level, const std::string &_message, const char *_file, int _line)
bool getSTDOutputEnabled() const
void setLogFilter(ILogFilter *_filter)
void addLogListener(ILogListener *_lestener)