Log.cc revision 10448
112837Sgabeblack@google.com/* Copyright (c) 2012 Massachusetts Institute of Technology 212837Sgabeblack@google.com * 312837Sgabeblack@google.com * Permission is hereby granted, free of charge, to any person obtaining a copy 412837Sgabeblack@google.com * of this software and associated documentation files (the "Software"), to deal 512837Sgabeblack@google.com * in the Software without restriction, including without limitation the rights 612837Sgabeblack@google.com * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 712837Sgabeblack@google.com * copies of the Software, and to permit persons to whom the Software is 812837Sgabeblack@google.com * furnished to do so, subject to the following conditions: 912837Sgabeblack@google.com * 1012837Sgabeblack@google.com * The above copyright notice and this permission notice shall be included in 1112837Sgabeblack@google.com * all copies or substantial portions of the Software. 1212837Sgabeblack@google.com * 1312837Sgabeblack@google.com * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1412837Sgabeblack@google.com * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1512837Sgabeblack@google.com * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1612837Sgabeblack@google.com * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1712837Sgabeblack@google.com * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1812837Sgabeblack@google.com * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 1912837Sgabeblack@google.com * THE SOFTWARE. 2012837Sgabeblack@google.com */ 2112837Sgabeblack@google.com 2212837Sgabeblack@google.com#include "Log.h" 2312837Sgabeblack@google.com 2412837Sgabeblack@google.com#include "Assert.h" 2512837Sgabeblack@google.com 2612837Sgabeblack@google.comnamespace LibUtil 2712837Sgabeblack@google.com{ 2812837Sgabeblack@google.com using std::ostream; 2912837Sgabeblack@google.com using std::endl; 3012901Sgabeblack@google.com 3112901Sgabeblack@google.com Log* Log::msSingleton = NULL; 3212901Sgabeblack@google.com const bool Log::msIsLog = LIBUTIL_IS_LOG; 3312837Sgabeblack@google.com 3412982Sgabeblack@google.com void Log::allocate(const String& log_file_name_) 3512951Sgabeblack@google.com { 3612953Sgabeblack@google.com if(msIsLog) 3712837Sgabeblack@google.com { 3812951Sgabeblack@google.com // Allocate static Config instance 3912837Sgabeblack@google.com ASSERT(!msSingleton, "Log singleton is allocated"); 4012952Sgabeblack@google.com msSingleton = new Log(log_file_name_); 4112952Sgabeblack@google.com } 4212952Sgabeblack@google.com } 4312952Sgabeblack@google.com 4412952Sgabeblack@google.com void Log::release() 4512952Sgabeblack@google.com { 4612952Sgabeblack@google.com if(msIsLog) 4712952Sgabeblack@google.com { 4812952Sgabeblack@google.com ASSERT(msSingleton, "Log singleton is not allocated"); 4912952Sgabeblack@google.com delete msSingleton; 5012952Sgabeblack@google.com msSingleton = NULL; 5112952Sgabeblack@google.com } 5212952Sgabeblack@google.com return; 5312952Sgabeblack@google.com } 5412952Sgabeblack@google.com 5512952Sgabeblack@google.com void Log::print(const String& str_) 5612952Sgabeblack@google.com { 5712952Sgabeblack@google.com if(msIsLog) 5812952Sgabeblack@google.com { 5912952Sgabeblack@google.com ASSERT(msSingleton, "Log singleton is not allocated"); 6012952Sgabeblack@google.com msSingleton->ofs << str_; 6112952Sgabeblack@google.com } 6212952Sgabeblack@google.com return; 6312837Sgabeblack@google.com } 6412837Sgabeblack@google.com 6512837Sgabeblack@google.com void Log::print(ostream& stream_, const String& str_) 6612951Sgabeblack@google.com { 6712951Sgabeblack@google.com if(msIsLog) 6812951Sgabeblack@google.com { 6912837Sgabeblack@google.com ASSERT(msSingleton, "Log singleton is not allocated"); 7012951Sgabeblack@google.com msSingleton->ofs << str_; 7112951Sgabeblack@google.com } 7212951Sgabeblack@google.com stream_ << str_; 7312837Sgabeblack@google.com return; 7412837Sgabeblack@google.com } 7512837Sgabeblack@google.com 7612982Sgabeblack@google.com void Log::printLine(const String& str_) 7712837Sgabeblack@google.com { 7812837Sgabeblack@google.com if(msIsLog) 7912837Sgabeblack@google.com { 8012837Sgabeblack@google.com ASSERT(msSingleton, "Log singleton is not allocated"); 8112837Sgabeblack@google.com msSingleton->ofs << str_ << endl; 8212837Sgabeblack@google.com } 8312837Sgabeblack@google.com return; 8412837Sgabeblack@google.com } 8512837Sgabeblack@google.com 8612837Sgabeblack@google.com void Log::printLine(ostream& stream_, const String& str_) 8712837Sgabeblack@google.com { 8812837Sgabeblack@google.com if(msIsLog) 8912837Sgabeblack@google.com { 9012837Sgabeblack@google.com ASSERT(msSingleton, "Log singleton is not allocated"); 9112837Sgabeblack@google.com msSingleton->ofs << str_ << endl; 9212837Sgabeblack@google.com } 9312837Sgabeblack@google.com stream_ << str_ << endl; 9412837Sgabeblack@google.com return; 9512837Sgabeblack@google.com } 9612837Sgabeblack@google.com 9712837Sgabeblack@google.com Log::Log(const String& log_file_name_) 9812837Sgabeblack@google.com { 9912837Sgabeblack@google.com ofs.open(log_file_name_.c_str()); 10012837Sgabeblack@google.com } 10112837Sgabeblack@google.com 10212837Sgabeblack@google.com Log::~Log() 10312837Sgabeblack@google.com { 10412837Sgabeblack@google.com ofs.close(); 10512837Sgabeblack@google.com } 10612837Sgabeblack@google.com} 10712837Sgabeblack@google.com 10812837Sgabeblack@google.com