mirror of https://github.com/aria2/aria2
Rewritten Logger using BufferedFile.
parent
b6b8cb005d
commit
fd5f9127cd
|
@ -36,13 +36,14 @@
|
||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include "DlAbortEx.h"
|
#include "DlAbortEx.h"
|
||||||
#include "fmt.h"
|
#include "fmt.h"
|
||||||
#include "message.h"
|
#include "message.h"
|
||||||
#include "A2STR.h"
|
#include "A2STR.h"
|
||||||
#include "a2time.h"
|
#include "a2time.h"
|
||||||
|
#include "BufferedFile.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -60,23 +61,31 @@ static const std::string ERROR_LABEL("ERROR");
|
||||||
|
|
||||||
Logger::Logger()
|
Logger::Logger()
|
||||||
: logLevel_(Logger::A2_DEBUG),
|
: logLevel_(Logger::A2_DEBUG),
|
||||||
|
fpp_(0),
|
||||||
|
stdoutfpp_(new BufferedFile(stdout)),
|
||||||
stdoutField_(0)
|
stdoutField_(0)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
Logger::~Logger() {}
|
Logger::~Logger()
|
||||||
|
{
|
||||||
|
delete fpp_;
|
||||||
|
delete stdoutfpp_;
|
||||||
|
}
|
||||||
|
|
||||||
void Logger::openFile(const std::string& filename)
|
void Logger::openFile(const std::string& filename)
|
||||||
{
|
{
|
||||||
file_.open(filename.c_str(), std::ios::app|std::ios::binary);
|
closeFile();
|
||||||
if(!file_) {
|
fpp_ = new BufferedFile(filename, BufferedFile::APPEND);
|
||||||
throw DL_ABORT_EX(fmt(EX_FILE_OPEN, filename.c_str(), "n/a"));
|
if(!fpp_) {
|
||||||
|
throw DL_ABORT_EX(fmt(EX_FILE_OPEN, utf8ToNative(filename).c_str(), "n/a"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Logger::closeFile()
|
void Logger::closeFile()
|
||||||
{
|
{
|
||||||
if(file_.is_open()) {
|
if(fpp_) {
|
||||||
file_.close();
|
fpp_->close();
|
||||||
|
fpp_ = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,7 +100,7 @@ void Logger::setStdoutLogLevel(Logger::LEVEL level, bool enabled)
|
||||||
|
|
||||||
bool Logger::levelEnabled(LEVEL level)
|
bool Logger::levelEnabled(LEVEL level)
|
||||||
{
|
{
|
||||||
return (level >= logLevel_ && file_.is_open()) || stdoutField_&level;
|
return (level >= logLevel_ && fpp_) || stdoutField_&level;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -116,11 +125,11 @@ const std::string& levelToString(Logger::LEVEL level)
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
void writeHeader
|
void writeHeader
|
||||||
(std::ostream& o, Logger::LEVEL level, const char* sourceFile, int lineNum)
|
(BufferedFile& fp, Logger::LEVEL level, const char* sourceFile, int lineNum)
|
||||||
{
|
{
|
||||||
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[20]; // 'YYYY-MM-DD hh:mm:ss'+'\0' = 20 bytes
|
||||||
struct tm tm;
|
struct tm tm;
|
||||||
//tv.tv_sec may not be of type time_t.
|
//tv.tv_sec may not be of type time_t.
|
||||||
time_t timesec = tv.tv_sec;
|
time_t timesec = tv.tv_sec;
|
||||||
|
@ -128,27 +137,23 @@ void writeHeader
|
||||||
size_t dateLength =
|
size_t dateLength =
|
||||||
strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
|
strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
|
||||||
assert(dateLength <= (size_t)20);
|
assert(dateLength <= (size_t)20);
|
||||||
snprintf(datestr+dateLength,
|
fp.printf("%s.%06ld %s - ", datestr, tv.tv_usec,
|
||||||
sizeof(datestr)-dateLength,
|
levelToString(level).c_str());
|
||||||
".%06ld", tv.tv_usec);
|
|
||||||
o << datestr << " " << levelToString(level) << " - ";
|
|
||||||
if(sourceFile) {
|
if(sourceFile) {
|
||||||
o << "[" << sourceFile << ":" << lineNum << "] ";
|
fp.printf("[%s:%d]", sourceFile, lineNum);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
void writeStackTrace(std::ostream& o, const std::string& stackTrace)
|
void writeStackTrace(BufferedFile& fp, const std::string& stackTrace)
|
||||||
{
|
{
|
||||||
o << stackTrace;
|
fp.write(stackTrace.data(), stackTrace.size());
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace {
|
void Logger::writeLog
|
||||||
void writeLog
|
(Logger::LEVEL level,
|
||||||
(std::ostream& o,
|
|
||||||
Logger::LEVEL level,
|
|
||||||
const char* sourceFile,
|
const char* sourceFile,
|
||||||
int lineNum,
|
int lineNum,
|
||||||
const char* msg,
|
const char* msg,
|
||||||
|
@ -157,20 +162,19 @@ void writeLog
|
||||||
bool toConsole)
|
bool toConsole)
|
||||||
{
|
{
|
||||||
if(toStream) {
|
if(toStream) {
|
||||||
writeHeader(o, level, sourceFile, lineNum);
|
writeHeader(*fpp_, level, sourceFile, lineNum);
|
||||||
o << msg << "\n";
|
fpp_->printf("%s\n", msg);
|
||||||
writeStackTrace(o, trace);
|
writeStackTrace(*fpp_, trace);
|
||||||
o << std::flush;
|
fpp_->flush();
|
||||||
}
|
}
|
||||||
if(toConsole) {
|
if(toConsole) {
|
||||||
std::cout << "\n";
|
stdoutfpp_->write("\n", 1);
|
||||||
writeHeader(std::cout, level, 0, 0);
|
writeHeader(*stdoutfpp_, level, 0, 0);
|
||||||
std::cout << msg << "\n";
|
stdoutfpp_->printf("%s\n", msg);
|
||||||
writeStackTrace(std::cout, trace);
|
writeStackTrace(*stdoutfpp_, trace);
|
||||||
std::cout << std::flush;
|
stdoutfpp_->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace
|
|
||||||
|
|
||||||
void Logger::log
|
void Logger::log
|
||||||
(LEVEL level,
|
(LEVEL level,
|
||||||
|
@ -178,8 +182,8 @@ void Logger::log
|
||||||
int lineNum,
|
int lineNum,
|
||||||
const char* msg)
|
const char* msg)
|
||||||
{
|
{
|
||||||
writeLog(file_, level, sourceFile, lineNum, msg, A2STR::NIL,
|
writeLog(level, sourceFile, lineNum, msg, A2STR::NIL,
|
||||||
level >= logLevel_ && file_.is_open(),
|
level >= logLevel_ && fpp_,
|
||||||
stdoutField_&level);
|
stdoutField_&level);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,8 +203,8 @@ void Logger::log
|
||||||
const char* msg,
|
const char* msg,
|
||||||
const Exception& ex)
|
const Exception& ex)
|
||||||
{
|
{
|
||||||
writeLog(file_, level, sourceFile, lineNum, msg, ex.stackTrace(),
|
writeLog(level, sourceFile, lineNum, msg, ex.stackTrace(),
|
||||||
level >= logLevel_ && file_.is_open(),
|
level >= logLevel_ && fpp_,
|
||||||
stdoutField_&level);
|
stdoutField_&level);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
14
src/Logger.h
14
src/Logger.h
|
@ -38,11 +38,11 @@
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <fstream>
|
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
class Exception;
|
class Exception;
|
||||||
|
class BufferedFile;
|
||||||
|
|
||||||
class Logger {
|
class Logger {
|
||||||
public:
|
public:
|
||||||
|
@ -55,11 +55,21 @@ public:
|
||||||
};
|
};
|
||||||
private:
|
private:
|
||||||
LEVEL logLevel_;
|
LEVEL logLevel_;
|
||||||
std::ofstream file_;
|
BufferedFile* fpp_;
|
||||||
|
BufferedFile* stdoutfpp_;
|
||||||
int stdoutField_;
|
int stdoutField_;
|
||||||
// Don't allow copying
|
// Don't allow copying
|
||||||
Logger(const Logger&);
|
Logger(const Logger&);
|
||||||
Logger& operator=(const Logger&);
|
Logger& operator=(const Logger&);
|
||||||
|
|
||||||
|
void writeLog
|
||||||
|
(Logger::LEVEL level,
|
||||||
|
const char* sourceFile,
|
||||||
|
int lineNum,
|
||||||
|
const char* msg,
|
||||||
|
const std::string& trace,
|
||||||
|
bool toStream,
|
||||||
|
bool toConsole);
|
||||||
public:
|
public:
|
||||||
Logger();
|
Logger();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue