Log.cc revision 10447
112870Sgabeblack@google.com#include "Log.h" 212870Sgabeblack@google.com 312870Sgabeblack@google.com#include "Assert.h" 412870Sgabeblack@google.com 512870Sgabeblack@google.comnamespace LibUtil 612870Sgabeblack@google.com{ 712870Sgabeblack@google.com using std::ostream; 812870Sgabeblack@google.com using std::endl; 912870Sgabeblack@google.com 1012870Sgabeblack@google.com Log* Log::msSingleton = NULL; 1112870Sgabeblack@google.com const bool Log::msIsLog = LIBUTIL_IS_LOG; 1212870Sgabeblack@google.com 1312870Sgabeblack@google.com void Log::allocate(const String& log_file_name_) 1412870Sgabeblack@google.com { 1512870Sgabeblack@google.com if(msIsLog) 1612870Sgabeblack@google.com { 1712870Sgabeblack@google.com // Allocate static Config instance 1812870Sgabeblack@google.com ASSERT(!msSingleton, "Log singleton is allocated"); 1912870Sgabeblack@google.com msSingleton = new Log(log_file_name_); 2012870Sgabeblack@google.com } 2112870Sgabeblack@google.com } 2212870Sgabeblack@google.com 2312870Sgabeblack@google.com void Log::release() 2412870Sgabeblack@google.com { 2512870Sgabeblack@google.com if(msIsLog) 2612870Sgabeblack@google.com { 2712870Sgabeblack@google.com ASSERT(msSingleton, "Log singleton is not allocated"); 2812870Sgabeblack@google.com delete msSingleton; 2912870Sgabeblack@google.com msSingleton = NULL; 3012870Sgabeblack@google.com } 3112870Sgabeblack@google.com return; 3212870Sgabeblack@google.com } 3312870Sgabeblack@google.com 3412870Sgabeblack@google.com void Log::print(const String& str_) 3512870Sgabeblack@google.com { 3612870Sgabeblack@google.com if(msIsLog) 3712870Sgabeblack@google.com { 3812870Sgabeblack@google.com ASSERT(msSingleton, "Log singleton is not allocated"); 3912870Sgabeblack@google.com msSingleton->ofs << str_; 4012870Sgabeblack@google.com } 4112870Sgabeblack@google.com return; 4212870Sgabeblack@google.com } 4312870Sgabeblack@google.com 4412870Sgabeblack@google.com void Log::print(ostream& stream_, const String& str_) 4512870Sgabeblack@google.com { 4612870Sgabeblack@google.com if(msIsLog) 4712870Sgabeblack@google.com { 4812870Sgabeblack@google.com ASSERT(msSingleton, "Log singleton is not allocated"); 4912870Sgabeblack@google.com msSingleton->ofs << str_; 5012870Sgabeblack@google.com } 5112870Sgabeblack@google.com stream_ << str_; 5212870Sgabeblack@google.com return; 5312870Sgabeblack@google.com } 5412870Sgabeblack@google.com 5512870Sgabeblack@google.com void Log::printLine(const String& str_) 5612870Sgabeblack@google.com { 5712870Sgabeblack@google.com if(msIsLog) 5812870Sgabeblack@google.com { 5912870Sgabeblack@google.com ASSERT(msSingleton, "Log singleton is not allocated"); 6012870Sgabeblack@google.com msSingleton->ofs << str_ << endl; 6112870Sgabeblack@google.com } 6212870Sgabeblack@google.com return; 6312870Sgabeblack@google.com } 6412870Sgabeblack@google.com 6512870Sgabeblack@google.com void Log::printLine(ostream& stream_, const String& str_) 6612870Sgabeblack@google.com { 6712870Sgabeblack@google.com if(msIsLog) 6812870Sgabeblack@google.com { 6912870Sgabeblack@google.com ASSERT(msSingleton, "Log singleton is not allocated"); 7012870Sgabeblack@google.com msSingleton->ofs << str_ << endl; 7112870Sgabeblack@google.com } 7212870Sgabeblack@google.com stream_ << str_ << endl; 7312897Sgabeblack@google.com return; 7412870Sgabeblack@google.com } 7512870Sgabeblack@google.com 7612870Sgabeblack@google.com Log::Log(const String& log_file_name_) 7712870Sgabeblack@google.com { 7812870Sgabeblack@google.com ofs.open(log_file_name_.c_str()); 7912870Sgabeblack@google.com } 8012870Sgabeblack@google.com 8112870Sgabeblack@google.com Log::~Log() 8212870Sgabeblack@google.com { 8312870Sgabeblack@google.com ofs.close(); 8412870Sgabeblack@google.com } 8512870Sgabeblack@google.com} 8612870Sgabeblack@google.com 8712870Sgabeblack@google.com