2008-05-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Added --log-level option to specify log level. Log messages under
	the specified level are not output. Default value is `debug'.
	* src/HelpItemFactory.cc
	* src/LogFactory.cc
	* src/LogFactory.h
	* src/Logger.h
	* src/OptionHandlerFactory.cc
	* src/SimpleLogger.cc
	* src/SimpleLogger.h
	* src/main.cc
	* src/option_processing.cc
	* src/prefs.h
	* src/usage_text.h
pull/1/head
Tatsuhiro Tsujikawa 2008-05-15 15:48:41 +00:00
parent 2e8e926c10
commit d76e423884
12 changed files with 116 additions and 18 deletions

View File

@ -1,3 +1,19 @@
2008-05-16 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added --log-level option to specify log level. Log messages under
the specified level are not output. Default value is `debug'.
* src/HelpItemFactory.cc
* src/LogFactory.cc
* src/LogFactory.h
* src/Logger.h
* src/OptionHandlerFactory.cc
* src/SimpleLogger.cc
* src/SimpleLogger.h
* src/main.cc
* src/option_processing.cc
* src/prefs.h
* src/usage_text.h
2008-05-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> 2008-05-15 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Print download progress summary of all parallel downloads in specified Print download progress summary of all parallel downloads in specified

View File

@ -477,6 +477,15 @@ TagContainerHandle HelpItemFactory::createHelpItems(const Option* op)
item->addTag(TAG_ADVANCED); item->addTag(TAG_ADVANCED);
tc->addItem(item); tc->addItem(item);
} }
{
HelpItemHandle item(new HelpItem(PREF_LOG_LEVEL,
TEXT_LOG_LEVEL,
op->get(PREF_LOG_LEVEL)));
item->addTag(TAG_ADVANCED);
item->setAvailableValues
(StringFormat("%s,%s,%s,%s,%s", V_DEBUG, V_INFO, V_NOTICE, V_WARN, V_ERROR).str());
tc->addItem(item);
}
{ {
HelpItemHandle item(new HelpItem("help", TEXT_HELP, TAG_BASIC)); HelpItemHandle item(new HelpItem("help", TEXT_HELP, TAG_BASIC));
item->setAvailableValues item->setAvailableValues

View File

@ -35,17 +35,21 @@
#include "LogFactory.h" #include "LogFactory.h"
#include "SimpleLogger.h" #include "SimpleLogger.h"
#include "a2io.h" #include "a2io.h"
#include "prefs.h"
#include <cstring>
namespace aria2 { namespace aria2 {
std::string LogFactory::filename = DEV_NULL; std::string LogFactory::filename = DEV_NULL;
Logger* LogFactory::logger = 0; Logger* LogFactory::logger = 0;
bool LogFactory::_consoleOutput = true; bool LogFactory::_consoleOutput = true;
Logger::LEVEL LogFactory::_logLevel = Logger::DEBUG;
Logger* LogFactory::getInstance() { Logger* LogFactory::getInstance() {
if(logger == NULL) { if(logger == NULL) {
SimpleLogger* slogger = new SimpleLogger(); SimpleLogger* slogger = new SimpleLogger();
slogger->openFile(filename); slogger->openFile(filename);
slogger->setLogLevel(_logLevel);
if(_consoleOutput) { if(_consoleOutput) {
slogger->setStdout(Logger::NOTICE, true); slogger->setStdout(Logger::NOTICE, true);
slogger->setStdout(Logger::WARN, true); slogger->setStdout(Logger::WARN, true);
@ -56,6 +60,26 @@ Logger* LogFactory::getInstance() {
return logger; return logger;
} }
void LogFactory::setLogLevel(Logger::LEVEL level)
{
_logLevel = level;
}
void LogFactory::setLogLevel(const std::string& level)
{
if(strcmp(level.c_str(), V_DEBUG) == 0) {
_logLevel = Logger::DEBUG;
} else if(strcmp(level.c_str(), V_INFO) == 0) {
_logLevel = Logger::INFO;
} else if(strcmp(level.c_str(), V_NOTICE) == 0) {
_logLevel = Logger::NOTICE;
} else if(strcmp(level.c_str(), V_WARN) == 0) {
_logLevel = Logger::WARN;
} else if(strcmp(level.c_str(), V_ERROR) == 0) {
_logLevel = Logger::ERROR;
}
}
void LogFactory::release() { void LogFactory::release() {
delete logger; delete logger;
logger = 0; logger = 0;

View File

@ -36,17 +36,17 @@
#define _D_LOG_FACTORY_H_ #define _D_LOG_FACTORY_H_
#include "common.h" #include "common.h"
#include "Logger.h"
#include <string> #include <string>
namespace aria2 { namespace aria2 {
class Logger;
class LogFactory { class LogFactory {
private: private:
static std::string filename; static std::string filename;
static Logger* logger; static Logger* logger;
static bool _consoleOutput; static bool _consoleOutput;
static Logger::LEVEL _logLevel;
public: public:
/** /**
* Get logger instance. Returned logger is singleton. * Get logger instance. Returned logger is singleton.
@ -69,6 +69,18 @@ public:
_consoleOutput = f; _consoleOutput = f;
} }
/**
* Set log level to output.
*/
static void setLogLevel(Logger::LEVEL level);
/**
* Set log level to output by string represention of log level.
* Possible values are: debug, info, notice, warn, error
*/
static void setLogLevel(const std::string& level);
/** /**
* Releases used resources * Releases used resources
*/ */

View File

@ -62,6 +62,8 @@ public:
WARN = 1 << 3, WARN = 1 << 3,
ERROR = 1 << 4, ERROR = 1 << 4,
}; };
virtual void setLogLevel(LEVEL level) = 0;
}; };
} // namespace aria2 } // namespace aria2

View File

@ -133,6 +133,13 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers()
#endif // ENABLE_ASYNC_DNS #endif // ENABLE_ASYNC_DNS
handlers.push_back(SH(new BooleanOptionHandler(PREF_FTP_REUSE_CONNECTION))); handlers.push_back(SH(new BooleanOptionHandler(PREF_FTP_REUSE_CONNECTION)));
handlers.push_back(SH(new NumberOptionHandler(PREF_SUMMARY_INTERVAL, 0, INT32_MAX))); handlers.push_back(SH(new NumberOptionHandler(PREF_SUMMARY_INTERVAL, 0, INT32_MAX)));
{
const char* params[] = { V_DEBUG, V_INFO, V_NOTICE, V_WARN, V_ERROR };
handlers.push_back(SH(new ParameterOptionHandler
(PREF_LOG_LEVEL,
std::deque<std::string>(&params[0],
&params[arrayLength(params)]))));
}
return handlers; return handlers;
} }

View File

@ -56,19 +56,23 @@ namespace aria2 {
#endif #endif
#define WRITE_LOG(LEVEL, MSG) \ #define WRITE_LOG(LEVEL, MSG) \
if(LEVEL >= _logLevel){\
va_list ap;\ va_list ap;\
va_start(ap, MSG);\ va_start(ap, MSG);\
writeFile(Logger::LEVEL, MSG, ap);\ writeFile(LEVEL, MSG, ap);\
flush();\ flush();\
va_end(ap); va_end(ap);\
}
#define WRITE_LOG_EX(LEVEL, MSG, EX) \ #define WRITE_LOG_EX(LEVEL, MSG, EX) \
if(LEVEL >= _logLevel) {\
va_list ap;\ va_list ap;\
va_start(ap, EX);\ va_start(ap, EX);\
writeFile(Logger::LEVEL, MSG, ap);\ writeFile(LEVEL, MSG, ap);\
writeStackTrace(Logger::LEVEL, EX);\ writeStackTrace(LEVEL, EX);\
flush();\ flush();\
va_end(ap); va_end(ap);\
}
const std::string SimpleLogger::DEBUG("DEBUG"); const std::string SimpleLogger::DEBUG("DEBUG");
@ -80,7 +84,7 @@ const std::string SimpleLogger::ERROR("ERROR");
const std::string SimpleLogger::INFO("INFO"); const std::string SimpleLogger::INFO("INFO");
SimpleLogger::SimpleLogger():stdoutField(0) {} SimpleLogger::SimpleLogger():stdoutField(0), _logLevel(Logger::DEBUG) {}
SimpleLogger::~SimpleLogger() { SimpleLogger::~SimpleLogger() {
closeFile(); closeFile();
@ -184,43 +188,48 @@ void SimpleLogger::flush()
} }
void SimpleLogger::debug(const char* msg, ...) { void SimpleLogger::debug(const char* msg, ...) {
WRITE_LOG(DEBUG, msg); WRITE_LOG(Logger::DEBUG, msg);
} }
void SimpleLogger::debug(const char* msg, const Exception& e, ...) { void SimpleLogger::debug(const char* msg, const Exception& e, ...) {
WRITE_LOG_EX(DEBUG, msg, e); WRITE_LOG_EX(Logger::DEBUG, msg, e);
} }
void SimpleLogger::info(const char* msg, ...) { void SimpleLogger::info(const char* msg, ...) {
WRITE_LOG(INFO, msg); WRITE_LOG(Logger::INFO, msg);
} }
void SimpleLogger::info(const char* msg, const Exception& e, ...) { void SimpleLogger::info(const char* msg, const Exception& e, ...) {
WRITE_LOG_EX(INFO, msg, e); WRITE_LOG_EX(Logger::INFO, msg, e);
} }
void SimpleLogger::notice(const char* msg, ...) { void SimpleLogger::notice(const char* msg, ...) {
WRITE_LOG(NOTICE, msg); WRITE_LOG(Logger::NOTICE, msg);
} }
void SimpleLogger::notice(const char* msg, const Exception& e, ...) { void SimpleLogger::notice(const char* msg, const Exception& e, ...) {
WRITE_LOG_EX(INFO, msg, e); WRITE_LOG_EX(Logger::INFO, msg, e);
} }
void SimpleLogger::warn(const char* msg, ...) { void SimpleLogger::warn(const char* msg, ...) {
WRITE_LOG(WARN, msg); WRITE_LOG(Logger::WARN, msg);
} }
void SimpleLogger::warn(const char* msg, const Exception& e, ...) { void SimpleLogger::warn(const char* msg, const Exception& e, ...) {
WRITE_LOG_EX(WARN, msg, e); WRITE_LOG_EX(Logger::WARN, msg, e);
} }
void SimpleLogger::error(const char* msg, ...) { void SimpleLogger::error(const char* msg, ...) {
WRITE_LOG(ERROR, msg); WRITE_LOG(Logger::ERROR, msg);
} }
void SimpleLogger::error(const char* msg, const Exception& e, ...) { void SimpleLogger::error(const char* msg, const Exception& e, ...) {
WRITE_LOG_EX(ERROR, msg, e); WRITE_LOG_EX(Logger::ERROR, msg, e);
}
void SimpleLogger::setLogLevel(Logger::LEVEL level)
{
_logLevel = level;
} }
} // namespace aria2 } // namespace aria2

View File

@ -60,6 +60,8 @@ private:
std::ofstream file; std::ofstream file;
int stdoutField; int stdoutField;
Logger::LEVEL _logLevel;
static const std::string DEBUG; static const std::string DEBUG;
static const std::string NOTICE; static const std::string NOTICE;
@ -86,6 +88,8 @@ public:
virtual void error(const char* msg, ...); virtual void error(const char* msg, ...);
virtual void error(const char* msg, const Exception& ex, ...); virtual void error(const char* msg, const Exception& ex, ...);
virtual void setLogLevel(Logger::LEVEL level);
void setStdout(Logger::LEVEL level, bool enabled); void setStdout(Logger::LEVEL level, bool enabled);
}; };

View File

@ -339,6 +339,7 @@ int main(int argc, char* argv[])
} else { } else {
LogFactory::setLogFile(DEV_NULL); LogFactory::setLogFile(DEV_NULL);
} }
LogFactory::setLogLevel(op->get(PREF_LOG_LEVEL));
if(op->getAsBool(PREF_QUIET)) { if(op->getAsBool(PREF_QUIET)) {
LogFactory::setConsoleOutput(false); LogFactory::setConsoleOutput(false);
} }

View File

@ -152,6 +152,7 @@ Option* createDefaultOption()
#endif // ENABLE_ASYNC_DNS #endif // ENABLE_ASYNC_DNS
op->put(PREF_FTP_REUSE_CONNECTION, V_TRUE); op->put(PREF_FTP_REUSE_CONNECTION, V_TRUE);
op->put(PREF_SUMMARY_INTERVAL, "60"); op->put(PREF_SUMMARY_INTERVAL, "60");
op->put(PREF_LOG_LEVEL, V_DEBUG);
return op; return op;
} }
@ -228,6 +229,7 @@ Option* option_processing(int argc, char* const argv[])
#endif // ENABLE_ASYNC_DNS #endif // ENABLE_ASYNC_DNS
{ PREF_FTP_REUSE_CONNECTION, optional_argument, &lopt, 217 }, { PREF_FTP_REUSE_CONNECTION, optional_argument, &lopt, 217 },
{ PREF_SUMMARY_INTERVAL, required_argument, &lopt, 218 }, { PREF_SUMMARY_INTERVAL, required_argument, &lopt, 218 },
{ PREF_LOG_LEVEL, required_argument, &lopt, 219 },
#if defined ENABLE_BITTORRENT || ENABLE_METALINK #if defined ENABLE_BITTORRENT || ENABLE_METALINK
{ PREF_SHOW_FILES, no_argument, NULL, 'S' }, { PREF_SHOW_FILES, no_argument, NULL, 'S' },
{ PREF_SELECT_FILE, required_argument, &lopt, 21 }, { PREF_SELECT_FILE, required_argument, &lopt, 21 },
@ -440,6 +442,9 @@ Option* option_processing(int argc, char* const argv[])
case 218: case 218:
cmdstream << PREF_SUMMARY_INTERVAL << "=" << optarg << "\n"; cmdstream << PREF_SUMMARY_INTERVAL << "=" << optarg << "\n";
break; break;
case 219:
cmdstream << PREF_LOG_LEVEL << "=" << optarg << "\n";
break;
} }
break; break;
} }

View File

@ -132,6 +132,13 @@
#define PREF_ASYNC_DNS "async-dns" #define PREF_ASYNC_DNS "async-dns"
// value: 1*digit // value: 1*digit
#define PREF_SUMMARY_INTERVAL "summary-interval" #define PREF_SUMMARY_INTERVAL "summary-interval"
// value: debug, info, notice, warn, error
#define PREF_LOG_LEVEL "log-level"
# define V_DEBUG "debug"
# define V_INFO "info"
# define V_NOTICE "notice"
# define V_WARN "warn"
# define V_ERROR "error"
/** /**
* FTP related preferences * FTP related preferences

View File

@ -341,3 +341,5 @@ _(" --ftp-reuse-connection[=true|false] Reuse connection in FTP.")
#define TEXT_SUMMARY_INTERVAL \ #define TEXT_SUMMARY_INTERVAL \
_(" --summary-interval=SEC Set interval to output download progress summary.\n"\ _(" --summary-interval=SEC Set interval to output download progress summary.\n"\
" Setting 0 suppresses the output.") " Setting 0 suppresses the output.")
#define TEXT_LOG_LEVEL \
_(" --log-level=LEVEL Set log level to output.")