diff --git a/ChangeLog b/ChangeLog index f741687c..c88e9076 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2009-07-18 Tatsuhiro Tsujikawa + + Rewritten Logger to avoiding constly virtual call. + * src/LogFactory.cc + * src/LogFactory.h + * src/Logger.h + * src/Makefile.am + * src/Makefile.in + * src/NullLogger.h: Removed. + * src/SimpleLogger.cc + * src/SimpleLogger.h + 2009-07-17 Tatsuhiro Tsujikawa Updated doc diff --git a/src/LogFactory.cc b/src/LogFactory.cc index aacbb43b..13f104b7 100644 --- a/src/LogFactory.cc +++ b/src/LogFactory.cc @@ -36,7 +36,6 @@ #include "SimpleLogger.h" #include "a2io.h" #include "prefs.h" -#include namespace aria2 { @@ -46,7 +45,7 @@ bool LogFactory::_consoleOutput = true; Logger::LEVEL LogFactory::_logLevel = Logger::DEBUG; Logger* LogFactory::getInstance() { - if(logger == NULL) { + if(!logger) { SimpleLogger* slogger = new SimpleLogger(); if(filename != DEV_NULL) { // don't open file DEV_NULL for performance sake. @@ -55,9 +54,9 @@ Logger* LogFactory::getInstance() { } slogger->setLogLevel(_logLevel); if(_consoleOutput) { - slogger->setStdout(Logger::NOTICE, true); - slogger->setStdout(Logger::WARN, true); - slogger->setStdout(Logger::ERROR, true); + slogger->setStdoutLogLevel(Logger::NOTICE, true); + slogger->setStdoutLogLevel(Logger::WARN, true); + slogger->setStdoutLogLevel(Logger::ERROR, true); } logger = slogger; } diff --git a/src/LogFactory.h b/src/LogFactory.h index 7ebfb037..cb71eaeb 100644 --- a/src/LogFactory.h +++ b/src/LogFactory.h @@ -36,9 +36,11 @@ #define _D_LOG_FACTORY_H_ #include "common.h" -#include "Logger.h" + #include +#include "Logger.h" + namespace aria2 { class LogFactory { diff --git a/src/Logger.h b/src/Logger.h index 7119df59..fe141ffd 100644 --- a/src/Logger.h +++ b/src/Logger.h @@ -37,24 +37,17 @@ #include "common.h" +#include +#include +#include +#include + namespace aria2 { class Exception; class Logger { public: - virtual ~Logger() {} - virtual void debug(const char* msg, ...) = 0; - virtual void debug(const char* msg, const Exception& ex, ...) = 0; - virtual void info(const char* msg, ...) = 0; - virtual void info(const char* msg, const Exception& ex, ...) = 0; - virtual void notice(const char* msg, ...) = 0; - virtual void notice(const char* msg, const Exception& ex, ...) = 0; - virtual void warn(const char* msg, ...) = 0; - virtual void warn(const char* msg, const Exception& ex, ...) = 0; - virtual void error(const char* msg, ...) = 0; - virtual void error(const char* msg, const Exception& ex, ...) = 0; - enum LEVEL { DEBUG = 1 << 0, INFO = 1 << 1, @@ -63,7 +56,137 @@ public: ERROR = 1 << 4, }; - virtual void setLogLevel(LEVEL level) = 0; + static const std::string DEBUG_LABEL; + + static const std::string NOTICE_LABEL; + + static const std::string WARN_LABEL; + + static const std::string ERROR_LABEL; + + static const std::string INFO_LABEL; +private: + LEVEL _logLevel; + + std::ofstream _file; + + int _stdoutField; +protected: + virtual void writeLog + (std::ostream& o, LEVEL logLevel, const std::string& logLevelLabel, + const char* msg, va_list ap) = 0; + + virtual void writeStackTrace + (std::ostream& o, LEVEL logLevel, const std::string& logLevelLabel, + const Exception& ex) = 0; +public: + Logger(); + + virtual ~Logger(); + +#define WRITE_LOG(LEVEL, LEVEL_LABEL, MSG) \ + if(LEVEL >= _logLevel && _file.is_open()) { \ + va_list ap; \ + va_start(ap, MSG); \ + writeLog(_file, LEVEL, LEVEL_LABEL, MSG, ap); \ + va_end(ap); \ + _file << std::flush; \ + } \ + if(_stdoutField&LEVEL) { \ + std::cout << "\n"; \ + va_list ap; \ + va_start(ap, MSG); \ + writeLog(std::cout, LEVEL, LEVEL_LABEL, MSG, ap); \ + va_end(ap); \ + std::cout << std::flush; \ + } \ + +#define WRITE_LOG_EX(LEVEL, LEVEL_LABEL, MSG, EX) \ + if(LEVEL >= _logLevel && _file.is_open()) { \ + va_list ap; \ + va_start(ap, EX); \ + writeLog(_file, LEVEL, LEVEL_LABEL, MSG, ap); \ + va_end(ap); \ + writeStackTrace(_file, LEVEL, LEVEL_LABEL, EX); \ + _file << std::flush; \ + } \ + if(_stdoutField&LEVEL) { \ + std::cout << "\n"; \ + va_list ap; \ + va_start(ap, EX); \ + writeLog(std::cout, LEVEL, LEVEL_LABEL, MSG, ap); \ + va_end(ap); \ + writeStackTrace(std::cout, LEVEL, LEVEL_LABEL, EX); \ + std::cout << std::flush; \ + } \ + + void debug(const char* msg, ...) + { + WRITE_LOG(DEBUG, DEBUG_LABEL, msg); + } + + void debug(const char* msg, const Exception& ex, ...) + { + WRITE_LOG_EX(DEBUG, DEBUG_LABEL, msg, ex); + } + + void info(const char* msg, ...) + { + WRITE_LOG(INFO, INFO_LABEL, msg); + } + + void info(const char* msg, const Exception& ex, ...) + { + WRITE_LOG_EX(INFO, INFO_LABEL, msg, ex); + } + + void notice(const char* msg, ...) + { + WRITE_LOG(NOTICE, NOTICE_LABEL, msg); + } + + void notice(const char* msg, const Exception& ex, ...) + { + WRITE_LOG_EX(NOTICE, NOTICE_LABEL, msg, ex); + } + + void warn(const char* msg, ...) + { + WRITE_LOG(WARN, WARN_LABEL, msg); + } + + void warn(const char* msg, const Exception& ex, ...) + { + WRITE_LOG_EX(WARN, WARN_LABEL, msg, ex); + } + + void error(const char* msg, ...) + { + WRITE_LOG(ERROR, ERROR_LABEL, msg); + } + + void error(const char* msg, const Exception& ex, ...) + { + WRITE_LOG_EX(ERROR, ERROR_LABEL, msg, ex); + } + + void openFile(const std::string& filename); + + void closeFile(); + + void setLogLevel(LEVEL level) + { + _logLevel = level; + } + + void setStdoutLogLevel(Logger::LEVEL level, bool enabled) + { + if(enabled) { + _stdoutField |= level; + } else { + _stdoutField &= ~level; + } + } }; } // namespace aria2 diff --git a/src/Makefile.am b/src/Makefile.am index 993e9331..b84623ac 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -40,7 +40,7 @@ SRCS = Socket.h\ DlAbortEx.h\ DlRetryEx.h\ DownloadFailureException.h\ - Logger.h\ + Logger.cc Logger.h\ SimpleLogger.cc SimpleLogger.h\ DiskWriter.h\ DiskWriterFactory.h\ @@ -51,7 +51,6 @@ SRCS = Socket.h\ Option.cc Option.h\ Base64.cc Base64.h\ LogFactory.cc LogFactory.h\ - NullLogger.h\ TimeA2.cc TimeA2.h\ SharedHandle.h\ HandleRegistry.h\ diff --git a/src/Makefile.in b/src/Makefile.in index 00806a3a..af944299 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -329,13 +329,13 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ SegmentMan.h Util.cc Util.h Request.cc Request.h common.h \ message.h Exception.cc Exception.h FatalException.h \ RecoverableException.h DlAbortEx.h DlRetryEx.h \ - DownloadFailureException.h Logger.h SimpleLogger.cc \ + DownloadFailureException.h Logger.cc Logger.h SimpleLogger.cc \ SimpleLogger.h DiskWriter.h DiskWriterFactory.h \ AbstractDiskWriter.cc AbstractDiskWriter.h \ DefaultDiskWriter.cc DefaultDiskWriter.h \ DefaultDiskWriterFactory.cc DefaultDiskWriterFactory.h File.cc \ File.h Option.cc Option.h Base64.cc Base64.h LogFactory.cc \ - LogFactory.h NullLogger.h TimeA2.cc TimeA2.h SharedHandle.h \ + LogFactory.h TimeA2.cc TimeA2.h SharedHandle.h \ HandleRegistry.h FeatureConfig.cc FeatureConfig.h \ DownloadEngineFactory.cc DownloadEngineFactory.h SpeedCalc.cc \ SpeedCalc.h PeerStat.h BitfieldMan.cc BitfieldMan.h \ @@ -787,11 +787,11 @@ am__objects_26 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \ FtpTunnelResponseCommand.$(OBJEXT) SleepCommand.$(OBJEXT) \ DownloadEngine.$(OBJEXT) GrowSegment.$(OBJEXT) \ PiecedSegment.$(OBJEXT) SegmentMan.$(OBJEXT) Util.$(OBJEXT) \ - Request.$(OBJEXT) Exception.$(OBJEXT) SimpleLogger.$(OBJEXT) \ - AbstractDiskWriter.$(OBJEXT) DefaultDiskWriter.$(OBJEXT) \ - DefaultDiskWriterFactory.$(OBJEXT) File.$(OBJEXT) \ - Option.$(OBJEXT) Base64.$(OBJEXT) LogFactory.$(OBJEXT) \ - TimeA2.$(OBJEXT) FeatureConfig.$(OBJEXT) \ + Request.$(OBJEXT) Exception.$(OBJEXT) Logger.$(OBJEXT) \ + SimpleLogger.$(OBJEXT) AbstractDiskWriter.$(OBJEXT) \ + DefaultDiskWriter.$(OBJEXT) DefaultDiskWriterFactory.$(OBJEXT) \ + File.$(OBJEXT) Option.$(OBJEXT) Base64.$(OBJEXT) \ + LogFactory.$(OBJEXT) TimeA2.$(OBJEXT) FeatureConfig.$(OBJEXT) \ DownloadEngineFactory.$(OBJEXT) SpeedCalc.$(OBJEXT) \ BitfieldMan.$(OBJEXT) BitfieldManFactory.$(OBJEXT) \ SimpleRandomizer.$(OBJEXT) HttpResponse.$(OBJEXT) \ @@ -1085,13 +1085,13 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \ SegmentMan.h Util.cc Util.h Request.cc Request.h common.h \ message.h Exception.cc Exception.h FatalException.h \ RecoverableException.h DlAbortEx.h DlRetryEx.h \ - DownloadFailureException.h Logger.h SimpleLogger.cc \ + DownloadFailureException.h Logger.cc Logger.h SimpleLogger.cc \ SimpleLogger.h DiskWriter.h DiskWriterFactory.h \ AbstractDiskWriter.cc AbstractDiskWriter.h \ DefaultDiskWriter.cc DefaultDiskWriter.h \ DefaultDiskWriterFactory.cc DefaultDiskWriterFactory.h File.cc \ File.h Option.cc Option.h Base64.cc Base64.h LogFactory.cc \ - LogFactory.h NullLogger.h TimeA2.cc TimeA2.h SharedHandle.h \ + LogFactory.h TimeA2.cc TimeA2.h SharedHandle.h \ HandleRegistry.h FeatureConfig.cc FeatureConfig.h \ DownloadEngineFactory.cc DownloadEngineFactory.h SpeedCalc.cc \ SpeedCalc.h PeerStat.h BitfieldMan.cc BitfieldMan.h \ @@ -1469,6 +1469,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LibgnutlsTLSContext.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LibsslTLSContext.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LogFactory.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Logger.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LongestSequencePieceSelector.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MSEHandshake.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MemoryBufferPreDownloadHandler.Po@am__quote@ diff --git a/src/NullLogger.h b/src/NullLogger.h deleted file mode 100644 index d113d2f8..00000000 --- a/src/NullLogger.h +++ /dev/null @@ -1,56 +0,0 @@ -/* */ -#ifndef _D_NULL_LOGGER_H_ -#define _D_NULL_LOGGER_H_ - -#include "Logger.h" - -class NullLogger : public Logger { -public: - NullLogger() {} - virtual ~NullLogger() {} - virtual void debug(const char* msg, ...) const {} - virtual void debug(const char* msg, Exception& ex, ...) const {} - virtual void info(const char* msg, ...) const {} - virtual void info(const char* msg, Exception& ex, ...) const {} - virtual void notice(const char* msg, ...) const {} - virtual void notice(const char* msg, Exception& ex, ...) const {} - virtual void warn(const char* msg, ...) const {} - virtual void warn(const char* msg, Exception& ex, ...) const {} - virtual void error(const char* msg, ...) const {} - virtual void error(const char* msg, Exception& ex, ...) const {} -}; - -#endif // _D_NULL_LOGGER_H_ diff --git a/src/SimpleLogger.cc b/src/SimpleLogger.cc index 0a16d344..c3be2526 100644 --- a/src/SimpleLogger.cc +++ b/src/SimpleLogger.cc @@ -33,116 +33,27 @@ */ /* copyright --> */ #include "SimpleLogger.h" -#include "Util.h" -#include "DlAbortEx.h" -#include "message.h" -#include "a2io.h" -#include "a2time.h" -#include "StringFormat.h" -#include "A2STR.h" -#include -#include -#include -#include + #include +#include "Util.h" +#include "a2time.h" +#include "A2STR.h" +#include "StringFormat.h" +#include "Exception.h" + namespace aria2 { -#if !defined(va_copy) -# if defined(__va_copy) -# define va_copy(dest, src) __va_copy(dest, src) -# else -# define va_copy(dest, src) (dest = src) -# endif -#endif - -#define WRITE_LOG(LEVEL, MSG) \ - if(LEVEL >= _logLevel && (stdoutField&LEVEL || file.is_open())) { \ - va_list ap; \ - va_start(ap, MSG); \ - writeFile(LEVEL, MSG, ap); \ - flush(); \ - va_end(ap); \ - } - -#define WRITE_LOG_EX(LEVEL, MSG, EX) \ - if(LEVEL >= _logLevel && (stdoutField&LEVEL || file.is_open())) { \ - va_list ap; \ - va_start(ap, EX); \ - writeFile(LEVEL, MSG, ap); \ - writeStackTrace(LEVEL, EX); \ - flush(); \ - va_end(ap); \ -} - -const std::string SimpleLogger::DEBUG("DEBUG"); - -const std::string SimpleLogger::NOTICE("NOTICE"); - -const std::string SimpleLogger::WARN("WARN"); - -const std::string SimpleLogger::ERROR("ERROR"); - -const std::string SimpleLogger::INFO("INFO"); - -SimpleLogger::SimpleLogger():stdoutField(0), _logLevel(Logger::DEBUG) {} - -SimpleLogger::~SimpleLogger() { - closeFile(); -} - -void SimpleLogger::openFile(const std::string& filename) { - file.open(filename.c_str(), std::ios::app|std::ios::binary); - if(!file) { - throw DL_ABORT_EX - (StringFormat(EX_FILE_OPEN, filename.c_str(), strerror(errno)).str()); - } -} - -void SimpleLogger::closeFile() { - if(file.is_open()) { - file.close(); - } -} - -void SimpleLogger::setStdout(Logger::LEVEL level, bool enabled) { - if(enabled) { - stdoutField |= level; - } else { - stdoutField &= ~level; - } -} - -void SimpleLogger::writeHeader(std::ostream& o, const std::string& date, - const std::string& level) +static void writeHeader +(std::ostream& o, const std::string& date, const std::string& logLevelLabel) { - o << StringFormat("%s %s - ", date.c_str(), level.c_str()); + o << StringFormat("%s %s - ", date.c_str(), logLevelLabel.c_str()); } -void SimpleLogger::writeLog(std::ostream& o, Logger::LEVEL level, - const char* msg, va_list ap, - bool printHeader) +void SimpleLogger::writeLog +(std::ostream& o, Logger::LEVEL level, const std::string& logLevelLabel, + const char* msg, va_list ap) { - va_list apCopy; - va_copy(apCopy, ap); - std::string levelStr; - switch(level) { - case Logger::DEBUG: - levelStr = DEBUG; - break; - case Logger::NOTICE: - levelStr = NOTICE; - break; - case Logger::WARN: - levelStr = WARN; - break; - case Logger::ERROR: - levelStr = ERROR; - break; - case Logger::INFO: - default: - levelStr = INFO; - } struct timeval tv; gettimeofday(&tv, 0); char datestr[27]; // 'YYYY-MM-DD hh:mm:ss.uuuuuu'+'\0' = 27 bytes @@ -155,88 +66,24 @@ void SimpleLogger::writeLog(std::ostream& o, Logger::LEVEL level, assert(dateLength <= (size_t)20); snprintf(datestr+dateLength, sizeof(datestr)-dateLength, ".%06ld", tv.tv_usec); - - // TODO a quick hack not to print header in console - if(printHeader) { - writeHeader(o, datestr, levelStr); - } + writeHeader(o, datestr, logLevelLabel); { char buf[1024]; - if(vsnprintf(buf, sizeof(buf), std::string(Util::replace(msg, A2STR::CR_C, A2STR::NIL)+A2STR::LF_C).c_str(), apCopy) < 0) { + std::string body = Util::replace(msg, A2STR::CR_C, A2STR::NIL); + body += A2STR::LF_C; + if(vsnprintf(buf, sizeof(buf), body.c_str(), ap) < 0) { o << "SimpleLogger error, failed to format message.\n"; } else { o << buf; } } - va_end(apCopy); } -void SimpleLogger::writeFile(Logger::LEVEL level, const char* msg, va_list ap) +void SimpleLogger::writeStackTrace +(std::ostream& o, Logger::LEVEL level, const std::string& logLevelLabel, + const Exception& e) { - writeLog(file, level, msg, ap); - if(stdoutField&level) { - std::cout << "\n"; - writeLog(std::cout, level, msg, ap); - } -} - -void SimpleLogger::writeStackTrace(Logger::LEVEL level, const Exception& e) -{ - file << e.stackTrace(); - if(stdoutField&level) { - std::cout << e.stackTrace(); - } -} - -void SimpleLogger::flush() -{ - file << std::flush; - std::cout << std::flush; -} - -void SimpleLogger::debug(const char* msg, ...) { - WRITE_LOG(Logger::DEBUG, msg); -} - -void SimpleLogger::debug(const char* msg, const Exception& e, ...) { - WRITE_LOG_EX(Logger::DEBUG, msg, e); -} - -void SimpleLogger::info(const char* msg, ...) { - WRITE_LOG(Logger::INFO, msg); -} - -void SimpleLogger::info(const char* msg, const Exception& e, ...) { - WRITE_LOG_EX(Logger::INFO, msg, e); -} - -void SimpleLogger::notice(const char* msg, ...) { - WRITE_LOG(Logger::NOTICE, msg); -} - -void SimpleLogger::notice(const char* msg, const Exception& e, ...) { - WRITE_LOG_EX(Logger::INFO, msg, e); -} - -void SimpleLogger::warn(const char* msg, ...) { - WRITE_LOG(Logger::WARN, msg); -} - -void SimpleLogger::warn(const char* msg, const Exception& e, ...) { - WRITE_LOG_EX(Logger::WARN, msg, e); -} - -void SimpleLogger::error(const char* msg, ...) { - WRITE_LOG(Logger::ERROR, msg); -} - -void SimpleLogger::error(const char* msg, const Exception& e, ...) { - WRITE_LOG_EX(Logger::ERROR, msg, e); -} - -void SimpleLogger::setLogLevel(Logger::LEVEL level) -{ - _logLevel = level; + o << e.stackTrace(); } } // namespace aria2 diff --git a/src/SimpleLogger.h b/src/SimpleLogger.h index 5789b3dc..ebd0e450 100644 --- a/src/SimpleLogger.h +++ b/src/SimpleLogger.h @@ -36,61 +36,18 @@ #define _D_SIMPLE_LOGGER_H_ #include "Logger.h" -#include -#include -#include namespace aria2 { class SimpleLogger:public Logger { -private: - void writeFile(Logger::LEVEL level, const char* msg, va_list ap); +protected: + virtual void writeLog + (std::ostream& out, Logger::LEVEL logLevel, const std::string& logLevelLabel, + const char* msg, va_list ap); - void writeStackTrace(Logger::LEVEL level, const Exception& e); - - void flush(); - - void writeHeader(std::ostream& out, - const std::string& date, const std::string& level); - - void writeLog(std::ostream& out, Logger::LEVEL level, - const char* msg, va_list ap, - bool printHeader = true); - - std::ofstream file; - int stdoutField; - - Logger::LEVEL _logLevel; - - static const std::string DEBUG; - - static const std::string NOTICE; - - static const std::string WARN; - - static const std::string ERROR; - - static const std::string INFO; -public: - SimpleLogger(); - ~SimpleLogger(); - - void openFile(const std::string& filename); - void closeFile(); - virtual void debug(const char* msg, ...); - virtual void debug(const char* msg, const Exception& ex, ...); - virtual void info(const char* msg, ...); - virtual void info(const char* msg, const Exception& ex, ...); - virtual void notice(const char* msg, ...); - virtual void notice(const char* msg, const Exception& ex, ...); - virtual void warn(const char* msg, ...); - virtual void warn(const char* msg, const Exception& ex, ...); - virtual void error(const char* msg, ...); - virtual void error(const char* msg, const Exception& ex, ...); - - virtual void setLogLevel(Logger::LEVEL level); - - void setStdout(Logger::LEVEL level, bool enabled); + virtual void writeStackTrace + (std::ostream& out, Logger::LEVEL logLevel, const std::string& logLevelLabel, + const Exception& e); }; } // namespace aria2