• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

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 
00027 namespace MyGUI
00028 {
00029 
00030     const std::string LogManager::LevelsName[EndLogLevel] =
00031     {
00032         "Info",
00033         "Warning",
00034         "Error",
00035         "Critical"
00036     };
00037 
00038     const std::string LogManager::General = "General";
00039     const std::string LogManager::separator = "  |  ";
00040 
00041     LogStream::LogStreamEnd LogManager::endl;
00042     LogManager* LogManager::msInstance = 0;
00043 
00044     LogManager::LogManager()
00045     {
00046         msInstance = this;
00047         mSTDOut = true;
00048     }
00049 
00050     LogManager::~LogManager()
00051     {
00052         MapLogStream& mapStream = msInstance->mMapSectionFileName;
00053         for (MapLogStream::iterator iter=mapStream.begin(); iter!=mapStream.end(); ++iter) {
00054             LogStream * stream = iter->second;
00055             if (stream == 0) continue;
00056 
00057             // ищем все такие потоки и обнуляем
00058             for (MapLogStream::iterator iter2=iter; iter2!=mapStream.end(); ++iter2) {
00059                 if (iter2->second == stream) iter2->second = 0;
00060             }
00061             delete stream;
00062         }
00063         mapStream.clear();
00064         msInstance = 0;
00065     }
00066 
00067     void LogManager::shutdown()
00068     {
00069         if (0 != msInstance) delete msInstance;
00070     }
00071 
00072     void LogManager::initialise()
00073     {
00074         if (0 == msInstance) new LogManager();
00075     }
00076 
00077     LogStream& LogManager::out(const std::string& _section, LogLevel _level)
00078     {
00079         static LogStream empty;
00080 
00081         if (0 == msInstance) return empty;
00082 
00083         MapLogStream& mapStream = msInstance->mMapSectionFileName;
00084         MapLogStream::iterator iter = mapStream.find(_section);
00085         if (iter == mapStream.end()) return empty;
00086 
00087         if (_level >= EndLogLevel) _level = Info;
00088 
00089         iter->second->start(_section, LevelsName[_level]);
00090 
00091         return *(iter->second);
00092     }
00093 
00094     void LogManager::registerSection(const std::string& _section, const std::string& _file)
00095     {
00096         if (0 == msInstance) new LogManager();
00097 
00098         // ищем такую же секцию и удаляем ее
00099         MapLogStream& mapStream = msInstance->mMapSectionFileName;
00100         MapLogStream::iterator iter = mapStream.find(_section);
00101         if (iter != mapStream.end()) {
00102             delete iter->second;
00103             mapStream.erase(iter);
00104         }
00105 
00106         // ищем поток с таким же именем, если нет, то создаем
00107         LogStream * stream = 0;
00108         for (iter=mapStream.begin(); iter!=mapStream.end(); ++iter) {
00109             if (iter->second->getFileName() == _file) {
00110                 stream = iter->second;
00111                 break;
00112             }
00113         }
00114         if (0 == stream) stream = new LogStream(_file);
00115 
00116         mapStream[_section] = stream;
00117     }
00118 
00119     void LogManager::unregisterSection(const std::string& _section)
00120     {
00121         MapLogStream& mapStream = msInstance->mMapSectionFileName;
00122         MapLogStream::iterator iter = mapStream.find(_section);
00123         if (iter == mapStream.end()) return;
00124 
00125         LogStream * stream = iter->second;
00126         mapStream.erase(iter);
00127 
00128         // если файл еще используеться то удалять не надо
00129         for (iter=mapStream.begin(); iter!=mapStream.end(); ++iter) {
00130             if (iter->second == stream) return;
00131         }
00132 
00133         delete stream;
00134 
00135         if (mapStream.size() == 0) shutdown();
00136     }
00137 
00138     const std::string& LogManager::info(const char * _file /* = __FILE__*/, int _line /* = __LINE__*/)
00139     {
00140         std::ostringstream stream;
00141         stream << separator << _file << separator << _line;
00142 
00143         static std::string ret;
00144         ret = stream.str();
00145         return ret;
00146     }
00147 
00148     const LogStream::LogStreamEnd& LogManager::end()
00149     {
00150         return endl;
00151     }
00152 
00153     void LogManager::setSTDOutputEnabled(bool _enable)
00154     {
00155         assert(msInstance);
00156         msInstance->mSTDOut = _enable;
00157     }
00158 
00159     bool LogManager::getSTDOutputEnabled()
00160     {
00161         assert(msInstance);
00162         return msInstance->mSTDOut;
00163     }
00164 
00165 } // namespace MyGUI

Generated on Sun Jan 30 2011 for MyGUI by  doxygen 1.7.1