aria2/src/SimpleLogFormatter.cc

95 lines
3.1 KiB
C++
Raw Normal View History

2006-02-17 13:35:04 +00:00
/* <!-- copyright */
/*
* aria2 - The high speed download utility
2006-02-17 13:35:04 +00:00
*
* Copyright (C) 2010 Tatsuhiro Tsujikawa
2006-02-17 13:35:04 +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
* 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.
2006-02-17 13:35:04 +00:00
*/
/* copyright --> */
#include "SimpleLogFormatter.h"
#include <cassert>
#include <ostream>
#include "util.h"
2007-08-01 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> Make a2netcompat.h include a2io.h to fix compilation error: * src/a2netcompat.h * src/SocketCore.cc: Removed include of a2io.h * src/Util.cc: Removed include of a2io.h Gather time related functions to a2time.h: * src/a2time.h: New file. * src/DefaultPeerStorage.cc * src/SimpleLogger.cc * src/Util.{h, cc} * src/SimpleRandomizer.h * src/TimeA2.{h,cc} * src/DownloadCommand.cc * src/main.cc Removed #ifdef __MINGW32__ since gai_strerror is included in a2netcompat.h: * src/NameResolver.cc Fixed compilation error without openssl: * src/SocketCore.{h,cc}: Moved include of openssl/err.h to SocketCore.h Added default block to suppress compiler warnings: * src/MetalinkRequestInfo.cc (AccumulateNonP2PUrl::operator()) 2007-07-26 Ross Smith II <aria2spam at smithii dot com> MinGW build enhancements. The following files are added: * src/gai_strerror.{c,h} * src/gettimeofday.{c,h} Changes to support the above new files: * configure.ac * src/Makefile.am * src/a2netcompat.h * src/TimeA2.cc * src/DefaultPeerStorage.cc * src/NameResolver.cc: removed mingw_strerror() function. * src/SocketCore.cc: removed mingw_strerror() function. Miscellaneous MinGW build fixes. * src/a2io.h: Use _lseeki64() instead of lseek() * src/common.h * src/DefaultFileAllocator.cc * src/GlowFileAllocator.cc * src/main.cc: Moved #include "prefs.h" to quiet compile error. * src/NameResolver.cc (callback): Changed int32_t to int. (resolve): Changed int32_t to int. * src/Platform.cc * src/Platform.h * test/MultiDiskWriterTest.cc * test/PeerMessageUtilTest.cc * src/localtime_r.c: Add DeleteCriticalSection() and at exit(). Other enhancements and fixes. * src/HttpRequestCommand.cc (executeInternal) Use non-blocking socket for HTTPS (MinGW only). * src/SocketCore.cc: (error): New function to abstract errno/WSAGetLastError(). (errorMsg): New function to abstract errno/WSAGetLastError(). (initiateSecureConnection): Added more detailed error reporting. * src/SocketCore.h: Added private variable blocking, to allow proper handling of OpenSSL psuedo-errors. * src/message.h (EX_SSL_INIT_FAILURE) (EX_SSL_IO_ERROR) (EX_SSL_PROTOCOL_ERROR) (EX_SSL_UNKNOWN_ERROR) (EX_SSL_CONNECT_ERROR) (EX_SOCKET_BLOCKING) (EX_SOCKET_NONBLOCKING) (EX_SOCKET_UNKNOWN_ERROR) * src/Util.cc (setGlobalSignalHandler): Renamed signal to sig as signal is a reserved name. (httpGMT): Fixed typo.
2007-07-31 16:45:16 +00:00
#include "a2time.h"
#include "A2STR.h"
#include "StringFormat.h"
#include "Exception.h"
namespace aria2 {
2006-02-17 13:35:04 +00:00
SimpleLogFormatter::SimpleLogFormatter() {}
SimpleLogFormatter::~SimpleLogFormatter() {}
static void writeHeader
(std::ostream& o, const std::string& date, const std::string& logLevelLabel)
{
o << StringFormat("%s %s - ", date.c_str(), logLevelLabel.c_str());
}
void SimpleLogFormatter::writeLog
(std::ostream& o, Logger::LEVEL level, const std::string& logLevelLabel,
const char* msg, va_list ap)
2006-02-21 12:27:17 +00:00
{
struct timeval tv;
gettimeofday(&tv, 0);
char datestr[27]; // 'YYYY-MM-DD hh:mm:ss.uuuuuu'+'\0' = 27 bytes
struct tm tm;
//tv.tv_sec may not be of type time_t.
time_t timesec = tv.tv_sec;
localtime_r(&timesec, &tm);
size_t dateLength =
strftime(datestr, sizeof(datestr), "%Y-%m-%d %H:%M:%S", &tm);
assert(dateLength <= (size_t)20);
snprintf(datestr+dateLength, sizeof(datestr)-dateLength,
".%06ld", tv.tv_usec);
writeHeader(o, datestr, logLevelLabel);
{
char buf[1024];
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";
} else {
o << buf;
}
}
}
void SimpleLogFormatter::writeStackTrace
(std::ostream& o, Logger::LEVEL level, const std::string& logLevelLabel,
const Exception& e)
{
o << e.stackTrace();
2006-02-17 13:35:04 +00:00
}
} // namespace aria2