MyGUI  3.0.1
MyGUI_LogStream.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_LogStream.h"
00025 #include "MyGUI_LogManager.h"
00026 #include <iomanip>
00027 #include <time.h>
00028 
00029 namespace MyGUI
00030 {
00031 
00032     LogStream::LogStream() { }
00033     LogStream::~LogStream()
00034     {
00035         if (mStream.is_open())
00036         {
00037             mStream.close();
00038             release();
00039         }
00040     }
00041 
00042     LogStream::LogStream(const std::string& _file) : mFileName(_file)
00043     {
00044         lock();
00045 
00046         struct tm *current_time;
00047         time_t ctTime; time(&ctTime);
00048         current_time = localtime( &ctTime );
00049 
00050         mStream.open(mFileName.c_str(), std::ios_base::out);
00051         if (mStream.is_open())
00052         {
00053             mStream << " ---------------------------------------------------------------------------------------------------------------------------------- " << std::endl;
00054             mStream << "                                          loging report for : "
00055                 << std::setw(2) << std::setfill('0') << current_time->tm_mon + 1 << "/"
00056                 << std::setw(2) << std::setfill('0') << current_time->tm_mday << "/"
00057                 << std::setw(4) << std::setfill('0') << current_time->tm_year + 1900 << "   "
00058                 << std::setw(2) << std::setfill('0') << current_time->tm_hour << ":"
00059                 << std::setw(2) << std::setfill('0') << current_time->tm_min << ":"
00060                 << std::setw(2) << std::setfill('0') << current_time->tm_sec << std::endl;
00061             mStream << " ---------------------------------------------------------------------------------------------------------------------------------- " << std::endl << std::endl;
00062             mStream.close();
00063         }
00064 
00065         release();
00066     }
00067 
00068     void LogStream::start(const std::string& _section, const std::string& _level)
00069     {
00070         if (mStream.is_open())
00071         {
00072             mStream.close();
00073             release();
00074         }
00075 
00076         lock();
00077 
00078         struct tm *current_time;
00079         time_t ctTime; time(&ctTime);
00080         current_time = localtime( &ctTime );
00081 
00082         if (!mFileName.empty())
00083         {
00084             mStream.open(mFileName.c_str(), std::ios_base::app);
00085             if (mStream.is_open())
00086             {
00087                 mStream << std::setw(2) << std::setfill('0') << current_time->tm_hour << ":"
00088                     << std::setw(2) << std::setfill('0') << current_time->tm_min << ":"
00089                     << std::setw(2) << std::setfill('0') << current_time->tm_sec << LogManager::separator
00090                     << _section << LogManager::separator << _level << LogManager::separator;
00091             }
00092         }
00093     }
00094 
00095     bool LogStream::getSTDOutputEnabled()
00096     {
00097         return LogManager::getSTDOutputEnabled();
00098     }
00099 
00100     LogStream& LogStream::operator<<(const LogStreamEnd& _endl)
00101     {
00102         if (getSTDOutputEnabled()) std::cout << std::endl;
00103         if (mStream.is_open())
00104         {
00105             mStream << std::endl;
00106             mStream.close();
00107         }
00108         release();
00109 
00110         return *this;
00111     }
00112 
00113 } // namespace MyGUI