MyGUI 3.2.0

- Main Page
- Related Pages
- Namespaces
- Data Structures
- Files
- Examples

- File List
- Globals

MyGUI_LogManager.cpp
Go to the documentation of this file.
1
6 /*
7 This file is part of MyGUI.
8
9 MyGUI is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 MyGUI is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
21 */
22 #include "MyGUI_Precompiled.h"
23 #include "MyGUI_LogManager.h"
24 #include "MyGUI_FileLogListener.h"
25 #include "MyGUI_ConsoleLogListener.h"
26 #include "MyGUI_LevelLogFilter.h"
27 #include "MyGUI_LogSource.h"
28 #include <time.h>
29
30 namespace MyGUI
31 {
32
33 LogManager* LogManager::msInstance = nullptr;
34
35 LogManager::LogManager() :
36 mConsole(nullptr),
37 mFile(nullptr),
38 mFilter(nullptr),
39 mDefaultSource(nullptr),
40 mLevel(LogLevel::Info),
41 mConsoleEnable(true)
42 {
43 msInstance = this;
44 }
45
46 LogManager::~LogManager()
47 {
48 flush();
49 close();
50
51 delete mDefaultSource;
52 mDefaultSource = nullptr;
53 delete mConsole;
54 mConsole = nullptr;
55 delete mFile;
56 mFile = nullptr;
57 delete mFilter;
58 mFilter = nullptr;
59
60 msInstance = nullptr;
61 }
62
63 LogManager& LogManager::getInstance()
64 {
65 if (msInstance == nullptr)
66 {
67 MYGUI_DBG_BREAK;
68 MYGUI_BASE_EXCEPT("Singleton instance LogManager was not created", "MyGUI");
69 }
70 return *msInstance;
71 }
72
73 LogManager* LogManager::getInstancePtr()
74 {
75 return msInstance;
76 }
77
78 void LogManager::flush()
79 {
80 for (VectorLogSource::iterator item = mSources.begin(); item != mSources.end(); ++item)
81 (*item)->flush();
82 }
83
84 void LogManager::log(const std::string& _section, LogLevel _level, const std::string& _message, const char* _file, int _line)
85 {
86 time_t ctTime;
87 time(&ctTime);
88 struct tm* currentTime;
89 currentTime = localtime(&ctTime);
90
91 for (VectorLogSource::iterator item = mSources.begin(); item != mSources.end(); ++item)
92 (*item)->log(_section, _level, currentTime, _message, _file, _line);
93 }
94
95 void LogManager::close()
96 {
97 for (VectorLogSource::iterator item = mSources.begin(); item != mSources.end(); ++item)
98 (*item)->close();
99 }
100
101 void LogManager::addLogSource(LogSource* _source)
102 {
103 mSources.push_back(_source);
104 }
105
106 void LogManager::createDefaultSource(const std::string& _logname)
107 {
108 mConsole = new ConsoleLogListener();
109 mFile = new FileLogListener();
110 mFilter = new LevelLogFilter();
111
112 mFile->setFileName(_logname);
113 mConsole->setEnabled(mConsoleEnable);
114 mFilter->setLoggingLevel(mLevel);
115
116 mDefaultSource = new LogSource();
117 mDefaultSource->addLogListener(mFile);
118 mDefaultSource->addLogListener(mConsole);
119 mDefaultSource->setLogFilter(mFilter);
120
121 mDefaultSource->open();
122
123 LogManager::getInstance().addLogSource(mDefaultSource);
124 }
125
126 void LogManager::setSTDOutputEnabled(bool _value)
127 {
128 mConsoleEnable = _value;
129
130 if (mConsole != nullptr)
131 mConsole->setEnabled(_value);
132 }
133
134 bool LogManager::getSTDOutputEnabled() const
135 {
136 return mConsoleEnable;
137 }
138
139 void LogManager::setLoggingLevel(LogLevel _value)
140 {
141 mLevel = _value;
142
143 if (mFilter != nullptr)
144 mFilter->setLoggingLevel(_value);
145 }
146
147 LogLevel LogManager::getLoggingLevel() const
148 {
149 return mLevel;
150 }
151
152 } // namespace MyGUI
MYGUI_DBG_BREAK
#define MYGUI_DBG_BREAK
Definition: MyGUI_Diagnostic.h:46
MyGUI::ConsoleLogListener
Definition: MyGUI_ConsoleLogListener.h:31
MyGUI::LogSource::setLogFilter
void setLogFilter(ILogFilter *_filter)
Definition: MyGUI_LogSource.cpp:38
MyGUI::LevelLogFilter
Definition: MyGUI_LevelLogFilter.h:31
MyGUI_Precompiled.h
MyGUI::LogManager::getInstance
static LogManager & getInstance()
Definition: MyGUI_LogManager.cpp:63
MyGUI::LogSource::open
void open()
Definition: MyGUI_LogSource.cpp:48
MyGUI::LogLevel
Definition: MyGUI_LogLevel.h:30
MyGUI::LogManager
Definition: MyGUI_LogManager.h:41
MyGUI::LogManager::getSTDOutputEnabled
bool getSTDOutputEnabled() const
Definition: MyGUI_LogManager.cpp:134
MyGUI::LogManager::getLoggingLevel
LogLevel getLoggingLevel() const
Definition: MyGUI_LogManager.cpp:147
MyGUI::LogManager::setLoggingLevel
void setLoggingLevel(LogLevel _value)
Definition: MyGUI_LogManager.cpp:139
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:47
MyGUI::LogManager::~LogManager
~LogManager()
Definition: MyGUI_LogManager.cpp:46
MyGUI::LogManager::addLogSource
void addLogSource(LogSource *_source)
Definition: MyGUI_LogManager.cpp:101
MyGUI::ConsoleLogListener::setEnabled
void setEnabled(bool _value)
Definition: MyGUI_ConsoleLogListener.cpp:48
MyGUI_FileLogListener.h
MyGUI::LogManager::getInstancePtr
static LogManager * getInstancePtr()
Definition: MyGUI_LogManager.cpp:73
MyGUI::LogManager::log
void log(const std::string &_section, LogLevel _level, const std::string &_message, const char *_file, int _line)
Definition: MyGUI_LogManager.cpp:84
MyGUI_LogSource.h
MyGUI::LogManager::setSTDOutputEnabled
void setSTDOutputEnabled(bool _value)
Definition: MyGUI_LogManager.cpp:126
MYGUI_BASE_EXCEPT
#define MYGUI_BASE_EXCEPT(desc, src)
Definition: MyGUI_Diagnostic.h:40
MyGUI_LogManager.h
MyGUI::LogManager::flush
void flush()
Definition: MyGUI_LogManager.cpp:78
MyGUI::LogManager::LogManager
LogManager()
Definition: MyGUI_LogManager.cpp:35
MyGUI_ConsoleLogListener.h
MyGUI_LevelLogFilter.h
MyGUI::LogManager::createDefaultSource
void createDefaultSource(const std::string &_logname)
Definition: MyGUI_LogManager.cpp:106
MyGUI::LevelLogFilter::setLoggingLevel
void setLoggingLevel(LogLevel _value)
Definition: MyGUI_LevelLogFilter.cpp:42
MyGUI::LogSource
Definition: MyGUI_LogSource.h:33
MyGUI::FileLogListener
Definition: MyGUI_FileLogListener.h:33
MyGUI::FileLogListener::setFileName
void setFileName(const std::string &_value)
Definition: MyGUI_FileLogListener.cpp:85
MyGUI::LogSource::addLogListener
void addLogListener(ILogListener *_lestener)
Definition: MyGUI_LogSource.cpp:43