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
pull/1/head
Tatsuhiro Tsujikawa 2010-06-23 14:15:35 +00:00
parent 4736e77acf
commit 66660d10c2
16 changed files with 239 additions and 60 deletions

View File

@ -1,3 +1,24 @@
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
2010-06-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Request ut_metadata in end-game mode(in other words, more

View File

@ -2,12 +2,12 @@
.\" Title: aria2c
.\" Author: [FIXME: author] [see http://docbook.sf.net/el/author]
.\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
.\" Date: 06/02/2010
.\" Date: 06/23/2010
.\" Manual: Aria2 Manual
.\" Source: Aria2 1.9.4
.\" Language: English
.\"
.TH "ARIA2C" "1" "06/02/2010" "Aria2 1\&.9\&.4" "Aria2 Manual"
.TH "ARIA2C" "1" "06/23/2010" "Aria2 1\&.9\&.4" "Aria2 Manual"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
@ -60,7 +60,7 @@ subsection for details\&.
.RS 4
The file name of the log file\&. If
\fI\-\fR
is specified, log is written to stdout\&.
is specified, log is written to stdout\&. If empty string("") is specified, log is not written to file\&.
.RE
.PP
\fB\-j\fR, \fB\-\-max\-concurrent\-downloads\fR=N
@ -2980,7 +2980,7 @@ This method returns global options\&. The response is of type struct\&. Its key
.sp
\fBaria2\&.changeGlobalOption\fR \fIoptions\fR
.sp
This method changes global options dynamically\&. \fIoptions\fR is of type struct and the available options are \fBmax\-concurrent\-downloads\fR, \fBmax\-overall\-download\-limit\fR and \fBmax\-overall\-upload\-limit\fR\&. This method returns "OK" for success\&.
This method changes global options dynamically\&. \fIoptions\fR is of type struct and the available options are \fBmax\-concurrent\-downloads\fR, \fBmax\-overall\-download\-limit\fR, \fBmax\-overall\-upload\-limit\fR, \fBlog\-level\fR and \fBlog\fR\&. Using \fBlog\fR option, you can dynamically start logging or change log file\&. To stop logging, give empty string("") as a parameter value\&. Note that log file is always opened in append mode\&. This method returns "OK" for success\&.
.sp
\fBaria2\&.purgeDownloadResult\fR
.sp

View File

@ -626,7 +626,7 @@ downloading a file like BitTorrent.</p></div>
<dd>
<p>
The file name of the log file. If <em>-</em> is specified, log is written to
stdout.
stdout. If empty string("") is specified, log is not written to file.
</p>
</dd>
<dt class="hdlist1">
@ -3657,8 +3657,11 @@ added download, the response contains keys returned by
<div class="paragraph"><p><strong>aria2.changeGlobalOption</strong> <em>options</em></p></div>
<div class="paragraph"><p>This method changes global options dynamically. <em>options</em> is of type
struct and the available options are <strong>max-concurrent-downloads</strong>,
<strong>max-overall-download-limit</strong> and <strong>max-overall-upload-limit</strong>. This
method returns "OK" for success.</p></div>
<strong>max-overall-download-limit</strong>, <strong>max-overall-upload-limit</strong>, <strong>log-level</strong>
and <strong>log</strong>. Using <strong>log</strong> option, you can dynamically start logging or
change log file. To stop logging, give empty string("") as a parameter
value. Note that log file is always opened in append mode. This method
returns "OK" for success.</p></div>
<div class="paragraph"><p><strong>aria2.purgeDownloadResult</strong></p></div>
<div class="paragraph"><p>This method purges completed/error/removed downloads to free memory.
This method returns "OK".</p></div>
@ -4199,7 +4202,7 @@ files in the program, then also delete it here.</p></div>
<div id="footnotes"><hr /></div>
<div id="footer">
<div id="footer-text">
Last updated 2010-06-02 21:12:07 JST
Last updated 2010-06-23 23:10:50 JST
</div>
</div>
</body>

View File

@ -43,7 +43,7 @@ Basic Options
*-l*, *--log*=LOG::
The file name of the log file. If '-' is specified, log is written to
stdout.
stdout. If empty string("") is specified, log is not written to file.
*-j*, *--max-concurrent-downloads*=N::
Set maximum number of parallel downloads for every static (HTTP/FTP) URI,
@ -1766,8 +1766,11 @@ added download, the response contains keys returned by
This method changes global options dynamically. 'options' is of type
struct and the available options are *max-concurrent-downloads*,
*max-overall-download-limit* and *max-overall-upload-limit*. This
method returns "OK" for success.
*max-overall-download-limit*, *max-overall-upload-limit*, *log-level*
and *log*. Using *log* option, you can dynamically start logging or
change log file. To stop logging, give empty string("") as a parameter
value. Note that log file is always opened in append mode. This method
returns "OK" for success.
*aria2.purgeDownloadResult*

View File

@ -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;

View File

@ -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

65
src/LogFormatter.h Normal file
View File

@ -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

View File

@ -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

View File

@ -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
(std::ostream& o, LEVEL logLevel, const std::string& logLevelLabel,
const char* msg, va_list ap) = 0;
virtual void writeStackTrace
void writeLog
(std::ostream& o, LEVEL logLevel, const std::string& logLevelLabel,
const Exception& ex) = 0;
const char* msg, va_list ap);
void writeStackTrace
(std::ostream& o, LEVEL logLevel, const std::string& logLevelLabel,
const Exception& ex);
public:
Logger();
@ -113,6 +116,8 @@ public:
void closeFile();
void setLogFormatter(LogFormatter* logFormatter);
void setLogLevel(LEVEL level)
{
logLevel_ = level;

View File

@ -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\

View File

@ -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@

View File

@ -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)
{

View File

@ -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

View File

@ -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;

View File

@ -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;
}

View File

@ -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);