MyGUI: MyGUI_LogManager.cpp Source File

MyGUI 3.4.2

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

- File List
- Globals

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 <ctime>
13
14namespace MyGUI
15{
16
17 LogManager* LogManager::msInstance = nullptr;
18
19 LogManager::LogManager() :
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
30 LogManager::~LogManager()
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
47 LogManager& LogManager::getInstance()
48 {
49 MYGUI_ASSERT(nullptr != getInstancePtr(), "Singleton instance LogManager was not created");
50 return *msInstance;
51 }
52
53 LogManager* LogManager::getInstancePtr()
54 {
55 return msInstance;
56 }
57
58 void LogManager::flush()
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
81 void LogManager::addLogSource(LogSource* _source)
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
109 void LogManager::setSTDOutputEnabled(bool _value)
110 {
111 mConsoleEnable = _value;
112
113 if (mConsole != nullptr)
114 mConsole->setEnabled(_value);
115 }
116
117 bool LogManager::getSTDOutputEnabled() const
118 {
119 return mConsoleEnable;
120 }
121
122 void LogManager::setLoggingLevel(LogLevel _value)
123 {
124 mLevel = _value;
125
126 if (mFilter != nullptr)
127 mFilter->setLoggingLevel(_value);
128 }
129
130 LogLevel LogManager::getLoggingLevel() const
131 {
132 return mLevel;
133 }
134
135} // namespace MyGUI
MyGUI_ConsoleLogListener.h
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:29
MyGUI_FileLogListener.h
MyGUI_LevelLogFilter.h
MyGUI_LogManager.h
MyGUI_Precompiled.h
MyGUI::ConsoleLogListener
Definition MyGUI_ConsoleLogListener.h:18
MyGUI::ConsoleLogListener::setEnabled
void setEnabled(bool _value)
Definition MyGUI_ConsoleLogListener.cpp:29
MyGUI::FileLogListener
Definition MyGUI_FileLogListener.h:20
MyGUI::FileLogListener::setFileName
void setFileName(const std::string &_value)
Definition MyGUI_FileLogListener.cpp:62
MyGUI::LevelLogFilter
Definition MyGUI_LevelLogFilter.h:18
MyGUI::LevelLogFilter::setLoggingLevel
void setLoggingLevel(LogLevel _value)
Definition MyGUI_LevelLogFilter.cpp:23
MyGUI::LogManager
Definition MyGUI_LogManager.h:27
MyGUI::LogManager::~LogManager
~LogManager()
Definition MyGUI_LogManager.cpp:30
MyGUI::LogManager::flush
void flush()
Definition MyGUI_LogManager.cpp:58
MyGUI::LogManager::setSTDOutputEnabled
void setSTDOutputEnabled(bool _value)
Definition MyGUI_LogManager.cpp:109
MyGUI::LogManager::addLogSource
void addLogSource(LogSource *_source)
Definition MyGUI_LogManager.cpp:81
MyGUI::LogManager::getInstancePtr
static LogManager * getInstancePtr()
Definition MyGUI_LogManager.cpp:53
MyGUI::LogManager::getLoggingLevel
LogLevel getLoggingLevel() const
Definition MyGUI_LogManager.cpp:130
MyGUI::LogManager::LogManager
LogManager()
Definition MyGUI_LogManager.cpp:19
MyGUI::LogManager::createDefaultSource
void createDefaultSource(const std::string &_logname)
Definition MyGUI_LogManager.cpp:86
MyGUI::LogManager::setLoggingLevel
void setLoggingLevel(LogLevel _value)
Definition MyGUI_LogManager.cpp:122
MyGUI::LogManager::getInstance
static LogManager & getInstance()
Definition MyGUI_LogManager.cpp:47
MyGUI::LogManager::log
void log(const std::string &_section, LogLevel _level, const std::string &_message, const char *_file, int _line)
Definition MyGUI_LogManager.cpp:64
MyGUI::LogManager::getSTDOutputEnabled
bool getSTDOutputEnabled() const
Definition MyGUI_LogManager.cpp:117
MyGUI::LogSource
Definition MyGUI_LogSource.h:19
MyGUI::LogSource::setLogFilter
void setLogFilter(ILogFilter *_filter)
Definition MyGUI_LogSource.cpp:20
MyGUI::LogSource::open
void open()
Definition MyGUI_LogSource.cpp:30
MyGUI::LogSource::addLogListener
void addLogListener(ILogListener *_lestener)
Definition MyGUI_LogSource.cpp:25
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::LogLevel
Definition MyGUI_LogLevel.h:19