Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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 , int _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 }