📄 logwriter.cpp
字号:
/*============================================================================. | Copyright (C) 2006 Gareth Buxton | |----------------------------------------------------------------------------| | LogPlusPlus is free software; you can redistribute it and/or | | modify it under the terms of the GNU Lesser General Public | | License as published by the Free Software Foundation; either | | version 2.1 of the License, or (at your option) any later version. | | | | LogPlusPlus is distributed in the hope that it will be useful, | | but WITHOUT ANY WARRANTY; without even the implied warranty of | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | | Lesser General Public License for more details. | | | | You should have received a copy of the GNU Lesser General Public | | License along with this library; if not, write to the Free Software | | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | '============================================================================*/#include <liblpp/Log.h>#include "LogWriter.h"#include "LogOutput.h"//=============================================================================LPP_NAMESPACE_BEGIN//=============================================================================//Log& LogWriter::log = Log::getLog();LogWriter::LogWriter(): isDone(false), thread(0), threadMill(0){}LogWriter::~LogWriter(){// std::cout << "~LogWriter()" << std::endl; stopThread();// std::cout << "thread stopped" << std::endl; LogOutputMap::iterator i; for(i = logOutputMap.begin(); i != logOutputMap.end(); i++) { delete (*i).second; }}void LogWriter::flushQueue(){ while(messageQueue.size() > 0) { LogMessage& message = messageQueue.front(); LogOutputMap::const_iterator i; for(i = logOutputMap.begin(); i != logOutputMap.end(); i++) { (*i).second->write(message); } messageQueue.pop(); }}void LogWriter::run(){ thread->lock(); while(!isDone) { thread->wait(); flushQueue(); } thread->unlock();}void LogWriter::flush(){ if(thread) { thread->signal(); thread->yield(); }} void LogWriter::end(){ isDone = true; flush();}void LogWriter::setThreadMill(const ThreadMill* threadMill){ this->threadMill = threadMill;}void LogWriter::clrThreadMill(){ this->threadMill = 0;}void LogWriter::startThread(){ if(threadMill) // user wants threading { if(!thread) // Have no threading { thread = threadMill->create(*this); thread->start(); } }}void LogWriter::stopThread(){ if(thread) { //std::cout << "pre stop()" << std::endl; thread->stop(); // Request end of processing //std::cout << "pre join()" << std::endl; thread->join(); // Wait for termination //std::cout << "pre delete()" << std::endl; delete thread; // clean up //std::cout << "post delete()" << std::endl; thread = 0; // ensure state }}void LogWriter::addOutput(const std::string& name, const LogMask& mask, std::ostream& os){ LogOutput* output = logOutputMap[name]; if(!output) { output = new LogOutput(name, mask); output->setLogForm(Log::configuredLogForm()); output->attachOstream(os); logOutputMap[name] = output; } else { //log.error << "output " << name << " already exists."; //log.error << std::endl; }}void LogWriter::setLogMask(const std::string& name, const LogMask& mask, ModType modType){ LogOutput* output = logOutputMap[name]; if(output) { output->setLogMask(mask, modType); } else { //log.error << "Attempt to modify non-existent output " << name; //log.error << std::endl; }}LogMask LogWriter::getLogMask(const std::string& name){ LogOutput* output = logOutputMap[name]; if(output) { return output->getLogMask(); } //log.error << "Attempt to modify non-existent output " << name; //log.error << std::endl; return LogMask(0UL);}void LogWriter::setLogForm(const std::string& name, const LogForm* const format){ LogOutput* output = logOutputMap[name]; if(output) { output->setLogForm(format); } else {// log.error << "Attempt to re-format a non-existent output " << name;// log.error << std::endl; }}void LogWriter::delOutput(const std::string& name){ LogOutput* output = logOutputMap[name]; if(output) { logOutputMap.erase(name); } else { //log.error << "Attempt to delete non-existent output " << name; //log.error << std::endl; }}void LogWriter::attachOstream(const std::string& name, std::ostream& os){ LogOutput* output = logOutputMap[name]; if(output) { output->attachOstream(os); } else { //log.error << "Attempt to attach an ostream to non-existent output "; //log.error << name << std::endl; }}void LogWriter::removeOstream(const std::string& name, std::ostream& os){ LogOutput* output = logOutputMap[name]; if(output) { output->removeOstream(os); } else { //log.error << "Attempt to remove an ostream from non-existent output "; //log.error << name << std::endl; }}void LogWriter::write(LogMessage& message){ if(thread) // Add them to the messageQueue { thread->lock(); messageQueue.push(message); thread->unlock(); if(messageQueue.size() > 10) { flush();// thread->signal();// thread->yield(); } } else { LogOutputMap::const_iterator i; for(i = logOutputMap.begin(); i != logOutputMap.end(); i++) { (*i).second->write(message); } }}//=============================================================================LPP_NAMESPACE_END//=============================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -