MyGUI: MyGUI_LogManager.cpp Source File

MyGUI 3.4.3

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 {
21 msInstance = this;
22 }
23
24 LogManager::~LogManager()
25 {
26 flush();
27 close();
28
29 msInstance = nullptr;
30 }
31
32 LogManager& LogManager::getInstance()
33 {
34 MYGUI_ASSERT(nullptr != getInstancePtr(), "Singleton instance LogManager was not created");
35 return *msInstance;
36 }
37
38 LogManager* LogManager::getInstancePtr()
39 {
40 return msInstance;
41 }
42
43 void LogManager::flush()
44 {
45 for (auto& source : mSources)
46 source->flush();
47 }
48
49 void LogManager::log(
50 std::string_view _section,
51 LogLevel _level,
52 std::string_view _message,
53 std::string_view _file,
54 int _line)
55 {
56 time_t ctTime;
57 time(&ctTime);
58 struct tm* currentTime;
59 currentTime = localtime(&ctTime);
60
61 for (auto& source : mSources)
62 source->log(_section, _level, currentTime, _message, _file, _line);
63 }
64
65 void LogManager::close()
66 {
67 for (auto& source : mSources)
68 source->close();
69 }
70
71 void LogManager::addLogSource(LogSource* _source)
72 {
73 mSources.push_back(_source);
74 }
75
76 void LogManager::createDefaultSource(std::string_view _logname)
77 {
78 mDefaultSource = std::make_unique<LogSource>();
79
80 mConsole = std::make_unique<ConsoleLogListener>();
81 mConsole->setEnabled(mConsoleEnable);
82 mDefaultSource->addLogListener(mConsole.get());
83
84#ifndef EMSCRIPTEN
85 mFile = std::make_unique<FileLogListener>();
86 mFile->setFileName(_logname);
87 mDefaultSource->addLogListener(mFile.get());
88#endif
89
90 mFilter = std::make_unique<LevelLogFilter>();
91 mFilter->setLoggingLevel(mLevel);
92 mDefaultSource->setLogFilter(mFilter.get());
93
94 mDefaultSource->open();
95
96 LogManager::getInstance().addLogSource(mDefaultSource.get());
97 }
98
99 void LogManager::setSTDOutputEnabled(bool _value)
100 {
101 mConsoleEnable = _value;
102
103 if (mConsole != nullptr)
104 mConsole->setEnabled(_value);
105 }
106
107 bool LogManager::getSTDOutputEnabled() const
108 {
109 return mConsoleEnable;
110 }
111
112 void LogManager::setLoggingLevel(LogLevel _value)
113 {
114 mLevel = _value;
115
116 if (mFilter != nullptr)
117 mFilter->setLoggingLevel(_value);
118 }
119
120 LogLevel LogManager::getLoggingLevel() const
121 {
122 return mLevel;
123 }
124
125} // namespace MyGUI
MyGUI_ConsoleLogListener.h
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition MyGUI_Diagnostic.h:31
MyGUI_FileLogListener.h
MyGUI_LevelLogFilter.h
MyGUI_LogManager.h
MyGUI_Precompiled.h
MyGUI::LogManager
Definition MyGUI_LogManager.h:33
MyGUI::LogManager::~LogManager
~LogManager()
Definition MyGUI_LogManager.cpp:24
MyGUI::LogManager::flush
void flush()
Definition MyGUI_LogManager.cpp:43
MyGUI::LogManager::setSTDOutputEnabled
void setSTDOutputEnabled(bool _value)
Definition MyGUI_LogManager.cpp:99
MyGUI::LogManager::addLogSource
void addLogSource(LogSource *_source)
Definition MyGUI_LogManager.cpp:71
MyGUI::LogManager::createDefaultSource
void createDefaultSource(std::string_view _logname)
Definition MyGUI_LogManager.cpp:76
MyGUI::LogManager::getInstancePtr
static LogManager * getInstancePtr()
Definition MyGUI_LogManager.cpp:38
MyGUI::LogManager::getLoggingLevel
LogLevel getLoggingLevel() const
Definition MyGUI_LogManager.cpp:120
MyGUI::LogManager::LogManager
LogManager()
Definition MyGUI_LogManager.cpp:19
MyGUI::LogManager::setLoggingLevel
void setLoggingLevel(LogLevel _value)
Definition MyGUI_LogManager.cpp:112
MyGUI::LogManager::getInstance
static LogManager & getInstance()
Definition MyGUI_LogManager.cpp:32
MyGUI::LogManager::getSTDOutputEnabled
bool getSTDOutputEnabled() const
Definition MyGUI_LogManager.cpp:107
MyGUI::LogManager::log
void log(std::string_view _section, LogLevel _level, std::string_view _message, std::string_view _file, int _line)
Definition MyGUI_LogManager.cpp:49
MyGUI::LogSource
Definition MyGUI_LogSource.h:19
MyGUI
Definition MyGUI_ActionController.h:15
MyGUI::LogLevel
Definition MyGUI_LogLevel.h:19