00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: LogFormat.cpp 1230 2014-11-16 02:32:01Z sella $";
00010
00011 #include "LogFormat.h"
00012 #include "Log.h"
00013 #include "../Exception.h"
00014
00015 #include <sys/time.h>
00016 #include <string.h>
00017 #include <stdio.h>
00018
00019 #include <string>
00020
00021 #define BUFFER_SIZE (64 * 1024)
00022 #define MICRO_SIZE 64
00023
00024 using namespace sella::util;
00025
00026 const std::string LogFormat::DefaultStampFormat = "%Y-%m-%d+%H:%M:%S";
00027
00028 LogFormat::LogFormat(const std::string &stamp, bool micro) throw () :
00029 stamp(stamp),
00030 micro(micro)
00031 { }
00032
00033 std::string LogFormat::format(const struct timeval &stamp) throw () {
00034 char buffer[BUFFER_SIZE];
00035 char micro[MICRO_SIZE];
00036 struct tm time;
00037
00038 localtime_r(&(stamp.tv_sec), &time);
00039
00040 if (this->micro) {
00041 snprintf(micro, sizeof(micro), ".%06lu", stamp.tv_usec);
00042
00043 strncpy(&(buffer[strftime(buffer, sizeof(buffer)/2, this->stamp.c_str(), &time)]), micro, sizeof(buffer)/2);
00044 } else {
00045 strftime(buffer, sizeof(buffer), this->stamp.c_str(), &time);
00046 }
00047
00048 return std::string(buffer);
00049 }
00050
00051
00052
00053