2009-07-18 08:32:57 +00:00
|
|
|
/* <!-- copyright */
|
|
|
|
/*
|
|
|
|
* aria2 - The high speed download utility
|
|
|
|
*
|
2010-11-20 08:21:36 +00:00
|
|
|
* Copyright (C) 2010 Tatsuhiro Tsujikawa
|
2009-07-18 08:32:57 +00:00
|
|
|
*
|
|
|
|
* 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
|
2010-01-05 16:01:46 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2009-07-18 08:32:57 +00:00
|
|
|
*
|
|
|
|
* 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 --> */
|
|
|
|
#include "Logger.h"
|
|
|
|
|
2012-12-08 12:48:18 +00:00
|
|
|
#include <unistd.h>
|
2009-07-18 08:32:57 +00:00
|
|
|
#include <cstring>
|
2011-05-07 09:48:58 +00:00
|
|
|
#include <cstdio>
|
2013-06-21 16:10:38 +00:00
|
|
|
#include <cassert>
|
2009-07-18 08:32:57 +00:00
|
|
|
|
|
|
|
#include "DlAbortEx.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "fmt.h"
|
2009-07-18 08:32:57 +00:00
|
|
|
#include "message.h"
|
2010-11-20 08:21:36 +00:00
|
|
|
#include "A2STR.h"
|
|
|
|
#include "a2time.h"
|
2011-08-07 08:06:07 +00:00
|
|
|
#include "BufferedFile.h"
|
|
|
|
#include "util.h"
|
2011-08-09 14:33:55 +00:00
|
|
|
#include "console.h"
|
2009-07-18 08:32:57 +00:00
|
|
|
|
|
|
|
namespace aria2 {
|
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
Logger::Logger()
|
2015-12-27 09:39:47 +00:00
|
|
|
: logLevel_(Logger::A2_DEBUG),
|
|
|
|
consoleLogLevel_(Logger::A2_NOTICE),
|
|
|
|
consoleOutput_(true),
|
|
|
|
colorOutput_(global::cout()->supportsColor())
|
2011-08-07 08:06:07 +00:00
|
|
|
{
|
|
|
|
}
|
2009-07-18 08:32:57 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
Logger::~Logger() {}
|
|
|
|
|
2009-07-18 08:32:57 +00:00
|
|
|
void Logger::openFile(const std::string& filename)
|
|
|
|
{
|
2011-08-07 08:06:07 +00:00
|
|
|
closeFile();
|
2015-12-27 09:39:47 +00:00
|
|
|
if (filename == DEV_STDOUT) {
|
2011-08-09 16:28:20 +00:00
|
|
|
fpp_ = global::cout();
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
fpp_ =
|
|
|
|
std::make_shared<BufferedFile>(filename.c_str(), BufferedFile::APPEND);
|
|
|
|
if (!*static_cast<BufferedFile*>(fpp_.get())) {
|
2011-08-09 14:33:55 +00:00
|
|
|
throw DL_ABORT_EX(fmt(EX_FILE_OPEN, filename.c_str(), "n/a"));
|
|
|
|
}
|
2009-07-18 08:32:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Logger::closeFile()
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (fpp_) {
|
2011-08-09 14:33:55 +00:00
|
|
|
fpp_.reset();
|
2009-07-18 08:32:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void Logger::setConsoleOutput(bool enabled) { consoleOutput_ = enabled; }
|
2013-01-05 09:48:09 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void Logger::setColorOutput(bool enabled) { colorOutput_ = enabled; }
|
2014-01-29 15:12:24 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
bool Logger::fileLogEnabled(LEVEL level) { return level >= logLevel_ && fpp_; }
|
2013-01-05 09:48:09 +00:00
|
|
|
|
|
|
|
bool Logger::consoleLogEnabled(LEVEL level)
|
|
|
|
{
|
|
|
|
return consoleOutput_ && level >= consoleLogLevel_;
|
2010-06-20 12:00:51 +00:00
|
|
|
}
|
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
bool Logger::levelEnabled(LEVEL level)
|
2010-06-20 12:00:51 +00:00
|
|
|
{
|
2013-01-05 09:48:09 +00:00
|
|
|
return fileLogEnabled(level) || consoleLogEnabled(level);
|
2010-06-20 12:00:51 +00:00
|
|
|
}
|
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
namespace {
|
2012-09-24 14:20:43 +00:00
|
|
|
const char* levelToString(Logger::LEVEL level)
|
2010-06-20 12:00:51 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
switch (level) {
|
2010-11-20 08:21:36 +00:00
|
|
|
case Logger::A2_DEBUG:
|
2012-09-24 14:20:43 +00:00
|
|
|
return "DEBUG";
|
2010-11-20 08:21:36 +00:00
|
|
|
case Logger::A2_INFO:
|
2012-09-24 14:20:43 +00:00
|
|
|
return "INFO";
|
2010-11-20 08:21:36 +00:00
|
|
|
case Logger::A2_NOTICE:
|
2012-09-24 14:20:43 +00:00
|
|
|
return "NOTICE";
|
2010-11-20 08:21:36 +00:00
|
|
|
case Logger::A2_WARN:
|
2012-09-24 14:20:43 +00:00
|
|
|
return "WARN";
|
2010-11-20 08:21:36 +00:00
|
|
|
case Logger::A2_ERROR:
|
2012-09-24 14:20:43 +00:00
|
|
|
return "ERROR";
|
2010-11-20 08:21:36 +00:00
|
|
|
default:
|
2012-09-24 14:20:43 +00:00
|
|
|
return "";
|
2010-11-20 08:21:36 +00:00
|
|
|
}
|
2010-06-20 12:00:51 +00:00
|
|
|
}
|
2010-11-20 08:21:36 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename Output>
|
|
|
|
void writeHeader(Output& fp, Logger::LEVEL level, const char* sourceFile,
|
|
|
|
int lineNum)
|
2010-11-20 08:21:36 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2013-08-20 16:57:17 +00:00
|
|
|
gettimeofday(&tv, nullptr);
|
2011-08-07 08:06:07 +00:00
|
|
|
char datestr[20]; // 'YYYY-MM-DD hh:mm:ss'+'\0' = 20 bytes
|
2010-11-20 08:21:36 +00:00
|
|
|
struct tm tm;
|
2015-12-27 09:39:47 +00:00
|
|
|
// tv.tv_sec may not be of type time_t.
|
2010-11-20 08:21:36 +00:00
|
|
|
time_t timesec = tv.tv_sec;
|
|
|
|
localtime_r(×ec, &tm);
|
|
|
|
size_t dateLength =
|
2015-12-27 09:39:47 +00:00
|
|
|
strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
|
2010-11-20 08:21:36 +00:00
|
|
|
assert(dateLength <= (size_t)20);
|
2012-12-08 10:13:59 +00:00
|
|
|
fp.printf("%s.%06ld [%s] [%s:%d] ", datestr, tv.tv_usec, levelToString(level),
|
|
|
|
sourceFile, lineNum);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-12-08 12:48:18 +00:00
|
|
|
namespace {
|
|
|
|
const char* levelColor(Logger::LEVEL level)
|
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
switch (level) {
|
2012-12-08 12:48:18 +00:00
|
|
|
case Logger::A2_DEBUG:
|
2013-01-05 09:48:09 +00:00
|
|
|
return "\033[1;37m";
|
2012-12-08 12:48:18 +00:00
|
|
|
case Logger::A2_INFO:
|
2013-01-05 09:48:09 +00:00
|
|
|
return "\033[1;36m";
|
2012-12-08 12:48:18 +00:00
|
|
|
case Logger::A2_NOTICE:
|
|
|
|
return "\033[1;32m";
|
|
|
|
case Logger::A2_WARN:
|
|
|
|
return "\033[1;33m";
|
|
|
|
case Logger::A2_ERROR:
|
|
|
|
return "\033[1;31m";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2012-12-08 10:13:59 +00:00
|
|
|
namespace {
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename Output>
|
2012-12-08 12:48:18 +00:00
|
|
|
void writeHeaderConsole(Output& fp, Logger::LEVEL level, bool useColor)
|
2012-12-08 10:13:59 +00:00
|
|
|
{
|
2013-03-03 09:35:07 +00:00
|
|
|
struct timeval tv;
|
2013-08-20 16:57:17 +00:00
|
|
|
gettimeofday(&tv, nullptr);
|
2013-03-03 09:35:07 +00:00
|
|
|
char datestr[15]; // 'MM/DD hh:mm:ss'+'\0' = 15 bytes
|
|
|
|
struct tm tm;
|
2015-12-27 09:39:47 +00:00
|
|
|
// tv.tv_sec may not be of type time_t.
|
2013-03-03 09:35:07 +00:00
|
|
|
time_t timesec = tv.tv_sec;
|
|
|
|
localtime_r(×ec, &tm);
|
2015-12-27 09:39:47 +00:00
|
|
|
size_t dateLength = strftime(datestr, sizeof(datestr), "%m/%d %H:%M:%S", &tm);
|
2013-03-03 09:35:07 +00:00
|
|
|
assert(dateLength <= (size_t)15);
|
2015-12-27 09:39:47 +00:00
|
|
|
if (useColor) {
|
2013-03-03 09:35:07 +00:00
|
|
|
fp.printf("%s [%s%s\033[0m] ", datestr, levelColor(level),
|
|
|
|
levelToString(level));
|
2015-12-27 09:39:47 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-03-03 09:35:07 +00:00
|
|
|
fp.printf("%s [%s] ", datestr, levelToString(level));
|
2012-12-08 12:48:18 +00:00
|
|
|
}
|
2010-06-20 12:00:51 +00:00
|
|
|
}
|
2010-11-20 08:21:36 +00:00
|
|
|
} // namespace
|
2010-06-20 12:00:51 +00:00
|
|
|
|
2010-11-20 08:21:36 +00:00
|
|
|
namespace {
|
2015-12-27 09:39:47 +00:00
|
|
|
template <typename Output>
|
2013-01-05 09:48:09 +00:00
|
|
|
void writeStackTrace(Output& fp, const char* stackTrace)
|
2010-06-23 14:15:35 +00:00
|
|
|
{
|
2013-01-05 09:48:09 +00:00
|
|
|
fp.write(stackTrace);
|
2010-06-23 14:15:35 +00:00
|
|
|
}
|
2010-11-20 08:21:36 +00:00
|
|
|
} // namespace
|
2010-06-23 14:15:35 +00:00
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void Logger::writeLog(Logger::LEVEL level, const char* sourceFile, int lineNum,
|
|
|
|
const char* msg, const char* trace)
|
2010-06-20 12:00:51 +00:00
|
|
|
{
|
2015-12-27 09:39:47 +00:00
|
|
|
if (fileLogEnabled(level)) {
|
2011-08-07 08:06:07 +00:00
|
|
|
writeHeader(*fpp_, level, sourceFile, lineNum);
|
|
|
|
fpp_->printf("%s\n", msg);
|
|
|
|
writeStackTrace(*fpp_, trace);
|
|
|
|
fpp_->flush();
|
2010-11-20 08:21:36 +00:00
|
|
|
}
|
2015-12-27 09:39:47 +00:00
|
|
|
if (consoleLogEnabled(level)) {
|
2011-08-09 16:28:20 +00:00
|
|
|
global::cout()->printf("\n");
|
2014-01-29 15:12:24 +00:00
|
|
|
writeHeaderConsole(*global::cout(), level, colorOutput_);
|
2011-08-09 16:28:20 +00:00
|
|
|
global::cout()->printf("%s\n", msg);
|
|
|
|
writeStackTrace(*global::cout(), trace);
|
|
|
|
global::cout()->flush();
|
2010-06-20 12:00:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void Logger::log(LEVEL level, const char* sourceFile, int lineNum,
|
|
|
|
const char* msg)
|
2010-06-23 14:15:35 +00:00
|
|
|
{
|
2013-01-05 09:48:09 +00:00
|
|
|
writeLog(level, sourceFile, lineNum, msg, "");
|
2010-06-23 14:15:35 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void Logger::log(LEVEL level, const char* sourceFile, int lineNum,
|
|
|
|
const std::string& msg)
|
2010-06-23 14:15:35 +00:00
|
|
|
{
|
2010-11-20 08:21:36 +00:00
|
|
|
log(level, sourceFile, lineNum, msg.c_str());
|
2010-06-23 14:15:35 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void Logger::log(LEVEL level, const char* sourceFile, int lineNum,
|
|
|
|
const char* msg, const Exception& ex)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2013-01-05 09:48:09 +00:00
|
|
|
writeLog(level, sourceFile, lineNum, msg, ex.stackTrace().c_str());
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
2015-12-27 09:39:47 +00:00
|
|
|
void Logger::log(LEVEL level, const char* sourceFile, int lineNum,
|
|
|
|
const std::string& msg, const Exception& ex)
|
2010-11-14 07:17:55 +00:00
|
|
|
{
|
2010-11-20 08:21:36 +00:00
|
|
|
log(level, sourceFile, lineNum, msg.c_str(), ex);
|
2010-11-14 07:17:55 +00:00
|
|
|
}
|
|
|
|
|
2009-07-18 08:32:57 +00:00
|
|
|
} // namespace aria2
|