MyGUI: MyGUI_LogManager.cpp Source File

MyGUI 3.2.2

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"
9 #include "MyGUI_FileLogListener.h"
10 #include "MyGUI_ConsoleLogListener.h"
11 #include "MyGUI_LevelLogFilter.h"
12 #include "MyGUI_LogSource.h"
13 #include <time.h>
14
15 namespace MyGUI
16 {
17
18 LogManager* LogManager::msInstance = nullptr;
19
20 LogManager::LogManager() :
21 mConsole(nullptr),
22 mFile(nullptr),
23 mFilter(nullptr),
24 mDefaultSource(nullptr),
25 mLevel(LogLevel::Info),
26 mConsoleEnable(true)
27 {
28 msInstance = this;
29 }
30
31 LogManager::~LogManager()
32 {
33 flush();
34 close();
35
36 delete mDefaultSource;
37 mDefaultSource = nullptr;
38 delete mConsole;
39 mConsole = nullptr;
40 delete mFile;
41 mFile = nullptr;
42 delete mFilter;
43 mFilter = nullptr;
44
45 msInstance = nullptr;
46 }
47
48 LogManager& LogManager::getInstance()
49 {
50 if (msInstance == nullptr)
51 {
52 MYGUI_DBG_BREAK;
53 MYGUI_BASE_EXCEPT("Singleton instance LogManager was not created", "MyGUI");
54 }
55 return *msInstance;
56 }
57
58 LogManager* LogManager::getInstancePtr()
59 {
60 return msInstance;
61 }
62
63 void LogManager::flush()
64 {
65 for (VectorLogSource::iterator item = mSources.begin(); item != mSources.end(); ++item)
66 (*item)->flush();
67 }
68
69 void LogManager::log(const std::string& _section, LogLevel _level, const std::string& _message, const char* _file, int _line)
70 {
71 time_t ctTime;
72 time(&ctTime);
73 struct tm* currentTime;
74 currentTime = localtime(&ctTime);
75
76 for (VectorLogSource::iterator item = mSources.begin(); item != mSources.end(); ++item)
77 (*item)->log(_section, _level, currentTime, _message, _file, _line);
78 }
79
80 void LogManager::close()
81 {
82 for (VectorLogSource::iterator item = mSources.begin(); item != mSources.end(); ++item)
83 (*item)->close();
84 }
85
86 void LogManager::addLogSource(LogSource* _source)
87 {
88 mSources.push_back(_source);
89 }
90
91 void LogManager::createDefaultSource(const std::string& _logname)
92 {
93 mConsole = new ConsoleLogListener();
94 mFile = new FileLogListener();
95 mFilter = new LevelLogFilter();
96
97 mFile->setFileName(_logname);
98 mConsole->setEnabled(mConsoleEnable);
99 mFilter->setLoggingLevel(mLevel);
100
101 mDefaultSource = new LogSource();
102 mDefaultSource->addLogListener(mFile);
103 mDefaultSource->addLogListener(mConsole);
104 mDefaultSource->setLogFilter(mFilter);
105
106 mDefaultSource->open();
107
108 LogManager::getInstance().addLogSource(mDefaultSource);
109 }
110
111 void LogManager::setSTDOutputEnabled(bool _value)
112 {
113 mConsoleEnable = _value;
114
115 if (mConsole != nullptr)
116 mConsole->setEnabled(_value);
117 }
118
119 bool LogManager::getSTDOutputEnabled() const
120 {
121 return mConsoleEnable;
122 }
123
124 void LogManager::setLoggingLevel(LogLevel _value)
125 {
126 mLevel = _value;
127
128 if (mFilter != nullptr)
129 mFilter->setLoggingLevel(_value);
130 }
131
132 LogLevel LogManager::getLoggingLevel() const
133 {
134 return mLevel;
135 }
136
137 } // namespace MyGUI
MyGUI::LevelLogFilter
Definition: MyGUI_LevelLogFilter.h:16
MyGUI::LevelLogFilter::setLoggingLevel
void setLoggingLevel(LogLevel _value)
Definition: MyGUI_LevelLogFilter.cpp:27
MyGUI::LogSource
Definition: MyGUI_LogSource.h:18
MyGUI::LogManager::LogManager
LogManager()
Definition: MyGUI_LogManager.cpp:20
MyGUI::LogManager::getSTDOutputEnabled
bool getSTDOutputEnabled() const
Definition: MyGUI_LogManager.cpp:119
MyGUI_LogManager.h
MyGUI_ConsoleLogListener.h
MyGUI::FileLogListener::setFileName
void setFileName(const std::string &_value)
Definition: MyGUI_FileLogListener.cpp:70
MyGUI::LogManager
Definition: MyGUI_LogManager.h:26
MYGUI_BASE_EXCEPT
#define MYGUI_BASE_EXCEPT(desc, src)
Definition: MyGUI_Diagnostic.h:24
MyGUI::LogManager::createDefaultSource
void createDefaultSource(const std::string &_logname)
Definition: MyGUI_LogManager.cpp:91
MyGUI_LevelLogFilter.h
MyGUI::LogManager::getInstancePtr
static LogManager * getInstancePtr()
Definition: MyGUI_LogManager.cpp:58
MyGUI_Precompiled.h
MyGUI::LogManager::setLoggingLevel
void setLoggingLevel(LogLevel _value)
Definition: MyGUI_LogManager.cpp:124
MyGUI::LogManager::~LogManager
~LogManager()
Definition: MyGUI_LogManager.cpp:31
MyGUI::LogManager::getLoggingLevel
LogLevel getLoggingLevel() const
Definition: MyGUI_LogManager.cpp:132
MyGUI::LogManager::setSTDOutputEnabled
void setSTDOutputEnabled(bool _value)
Definition: MyGUI_LogManager.cpp:111
MyGUI::LogSource::addLogListener
void addLogListener(ILogListener *_lestener)
Definition: MyGUI_LogSource.cpp:27
MyGUI::ConsoleLogListener
Definition: MyGUI_ConsoleLogListener.h:16
MYGUI_DBG_BREAK
#define MYGUI_DBG_BREAK
Definition: MyGUI_Diagnostic.h:30
MyGUI::FileLogListener
Definition: MyGUI_FileLogListener.h:18
MyGUI::LogManager::log
void log(const std::string &_section, LogLevel _level, const std::string &_message, const char *_file, int _line)
Definition: MyGUI_LogManager.cpp:69
MyGUI::LogManager::flush
void flush()
Definition: MyGUI_LogManager.cpp:63
MyGUI::ConsoleLogListener::setEnabled
void setEnabled(bool _value)
Definition: MyGUI_ConsoleLogListener.cpp:33
MyGUI::LogManager::addLogSource
void addLogSource(LogSource *_source)
Definition: MyGUI_LogManager.cpp:86
MyGUI::LogSource::open
void open()
Definition: MyGUI_LogSource.cpp:32
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:29
MyGUI_FileLogListener.h
MyGUI_LogSource.h
MyGUI::LogManager::getInstance
static LogManager & getInstance()
Definition: MyGUI_LogManager.cpp:48
MyGUI::LogLevel
Definition: MyGUI_LogLevel.h:15
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::LogSource::setLogFilter
void setLogFilter(ILogFilter *_filter)
Definition: MyGUI_LogSource.cpp:22