MyGUI  3.2.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"
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 
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 
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 
49  {
50  if (msInstance == nullptr)
51  {
53  MYGUI_BASE_EXCEPT("Singleton instance LogManager was not created", "MyGUI");
54  }
55  return *msInstance;
56  }
57 
59  {
60  return msInstance;
61  }
62 
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 
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 
112  {
113  mConsoleEnable = _value;
114 
115  if (mConsole != nullptr)
116  mConsole->setEnabled(_value);
117  }
118 
120  {
121  return mConsoleEnable;
122  }
123 
125  {
126  mLevel = _value;
127 
128  if (mFilter != nullptr)
129  mFilter->setLoggingLevel(_value);
130  }
131 
133  {
134  return mLevel;
135  }
136 
137 } // namespace MyGUI