MyGUI  3.0.1
MyGUI_LogManager.cpp
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #include "MyGUI_Precompiled.h"
24 #include "MyGUI_LogManager.h"
25 #include <sstream>
26 #include <assert.h> // REMOVEME
27 
28 namespace MyGUI
29 {
30 
31  const std::string LogManager::LevelsName[EndLogLevel] =
32  {
33  "Info",
34  "Warning",
35  "Error",
36  "Critical"
37  };
38 
39  const std::string LogManager::General = "General";
40  const std::string LogManager::separator = " | ";
41 
42  LogStream::LogStreamEnd LogManager::endl;
43  LogManager* LogManager::msInstance = 0;
44 
45  LogManager::LogManager()
46  {
47  msInstance = this;
48  mSTDOut = true;
49  }
50 
51  LogManager::~LogManager()
52  {
53  MapLogStream& mapStream = msInstance->mMapSectionFileName;
54  for (MapLogStream::iterator iter=mapStream.begin(); iter!=mapStream.end(); ++iter)
55  {
56  LogStream * stream = iter->second;
57  if (stream == 0) continue;
58 
59  // ищем все такие потоки и обнуляем
60  for (MapLogStream::iterator iter2=iter; iter2!=mapStream.end(); ++iter2)
61  {
62  if (iter2->second == stream) iter2->second = 0;
63  }
64  delete stream;
65  }
66  mapStream.clear();
67  msInstance = nullptr;
68  }
69 
71  {
72  if (msInstance != nullptr)
73  {
74  delete msInstance;
75  msInstance = nullptr;
76  }
77  }
78 
80  {
81  if (msInstance == nullptr)
82  {
83  msInstance = new LogManager();
84  }
85  }
86 
87  LogStream& LogManager::out(const std::string& _section, LogLevel _level)
88  {
89  static LogStream empty;
90 
91  if (msInstance == nullptr)
92  return empty;
93 
94  MapLogStream& mapStream = msInstance->mMapSectionFileName;
95  MapLogStream::iterator iter = mapStream.find(_section);
96  if (iter == mapStream.end())
97  return empty;
98 
99  if (_level >= EndLogLevel)
100  _level = Info;
101 
102  iter->second->start(_section, LevelsName[_level]);
103 
104  return *(iter->second);
105  }
106 
107  void LogManager::registerSection(const std::string& _section, const std::string& _file)
108  {
109  if (0 == msInstance) new LogManager();
110 
111  // ищем такую же секцию и удаляем ее
112  MapLogStream& mapStream = msInstance->mMapSectionFileName;
113  /*MapLogStream::iterator iter = mapStream.find(_section);
114  if (iter != mapStream.end())
115  {
116  delete iter->second;
117  mapStream.erase(iter);
118  }*/
119 
120  // ищем поток с таким же именем, если нет, то создаем
121  LogStream * stream = 0;
122  for (MapLogStream::iterator iter=mapStream.begin(); iter!=mapStream.end(); ++iter)
123  {
124  if (iter->second->getFileName() == _file)
125  {
126  stream = iter->second;
127  break;
128  }
129  }
130  if (0 == stream)
131  stream = new LogStream(_file);
132 
133  mapStream[_section] = stream;
134  }
135 
136  void LogManager::unregisterSection(const std::string& _section)
137  {
138  MapLogStream& mapStream = msInstance->mMapSectionFileName;
139  MapLogStream::iterator iter = mapStream.find(_section);
140  if (iter == mapStream.end()) return;
141 
142  LogStream * stream = iter->second;
143  mapStream.erase(iter);
144 
145  // если файл еще используеться то удалять не надо
146  for (iter=mapStream.begin(); iter!=mapStream.end(); ++iter)
147  {
148  if (iter->second == stream)
149  return;
150  }
151 
152  delete stream;
153 
154  if (mapStream.size() == 0) shutdown();
155  }
156 
157  const std::string& LogManager::info(const char * _file /* = __FILE__*/, int _line /* = __LINE__*/)
158  {
159  std::ostringstream stream;
160  stream << separator << _file << separator << _line;
161 
162  static std::string ret;
163  ret = stream.str();
164  return ret;
165  }
166 
168  {
169  return endl;
170  }
171 
173  {
174  assert(msInstance);
175  msInstance->mSTDOut = _enable;
176  }
177 
179  {
180  assert(msInstance);
181  return msInstance->mSTDOut;
182  }
183 
184 } // namespace MyGUI