diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2010-06-23 14:15:35 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2010-06-23 14:15:35 +0000 |
| commit | 66660d10c2fec52b26bbe91bb09083c355600c79 (patch) | |
| tree | f6dc06be261e21e3df40002a8bc0bfb608256cd3 /src | |
| parent | 4736e77acfc728176591f677cd2c973ef1d71098 (diff) | |
2010-06-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Made log and log-level option modifiable using
aria2.changeGlobalOption. This means you can dynamically start or
stop logging and change log file and log level.
* doc/aria2c.1.txt
* src/LogFactory.cc
* src/LogFactory.h
* src/LogFormatter.h
* src/Logger.cc
* src/Logger.h
* src/Makefile.am
* src/Makefile.in
* src/SimpleLogFormatter.cc
* src/SimpleLogFormatter.h
* src/SimpleLogger.cc: Removed
* src/SimpleLogger.h: Removed
* src/XmlRpcMethod.cc
* src/XmlRpcMethodImpl.cc
* src/main.cc
Diffstat (limited to 'src')
| -rw-r--r-- | src/LogFactory.cc | 50 | ||||
| -rw-r--r-- | src/LogFactory.h | 12 | ||||
| -rw-r--r-- | src/LogFormatter.h | 65 | ||||
| -rw-r--r-- | src/Logger.cc | 29 | ||||
| -rw-r--r-- | src/Logger.h | 15 | ||||
| -rw-r--r-- | src/Makefile.am | 3 | ||||
| -rw-r--r-- | src/Makefile.in | 16 | ||||
| -rw-r--r-- | src/SimpleLogFormatter.cc (renamed from src/SimpleLogger.cc) | 13 | ||||
| -rw-r--r-- | src/SimpleLogFormatter.h (renamed from src/SimpleLogger.h) | 23 | ||||
| -rw-r--r-- | src/XmlRpcMethod.cc | 2 | ||||
| -rw-r--r-- | src/XmlRpcMethodImpl.cc | 12 | ||||
| -rw-r--r-- | src/main.cc | 8 |
12 files changed, 200 insertions, 48 deletions
diff --git a/src/LogFactory.cc b/src/LogFactory.cc index a7b99889..d4008336 100644 --- a/src/LogFactory.cc +++ b/src/LogFactory.cc @@ -33,9 +33,10 @@ */ /* copyright --> */ #include "LogFactory.h" -#include "SimpleLogger.h" +#include "SimpleLogFormatter.h" #include "a2io.h" #include "prefs.h" +#include "RecoverableException.h" namespace aria2 { @@ -44,15 +45,39 @@ Logger* LogFactory::logger_ = 0; bool LogFactory::consoleOutput_ = true; Logger::LEVEL LogFactory::logLevel_ = Logger::A2_DEBUG; +void LogFactory::openLogger(Logger* logger) +{ + if(filename_ != DEV_NULL) { + // don't open file DEV_NULL for performance sake. + // This avoids costly unecessary message formatting and write. + logger->openFile(filename_); + } + logger->setLogLevel(logLevel_); +} + +void LogFactory::reconfigure() +{ + if(logger_) { + logger_->closeFile(); + try { + openLogger(logger_); + } catch(RecoverableException& e) { + logger_->closeFile(); + throw; + } + } +} + Logger* LogFactory::getInstance() { if(!logger_) { - SimpleLogger* slogger = new SimpleLogger(); - if(filename_ != DEV_NULL) { - // don't open file DEV_NULL for performance sake. - // This avoids costly unecessary message formatting and write. - slogger->openFile(filename_); + Logger* slogger = new Logger(); + slogger->setLogFormatter(new SimpleLogFormatter()); + try { + openLogger(slogger); + } catch(RecoverableException& e) { + delete slogger; + throw; } - slogger->setLogLevel(logLevel_); if(consoleOutput_) { slogger->setStdoutLogLevel(Logger::A2_NOTICE, true); slogger->setStdoutLogLevel(Logger::A2_WARN, true); @@ -63,6 +88,17 @@ Logger* LogFactory::getInstance() { return logger_; } +void LogFactory::setLogFile(const std::string& name) +{ + if(name == "-") { + filename_ = DEV_STDOUT; + } else if(name == "") { + filename_ = DEV_NULL; + } else { + filename_ = name; + } +} + void LogFactory::setLogLevel(Logger::LEVEL level) { logLevel_ = level; diff --git a/src/LogFactory.h b/src/LogFactory.h index fa3e3d28..02fc0f90 100644 --- a/src/LogFactory.h +++ b/src/LogFactory.h @@ -49,6 +49,8 @@ private: static Logger* logger_; static bool consoleOutput_; static Logger::LEVEL logLevel_; + + static void openLogger(Logger* logger); public: /** * Get logger instance. Returned logger is singleton. @@ -57,11 +59,10 @@ public: static Logger* getInstance(); /** - * Set a filename to write log. + * Set a filename to write log. If name is "-", log is written to + * stdout. If name is "", log is not written to file. */ - static void setLogFile(const std::string& name) { - filename_ = name; - } + static void setLogFile(const std::string& name); /** * Set flag whether the log is printed in console. @@ -71,7 +72,6 @@ public: consoleOutput_ = f; } - /** * Set log level to output. */ @@ -87,6 +87,8 @@ public: * Releases used resources */ static void release(); + + static void reconfigure(); }; } // namespace aria2 diff --git a/src/LogFormatter.h b/src/LogFormatter.h new file mode 100644 index 00000000..7f872298 --- /dev/null +++ b/src/LogFormatter.h @@ -0,0 +1,65 @@ +/* <!-- copyright */ +/* + * aria2 - The high speed download utility + * + * Copyright (C) 2010 Tatsuhiro Tsujikawa + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of portions of this program with the + * OpenSSL library under certain conditions as described in each + * individual source file, and distribute linked combinations + * including the two. + * You must obey the GNU General Public License in all respects + * for all of the code used other than OpenSSL. If you modify + * file(s) with this exception, you may extend this exception to your + * version of the file(s), but you are not obligated to do so. If you + * do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source + * files in the program, then also delete it here. + */ +/* copyright --> */ +#ifndef D_LOG_FORMATTER_H +#define D_LOG_FORMATTER_H + +#include "common.h" + +#include <cstdarg> +#include <iosfwd> +#include <string> + +#include "Logger.h" + +namespace aria2 { + +class Exception; + +class LogFormatter { +public: + virtual ~LogFormatter() {} + + virtual void writeLog + (std::ostream& o, Logger::LEVEL logLevel, const std::string& logLevelLabel, + const char* msg, va_list ap) = 0; + + virtual void writeStackTrace + (std::ostream& o, Logger::LEVEL logLevel, const std::string& logLevelLabel, + const Exception& ex) = 0; +}; + +} // namespace aria2 + +#endif // D_LOG_FORMATTER_H diff --git a/src/Logger.cc b/src/Logger.cc index 09eb546f..6048f450 100644 --- a/src/Logger.cc +++ b/src/Logger.cc @@ -40,6 +40,7 @@ #include "DlAbortEx.h" #include "StringFormat.h" #include "message.h" +#include "LogFormatter.h" namespace aria2 { @@ -53,11 +54,13 @@ const std::string Logger::ERROR_LABEL("ERROR"); const std::string Logger::INFO_LABEL("INFO"); -Logger::Logger():logLevel_(Logger::A2_DEBUG), stdoutField_(0) {} +Logger::Logger(): + logFormatter_(0), logLevel_(Logger::A2_DEBUG), stdoutField_(0) {} Logger::~Logger() { closeFile(); + delete logFormatter_; } void Logger::openFile(const std::string& filename) @@ -162,6 +165,12 @@ void Logger::error(const char* msg, const Exception& ex, ...) WRITE_LOG_EX(A2_ERROR, ERROR_LABEL, msg, ex); } +void Logger::setLogFormatter(LogFormatter* logFormatter) +{ + delete logFormatter_; + logFormatter_ = logFormatter; +} + void Logger::setStdoutLogLevel(Logger::LEVEL level, bool enabled) { if(enabled) { @@ -171,4 +180,22 @@ void Logger::setStdoutLogLevel(Logger::LEVEL level, bool enabled) } } +void Logger::writeLog +(std::ostream& o, LEVEL logLevel, const std::string& logLevelLabel, + const char* msg, va_list ap) +{ + if(logFormatter_) { + logFormatter_->writeLog(o, logLevel, logLevelLabel, msg, ap); + } +} + +void Logger::writeStackTrace +(std::ostream& o, LEVEL logLevel, const std::string& logLevelLabel, + const Exception& ex) +{ + if(logFormatter_) { + logFormatter_->writeStackTrace(o, logLevel, logLevelLabel, ex); + } +} + } // namespace aria2 diff --git a/src/Logger.h b/src/Logger.h index 55d75185..4336175a 100644 --- a/src/Logger.h +++ b/src/Logger.h @@ -45,6 +45,7 @@ namespace aria2 { class Exception; +class LogFormatter; class Logger { public: @@ -66,6 +67,8 @@ public: static const std::string INFO_LABEL; private: + LogFormatter* logFormatter_; + LEVEL logLevel_; std::ofstream file_; @@ -76,14 +79,14 @@ private: { return (level >= logLevel_ && file_.is_open()) || stdoutField_&level; } -protected: - virtual void writeLog + + void writeLog (std::ostream& o, LEVEL logLevel, const std::string& logLevelLabel, - const char* msg, va_list ap) = 0; + const char* msg, va_list ap); - virtual void writeStackTrace + void writeStackTrace (std::ostream& o, LEVEL logLevel, const std::string& logLevelLabel, - const Exception& ex) = 0; + const Exception& ex); public: Logger(); @@ -113,6 +116,8 @@ public: void closeFile(); + void setLogFormatter(LogFormatter* logFormatter); + void setLogLevel(LEVEL level) { logLevel_ = level; diff --git a/src/Makefile.am b/src/Makefile.am index 2632de62..430af281 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -40,7 +40,8 @@ SRCS = Socket.h\ DlRetryEx.cc DlRetryEx.h\ DownloadFailureException.cc DownloadFailureException.h\ Logger.cc Logger.h\ - SimpleLogger.cc SimpleLogger.h\ + LogFormatter.h\ + SimpleLogFormatter.cc SimpleLogFormatter.h\ DiskWriter.h\ DiskWriterFactory.h\ AbstractDiskWriter.cc AbstractDiskWriter.h\ diff --git a/src/Makefile.in b/src/Makefile.in index a49dd613..f77d3cd9 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -342,9 +342,9 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \ FatalException.h RecoverableException.cc \ RecoverableException.h DlAbortEx.cc DlAbortEx.h DlRetryEx.cc \ DlRetryEx.h DownloadFailureException.cc \ - DownloadFailureException.h Logger.cc Logger.h SimpleLogger.cc \ - SimpleLogger.h DiskWriter.h DiskWriterFactory.h \ - AbstractDiskWriter.cc AbstractDiskWriter.h \ + DownloadFailureException.h Logger.cc Logger.h LogFormatter.h \ + SimpleLogFormatter.cc SimpleLogFormatter.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 base32.cc \ @@ -815,7 +815,7 @@ am__objects_32 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \ Request.$(OBJEXT) Exception.$(OBJEXT) FatalException.$(OBJEXT) \ RecoverableException.$(OBJEXT) DlAbortEx.$(OBJEXT) \ DlRetryEx.$(OBJEXT) DownloadFailureException.$(OBJEXT) \ - Logger.$(OBJEXT) SimpleLogger.$(OBJEXT) \ + Logger.$(OBJEXT) SimpleLogFormatter.$(OBJEXT) \ AbstractDiskWriter.$(OBJEXT) DefaultDiskWriter.$(OBJEXT) \ DefaultDiskWriterFactory.$(OBJEXT) File.$(OBJEXT) \ Option.$(OBJEXT) Base64.$(OBJEXT) base32.$(OBJEXT) \ @@ -1122,9 +1122,9 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \ FatalException.h RecoverableException.cc \ RecoverableException.h DlAbortEx.cc DlAbortEx.h DlRetryEx.cc \ DlRetryEx.h DownloadFailureException.cc \ - DownloadFailureException.h Logger.cc Logger.h SimpleLogger.cc \ - SimpleLogger.h DiskWriter.h DiskWriterFactory.h \ - AbstractDiskWriter.cc AbstractDiskWriter.h \ + DownloadFailureException.h Logger.cc Logger.h LogFormatter.h \ + SimpleLogFormatter.cc SimpleLogFormatter.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 base32.cc \ @@ -1594,7 +1594,7 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SessionSerializer.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Signature.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SimpleBtMessage.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SimpleLogger.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SimpleLogFormatter.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SimpleRandomizer.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SingleFileAllocationIterator.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/SleepCommand.Po@am__quote@ diff --git a/src/SimpleLogger.cc b/src/SimpleLogFormatter.cc index 4b3382e5..fc7ee41e 100644 --- a/src/SimpleLogger.cc +++ b/src/SimpleLogFormatter.cc @@ -2,7 +2,7 @@ /* * aria2 - The high speed download utility * - * Copyright (C) 2006 Tatsuhiro Tsujikawa + * Copyright (C) 2010 Tatsuhiro Tsujikawa * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,9 +32,10 @@ * files in the program, then also delete it here. */ /* copyright --> */ -#include "SimpleLogger.h" +#include "SimpleLogFormatter.h" #include <cassert> +#include <ostream> #include "util.h" #include "a2time.h" @@ -44,13 +45,17 @@ namespace aria2 { +SimpleLogFormatter::SimpleLogFormatter() {} + +SimpleLogFormatter::~SimpleLogFormatter() {} + static void writeHeader (std::ostream& o, const std::string& date, const std::string& logLevelLabel) { o << StringFormat("%s %s - ", date.c_str(), logLevelLabel.c_str()); } -void SimpleLogger::writeLog +void SimpleLogFormatter::writeLog (std::ostream& o, Logger::LEVEL level, const std::string& logLevelLabel, const char* msg, va_list ap) { @@ -79,7 +84,7 @@ void SimpleLogger::writeLog } } -void SimpleLogger::writeStackTrace +void SimpleLogFormatter::writeStackTrace (std::ostream& o, Logger::LEVEL level, const std::string& logLevelLabel, const Exception& e) { diff --git a/src/SimpleLogger.h b/src/SimpleLogFormatter.h index 603ddaa5..5819a500 100644 --- a/src/SimpleLogger.h +++ b/src/SimpleLogFormatter.h @@ -2,7 +2,7 @@ /* * aria2 - The high speed download utility * - * Copyright (C) 2006 Tatsuhiro Tsujikawa + * Copyright (C) 2010 Tatsuhiro Tsujikawa * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,25 +32,28 @@ * files in the program, then also delete it here. */ /* copyright --> */ -#ifndef _D_SIMPLE_LOGGER_H_ -#define _D_SIMPLE_LOGGER_H_ +#ifndef D_SIMPLE_LOG_FORMATTER_H +#define D_SIMPLE_LOG_FORMATTER_H -#include "Logger.h" +#include "LogFormatter.h" namespace aria2 { -class SimpleLogger:public Logger { -protected: +class SimpleLogFormatter:public LogFormatter { +public: + SimpleLogFormatter(); + + virtual ~SimpleLogFormatter(); + virtual void writeLog - (std::ostream& out, Logger::LEVEL logLevel, const std::string& logLevelLabel, + (std::ostream& o, Logger::LEVEL logLevel, const std::string& logLevelLabel, const char* msg, va_list ap); virtual void writeStackTrace - (std::ostream& out, Logger::LEVEL logLevel, const std::string& logLevelLabel, + (std::ostream& o, Logger::LEVEL logLevel, const std::string& logLevelLabel, const Exception& e); }; } // namespace aria2 -#endif // _D_SIMPLE_LOGGER_H_ - +#endif // D_SIMPLE_LOG_FORMATTER_H diff --git a/src/XmlRpcMethod.cc b/src/XmlRpcMethod.cc index fc305fdf..56781374 100644 --- a/src/XmlRpcMethod.cc +++ b/src/XmlRpcMethod.cc @@ -186,6 +186,8 @@ const std::set<std::string>& listChangeableGlobalOptions() PREF_MAX_OVERALL_UPLOAD_LIMIT, PREF_MAX_OVERALL_DOWNLOAD_LIMIT, PREF_MAX_CONCURRENT_DOWNLOADS, + PREF_LOG, + PREF_LOG_LEVEL }; static std::set<std::string> options(vbegin(OPTIONS), vend(OPTIONS)); return options; diff --git a/src/XmlRpcMethodImpl.cc b/src/XmlRpcMethodImpl.cc index d310b372..4cb2e843 100644 --- a/src/XmlRpcMethodImpl.cc +++ b/src/XmlRpcMethodImpl.cc @@ -964,6 +964,18 @@ SharedHandle<ValueBase> ChangeGlobalOptionXmlRpcMethod::process (option->getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS)); e->getRequestGroupMan()->requestQueueCheck(); } + if(option->defined(PREF_LOG_LEVEL)) { + LogFactory::setLogLevel(option->get(PREF_LOG_LEVEL)); + } + if(option->defined(PREF_LOG)) { + LogFactory::setLogFile(option->get(PREF_LOG)); + try { + LogFactory::reconfigure(); + } catch(RecoverableException& e) { + // TODO no exception handling + } + } + return VLB_OK; } diff --git a/src/main.cc b/src/main.cc index e2fef629..4f91c3bf 100644 --- a/src/main.cc +++ b/src/main.cc @@ -175,13 +175,7 @@ downloadresultcode::RESULT main(int argc, char* argv[]) #ifdef ENABLE_BITTORRENT bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX)); #endif // ENABLE_BITTORRENT - if(op->get(PREF_LOG) == "-") { - LogFactory::setLogFile(DEV_STDOUT); - } else if(op->blank(PREF_LOG)) { - LogFactory::setLogFile(DEV_NULL); - } else { - LogFactory::setLogFile(op->get(PREF_LOG)); - } + LogFactory::setLogFile(op->get(PREF_LOG)); LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL)); if(op->getAsBool(PREF_QUIET)) { LogFactory::setConsoleOutput(false); |
