MyGUI  3.0.3
MyGUI_LogManager.cpp
Go to the documentation of this file.
00001 
00007 /*
00008     This file is part of MyGUI.
00009 
00010     MyGUI is free software: you can redistribute it and/or modify
00011     it under the terms of the GNU Lesser General Public License as published by
00012     the Free Software Foundation, either version 3 of the License, or
00013     (at your option) any later version.
00014 
00015     MyGUI is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public License
00021     along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 #include "MyGUI_Precompiled.h"
00024 #include "MyGUI_LogManager.h"
00025 #include <sstream>
00026 #include <assert.h> // REMOVEME
00027 
00028 namespace MyGUI
00029 {
00030 
00031     const std::string LogManager::LevelsName[EndLogLevel] =
00032     {
00033         "Info",
00034         "Warning",
00035         "Error",
00036         "Critical"
00037     };
00038 
00039     const std::string LogManager::General = "General";
00040     const std::string LogManager::separator = "  |  ";
00041 
00042     LogStream::LogStreamEnd LogManager::endl;
00043     LogManager* LogManager::msInstance = 0;
00044 
00045     LogManager::LogManager()
00046     {
00047         msInstance = this;
00048         mSTDOut = true;
00049     }
00050 
00051     LogManager::~LogManager()
00052     {
00053         MapLogStream& mapStream = msInstance->mMapSectionFileName;
00054         for (MapLogStream::iterator iter=mapStream.begin(); iter!=mapStream.end(); ++iter)
00055         {
00056             LogStream * stream = iter->second;
00057             if (stream == 0) continue;
00058 
00059             // ищем все такие потоки и обнуляем
00060             for (MapLogStream::iterator iter2=iter; iter2!=mapStream.end(); ++iter2)
00061             {
00062                 if (iter2->second == stream) iter2->second = 0;
00063             }
00064             delete stream;
00065         }
00066         mapStream.clear();
00067         msInstance = nullptr;
00068     }
00069 
00070     void LogManager::shutdown()
00071     {
00072         if (msInstance != nullptr)
00073         {
00074             delete msInstance;
00075             msInstance = nullptr;
00076         }
00077     }
00078 
00079     void LogManager::initialise()
00080     {
00081         if (msInstance == nullptr)
00082         {
00083             msInstance = new LogManager();
00084         }
00085     }
00086 
00087     LogStream& LogManager::out(const std::string& _section, LogLevel _level)
00088     {
00089         static LogStream empty;
00090 
00091         if (msInstance == nullptr)
00092             return empty;
00093 
00094         MapLogStream& mapStream = msInstance->mMapSectionFileName;
00095         MapLogStream::iterator iter = mapStream.find(_section);
00096         if (iter == mapStream.end())
00097             return empty;
00098 
00099         if (_level >= EndLogLevel)
00100             _level = Info;
00101 
00102         iter->second->start(_section, LevelsName[_level]);
00103 
00104         return *(iter->second);
00105     }
00106 
00107     void LogManager::registerSection(const std::string& _section, const std::string& _file)
00108     {
00109         if (0 == msInstance) new LogManager();
00110 
00111         // ищем такую же секцию и удаляем ее
00112         MapLogStream& mapStream = msInstance->mMapSectionFileName;
00113         /*MapLogStream::iterator iter = mapStream.find(_section);
00114         if (iter != mapStream.end())
00115         {
00116             delete iter->second;
00117             mapStream.erase(iter);
00118         }*/
00119 
00120         // ищем поток с таким же именем, если нет, то создаем
00121         LogStream * stream = 0;
00122         for (MapLogStream::iterator iter=mapStream.begin(); iter!=mapStream.end(); ++iter)
00123         {
00124             if (iter->second->getFileName() == _file)
00125             {
00126                 stream = iter->second;
00127                 break;
00128             }
00129         }
00130         if (0 == stream)
00131             stream = new LogStream(_file);
00132 
00133         mapStream[_section] = stream;
00134     }
00135 
00136     void LogManager::unregisterSection(const std::string& _section)
00137     {
00138         MapLogStream& mapStream = msInstance->mMapSectionFileName;
00139         MapLogStream::iterator iter = mapStream.find(_section);
00140         if (iter == mapStream.end()) return;
00141 
00142         LogStream * stream = iter->second;
00143         mapStream.erase(iter);
00144 
00145         // если файл еще используеться то удалять не надо
00146         for (iter=mapStream.begin(); iter!=mapStream.end(); ++iter)
00147         {
00148             if (iter->second == stream)
00149                 return;
00150         }
00151 
00152         delete stream;
00153 
00154         if (mapStream.size() == 0) shutdown();
00155     }
00156 
00157     const std::string& LogManager::info(const char * _file /* = __FILE__*/, int _line /* = __LINE__*/)
00158     {
00159         std::ostringstream stream;
00160         stream << separator << _file << separator << _line;
00161 
00162         static std::string ret;
00163         ret = stream.str();
00164         return ret;
00165     }
00166 
00167     const LogStream::LogStreamEnd& LogManager::end()
00168     {
00169         return endl;
00170     }
00171 
00172     void LogManager::setSTDOutputEnabled(bool _enable)
00173     {
00174         assert(msInstance);
00175         msInstance->mSTDOut = _enable;
00176     }
00177 
00178     bool LogManager::getSTDOutputEnabled()
00179     {
00180         assert(msInstance);
00181         return msInstance->mSTDOut;
00182     }
00183 
00184 } // namespace MyGUI