/* */ #include "SimpleLogger.h" #include "Util.h" #include "DlAbortEx.h" #include "message.h" #include "a2io.h" #include "a2time.h" #include "StringFormat.h" #include #include #include #include namespace aria2 { #if !defined(va_copy) # if defined(__va_copy) # define va_copy(dest, src) __va_copy(dest, src) # else # define va_copy(dest, src) (dest = src) # endif #endif #define WRITE_LOG(LEVEL, MSG) \ va_list ap;\ va_start(ap, MSG);\ writeFile(Logger::LEVEL, MSG, ap);\ flush();\ va_end(ap); #define WRITE_LOG_EX(LEVEL, MSG, EX) \ va_list ap;\ va_start(ap, EX);\ writeFile(Logger::LEVEL, MSG, ap);\ writeStackTrace(Logger::LEVEL, EX);\ flush();\ va_end(ap); SimpleLogger::SimpleLogger():stdoutField(0) {} SimpleLogger::~SimpleLogger() { closeFile(); } void SimpleLogger::openFile(const std::string& filename) { file.open(filename.c_str(), std::ios::app|std::ios::binary); if(!file) { throw DlAbortEx (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()); } void SimpleLogger::writeLog(std::ostream& o, Logger::LEVEL level, const char* msg, va_list ap, bool printHeader) { 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"; } time_t now = time(NULL); char datestr[20]; struct tm tm; localtime_r(&now, &tm); strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm); // TODO a quick hack not to print header in console if(printHeader) { writeHeader(o, datestr, levelStr); } { char* res; if(vasprintf(&res, std::string(Util::replace(msg, "\r", "")+"\n").c_str(), apCopy) == -1) { o << "SimpleLogger error, cannot allocate memory.\n"; } else { o << res; free(res); } } va_end(apCopy); } void SimpleLogger::writeFile(Logger::LEVEL level, const char* msg, va_list ap) { writeLog(file, level, msg, ap); 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(DEBUG, msg); } void SimpleLogger::debug(const char* msg, Exception& e, ...) { WRITE_LOG_EX(DEBUG, msg, e); } void SimpleLogger::info(const char* msg, ...) { WRITE_LOG(INFO, msg); } void SimpleLogger::info(const char* msg, Exception& e, ...) { WRITE_LOG_EX(INFO, msg, e); } void SimpleLogger::notice(const char* msg, ...) { WRITE_LOG(NOTICE, msg); } void SimpleLogger::notice(const char* msg, Exception& e, ...) { WRITE_LOG_EX(INFO, msg, e); } void SimpleLogger::warn(const char* msg, ...) { WRITE_LOG(WARN, msg); } void SimpleLogger::warn(const char* msg, Exception& e, ...) { WRITE_LOG_EX(WARN, msg, e); } void SimpleLogger::error(const char* msg, ...) { WRITE_LOG(ERROR, msg); } void SimpleLogger::error(const char* msg, Exception& e, ...) { WRITE_LOG_EX(ERROR, msg, e); } } // namespace aria2