mirror of https://github.com/aria2/aria2
2009-07-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
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.hpull/1/head
parent
e6422f82eb
commit
21170e804d
12
ChangeLog
12
ChangeLog
|
@ -1,3 +1,15 @@
|
||||||
|
2009-07-18 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
|
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 <t-tujikawa@users.sourceforge.net>
|
2009-07-17 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
|
||||||
|
|
||||||
Updated doc
|
Updated doc
|
||||||
|
|
|
@ -36,7 +36,6 @@
|
||||||
#include "SimpleLogger.h"
|
#include "SimpleLogger.h"
|
||||||
#include "a2io.h"
|
#include "a2io.h"
|
||||||
#include "prefs.h"
|
#include "prefs.h"
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -46,7 +45,7 @@ bool LogFactory::_consoleOutput = true;
|
||||||
Logger::LEVEL LogFactory::_logLevel = Logger::DEBUG;
|
Logger::LEVEL LogFactory::_logLevel = Logger::DEBUG;
|
||||||
|
|
||||||
Logger* LogFactory::getInstance() {
|
Logger* LogFactory::getInstance() {
|
||||||
if(logger == NULL) {
|
if(!logger) {
|
||||||
SimpleLogger* slogger = new SimpleLogger();
|
SimpleLogger* slogger = new SimpleLogger();
|
||||||
if(filename != DEV_NULL) {
|
if(filename != DEV_NULL) {
|
||||||
// don't open file DEV_NULL for performance sake.
|
// don't open file DEV_NULL for performance sake.
|
||||||
|
@ -55,9 +54,9 @@ Logger* LogFactory::getInstance() {
|
||||||
}
|
}
|
||||||
slogger->setLogLevel(_logLevel);
|
slogger->setLogLevel(_logLevel);
|
||||||
if(_consoleOutput) {
|
if(_consoleOutput) {
|
||||||
slogger->setStdout(Logger::NOTICE, true);
|
slogger->setStdoutLogLevel(Logger::NOTICE, true);
|
||||||
slogger->setStdout(Logger::WARN, true);
|
slogger->setStdoutLogLevel(Logger::WARN, true);
|
||||||
slogger->setStdout(Logger::ERROR, true);
|
slogger->setStdoutLogLevel(Logger::ERROR, true);
|
||||||
}
|
}
|
||||||
logger = slogger;
|
logger = slogger;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,9 +36,11 @@
|
||||||
#define _D_LOG_FACTORY_H_
|
#define _D_LOG_FACTORY_H_
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "Logger.h"
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include "Logger.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
class LogFactory {
|
class LogFactory {
|
||||||
|
|
149
src/Logger.h
149
src/Logger.h
|
@ -37,24 +37,17 @@
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
|
#include <cstdarg>
|
||||||
|
#include <string>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
class Exception;
|
class Exception;
|
||||||
|
|
||||||
class Logger {
|
class Logger {
|
||||||
public:
|
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 {
|
enum LEVEL {
|
||||||
DEBUG = 1 << 0,
|
DEBUG = 1 << 0,
|
||||||
INFO = 1 << 1,
|
INFO = 1 << 1,
|
||||||
|
@ -63,7 +56,137 @@ public:
|
||||||
ERROR = 1 << 4,
|
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
|
} // namespace aria2
|
||||||
|
|
|
@ -40,7 +40,7 @@ SRCS = Socket.h\
|
||||||
DlAbortEx.h\
|
DlAbortEx.h\
|
||||||
DlRetryEx.h\
|
DlRetryEx.h\
|
||||||
DownloadFailureException.h\
|
DownloadFailureException.h\
|
||||||
Logger.h\
|
Logger.cc Logger.h\
|
||||||
SimpleLogger.cc SimpleLogger.h\
|
SimpleLogger.cc SimpleLogger.h\
|
||||||
DiskWriter.h\
|
DiskWriter.h\
|
||||||
DiskWriterFactory.h\
|
DiskWriterFactory.h\
|
||||||
|
@ -51,7 +51,6 @@ SRCS = Socket.h\
|
||||||
Option.cc Option.h\
|
Option.cc Option.h\
|
||||||
Base64.cc Base64.h\
|
Base64.cc Base64.h\
|
||||||
LogFactory.cc LogFactory.h\
|
LogFactory.cc LogFactory.h\
|
||||||
NullLogger.h\
|
|
||||||
TimeA2.cc TimeA2.h\
|
TimeA2.cc TimeA2.h\
|
||||||
SharedHandle.h\
|
SharedHandle.h\
|
||||||
HandleRegistry.h\
|
HandleRegistry.h\
|
||||||
|
|
|
@ -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 \
|
SegmentMan.h Util.cc Util.h Request.cc Request.h common.h \
|
||||||
message.h Exception.cc Exception.h FatalException.h \
|
message.h Exception.cc Exception.h FatalException.h \
|
||||||
RecoverableException.h DlAbortEx.h DlRetryEx.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 \
|
SimpleLogger.h DiskWriter.h DiskWriterFactory.h \
|
||||||
AbstractDiskWriter.cc AbstractDiskWriter.h \
|
AbstractDiskWriter.cc AbstractDiskWriter.h \
|
||||||
DefaultDiskWriter.cc DefaultDiskWriter.h \
|
DefaultDiskWriter.cc DefaultDiskWriter.h \
|
||||||
DefaultDiskWriterFactory.cc DefaultDiskWriterFactory.h File.cc \
|
DefaultDiskWriterFactory.cc DefaultDiskWriterFactory.h File.cc \
|
||||||
File.h Option.cc Option.h Base64.cc Base64.h LogFactory.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 \
|
HandleRegistry.h FeatureConfig.cc FeatureConfig.h \
|
||||||
DownloadEngineFactory.cc DownloadEngineFactory.h SpeedCalc.cc \
|
DownloadEngineFactory.cc DownloadEngineFactory.h SpeedCalc.cc \
|
||||||
SpeedCalc.h PeerStat.h BitfieldMan.cc BitfieldMan.h \
|
SpeedCalc.h PeerStat.h BitfieldMan.cc BitfieldMan.h \
|
||||||
|
@ -787,11 +787,11 @@ am__objects_26 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
|
||||||
FtpTunnelResponseCommand.$(OBJEXT) SleepCommand.$(OBJEXT) \
|
FtpTunnelResponseCommand.$(OBJEXT) SleepCommand.$(OBJEXT) \
|
||||||
DownloadEngine.$(OBJEXT) GrowSegment.$(OBJEXT) \
|
DownloadEngine.$(OBJEXT) GrowSegment.$(OBJEXT) \
|
||||||
PiecedSegment.$(OBJEXT) SegmentMan.$(OBJEXT) Util.$(OBJEXT) \
|
PiecedSegment.$(OBJEXT) SegmentMan.$(OBJEXT) Util.$(OBJEXT) \
|
||||||
Request.$(OBJEXT) Exception.$(OBJEXT) SimpleLogger.$(OBJEXT) \
|
Request.$(OBJEXT) Exception.$(OBJEXT) Logger.$(OBJEXT) \
|
||||||
AbstractDiskWriter.$(OBJEXT) DefaultDiskWriter.$(OBJEXT) \
|
SimpleLogger.$(OBJEXT) AbstractDiskWriter.$(OBJEXT) \
|
||||||
DefaultDiskWriterFactory.$(OBJEXT) File.$(OBJEXT) \
|
DefaultDiskWriter.$(OBJEXT) DefaultDiskWriterFactory.$(OBJEXT) \
|
||||||
Option.$(OBJEXT) Base64.$(OBJEXT) LogFactory.$(OBJEXT) \
|
File.$(OBJEXT) Option.$(OBJEXT) Base64.$(OBJEXT) \
|
||||||
TimeA2.$(OBJEXT) FeatureConfig.$(OBJEXT) \
|
LogFactory.$(OBJEXT) TimeA2.$(OBJEXT) FeatureConfig.$(OBJEXT) \
|
||||||
DownloadEngineFactory.$(OBJEXT) SpeedCalc.$(OBJEXT) \
|
DownloadEngineFactory.$(OBJEXT) SpeedCalc.$(OBJEXT) \
|
||||||
BitfieldMan.$(OBJEXT) BitfieldManFactory.$(OBJEXT) \
|
BitfieldMan.$(OBJEXT) BitfieldManFactory.$(OBJEXT) \
|
||||||
SimpleRandomizer.$(OBJEXT) HttpResponse.$(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 \
|
SegmentMan.h Util.cc Util.h Request.cc Request.h common.h \
|
||||||
message.h Exception.cc Exception.h FatalException.h \
|
message.h Exception.cc Exception.h FatalException.h \
|
||||||
RecoverableException.h DlAbortEx.h DlRetryEx.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 \
|
SimpleLogger.h DiskWriter.h DiskWriterFactory.h \
|
||||||
AbstractDiskWriter.cc AbstractDiskWriter.h \
|
AbstractDiskWriter.cc AbstractDiskWriter.h \
|
||||||
DefaultDiskWriter.cc DefaultDiskWriter.h \
|
DefaultDiskWriter.cc DefaultDiskWriter.h \
|
||||||
DefaultDiskWriterFactory.cc DefaultDiskWriterFactory.h File.cc \
|
DefaultDiskWriterFactory.cc DefaultDiskWriterFactory.h File.cc \
|
||||||
File.h Option.cc Option.h Base64.cc Base64.h LogFactory.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 \
|
HandleRegistry.h FeatureConfig.cc FeatureConfig.h \
|
||||||
DownloadEngineFactory.cc DownloadEngineFactory.h SpeedCalc.cc \
|
DownloadEngineFactory.cc DownloadEngineFactory.h SpeedCalc.cc \
|
||||||
SpeedCalc.h PeerStat.h BitfieldMan.cc BitfieldMan.h \
|
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)/LibgnutlsTLSContext.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LibsslTLSContext.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)/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)/LongestSequencePieceSelector.Po@am__quote@
|
||||||
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MSEHandshake.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@
|
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/MemoryBufferPreDownloadHandler.Po@am__quote@
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
/* <!-- copyright */
|
|
||||||
/*
|
|
||||||
* aria2 - The high speed download utility
|
|
||||||
*
|
|
||||||
* Copyright (C) 2006 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_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_
|
|
|
@ -33,116 +33,27 @@
|
||||||
*/
|
*/
|
||||||
/* copyright --> */
|
/* copyright --> */
|
||||||
#include "SimpleLogger.h"
|
#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 <cerrno>
|
|
||||||
#include <cstring>
|
|
||||||
#include <iostream>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "Util.h"
|
||||||
|
#include "a2time.h"
|
||||||
|
#include "A2STR.h"
|
||||||
|
#include "StringFormat.h"
|
||||||
|
#include "Exception.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
#if !defined(va_copy)
|
static void writeHeader
|
||||||
# if defined(__va_copy)
|
(std::ostream& o, const std::string& date, const std::string& logLevelLabel)
|
||||||
# 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)
|
|
||||||
{
|
{
|
||||||
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,
|
void SimpleLogger::writeLog
|
||||||
const char* msg, va_list ap,
|
(std::ostream& o, Logger::LEVEL level, const std::string& logLevelLabel,
|
||||||
bool printHeader)
|
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;
|
struct timeval tv;
|
||||||
gettimeofday(&tv, 0);
|
gettimeofday(&tv, 0);
|
||||||
char datestr[27]; // 'YYYY-MM-DD hh:mm:ss.uuuuuu'+'\0' = 27 bytes
|
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);
|
assert(dateLength <= (size_t)20);
|
||||||
snprintf(datestr+dateLength, sizeof(datestr)-dateLength,
|
snprintf(datestr+dateLength, sizeof(datestr)-dateLength,
|
||||||
".%06ld", tv.tv_usec);
|
".%06ld", tv.tv_usec);
|
||||||
|
writeHeader(o, datestr, logLevelLabel);
|
||||||
// TODO a quick hack not to print header in console
|
|
||||||
if(printHeader) {
|
|
||||||
writeHeader(o, datestr, levelStr);
|
|
||||||
}
|
|
||||||
{
|
{
|
||||||
char buf[1024];
|
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";
|
o << "SimpleLogger error, failed to format message.\n";
|
||||||
} else {
|
} else {
|
||||||
o << buf;
|
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);
|
o << e.stackTrace();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -36,61 +36,18 @@
|
||||||
#define _D_SIMPLE_LOGGER_H_
|
#define _D_SIMPLE_LOGGER_H_
|
||||||
|
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
#include <cstdarg>
|
|
||||||
#include <string>
|
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
class SimpleLogger:public Logger {
|
class SimpleLogger:public Logger {
|
||||||
private:
|
protected:
|
||||||
void writeFile(Logger::LEVEL level, const char* msg, va_list ap);
|
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);
|
virtual void writeStackTrace
|
||||||
|
(std::ostream& out, Logger::LEVEL logLevel, const std::string& logLevelLabel,
|
||||||
void flush();
|
const Exception& e);
|
||||||
|
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
Loading…
Reference in New Issue