Skip to content

Commit

Permalink
support localtime in logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
chenshuo committed Mar 6, 2014
1 parent f692f76 commit 2c80ccb
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/asio/chat/loadtest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void statistic(const boost::ptr_vector<ChatClient>& clients)
}

std::sort(seconds.begin(), seconds.end());
for (size_t i = 0; i < clients.size(); i += clients.size()/20)
for (size_t i = 0; i < clients.size(); i += std::max(static_cast<size_t>(1), clients.size()/20))
{
printf("%6zd%% %.6f\n", i*100/clients.size(), seconds[i]);
}
Expand Down
32 changes: 28 additions & 4 deletions muduo/base/Logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <muduo/base/CurrentThread.h>
#include <muduo/base/Timestamp.h>
#include <muduo/base/TimeZone.h>

#include <errno.h>
#include <stdio.h>
Expand Down Expand Up @@ -101,6 +102,7 @@ void defaultFlush()

Logger::OutputFunc g_output = defaultOutput;
Logger::FlushFunc g_flush = defaultFlush;
TimeZone g_logTimeZone;

}

Expand Down Expand Up @@ -132,16 +134,33 @@ void Logger::Impl::formatTime()
{
t_lastSecond = seconds;
struct tm tm_time;
::gmtime_r(&seconds, &tm_time); // FIXME TimeZone::fromUtcTime
if (g_logTimeZone.valid())
{
tm_time = g_logTimeZone.toLocalTime(seconds);
}
else
{
::gmtime_r(&seconds, &tm_time); // FIXME TimeZone::fromUtcTime
}

int len = snprintf(t_time, sizeof(t_time), "%4d%02d%02d %02d:%02d:%02d",
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec);
assert(len == 17); (void)len;
}
Fmt us(".%06dZ ", microseconds);
assert(us.length() == 9);
stream_ << T(t_time, 17) << T(us.data(), 9);

if (g_logTimeZone.valid())
{
Fmt us(".%06d ", microseconds);
assert(us.length() == 8);
stream_ << T(t_time, 17) << T(us.data(), 8);
}
else
{
Fmt us(".%06dZ ", microseconds);
assert(us.length() == 9);
stream_ << T(t_time, 17) << T(us.data(), 9);
}
}

void Logger::Impl::finish()
Expand Down Expand Up @@ -196,3 +215,8 @@ void Logger::setFlush(FlushFunc flush)
{
g_flush = flush;
}

void Logger::setTimeZone(const TimeZone& tz)
{
g_logTimeZone = tz;
}
3 changes: 3 additions & 0 deletions muduo/base/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
namespace muduo
{

class TimeZone;

class Logger
{
public:
Expand Down Expand Up @@ -68,6 +70,7 @@ class Logger
typedef void (*FlushFunc)();
static void setOutput(OutputFunc);
static void setFlush(FlushFunc);
static void setTimeZone(const TimeZone& tz);

private:

Expand Down
7 changes: 7 additions & 0 deletions muduo/base/TimeZone.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@ TimeZone::TimeZone(const char* zonefile)
}
}

TimeZone::TimeZone(int eastOfUtc, const char* name)
: data_(new TimeZone::Data)
{
data_->localtimes.push_back(detail::Localtime(eastOfUtc, false, 0));
data_->abbreviation = name;
}

struct tm TimeZone::toLocalTime(time_t seconds) const
{
struct tm localTime;
Expand Down
2 changes: 2 additions & 0 deletions muduo/base/TimeZone.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class TimeZone : public muduo::copyable
{
public:
explicit TimeZone(const char* zonefile);
TimeZone(int eastOfUtc, const char* tzname); // a fixed timezone
TimeZone() {} // an invalid timezone

// default copy ctor/assignment/dtor are Okay.

Expand Down
24 changes: 24 additions & 0 deletions muduo/base/tests/Logging_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <muduo/base/Logging.h>
#include <muduo/base/LogFile.h>
#include <muduo/base/ThreadPool.h>
#include <muduo/base/TimeZone.h>

#include <stdio.h>

Expand Down Expand Up @@ -94,4 +95,27 @@ int main()
g_logFile.reset(new muduo::LogFile("test_log_mt", 500*1000*1000, true));
bench("test_log_mt");
g_logFile.reset();

{
g_file = stdout;
sleep(1);
muduo::TimeZone beijing(8*3600, "CST");
muduo::Logger::setTimeZone(beijing);
LOG_TRACE << "trace CST";
LOG_DEBUG << "debug CST";
LOG_INFO << "Hello CST";
LOG_WARN << "World CST";
LOG_ERROR << "Error CST";

sleep(1);
muduo::TimeZone newyork("/usr/share/zoneinfo/America/New_York");
muduo::Logger::setTimeZone(newyork);
LOG_TRACE << "trace NYT";
LOG_DEBUG << "debug NYT";
LOG_INFO << "Hello NYT";
LOG_WARN << "World NYT";
LOG_ERROR << "Error NYT";
g_file = NULL;
}
bench("timezone nop");
}
17 changes: 17 additions & 0 deletions muduo/base/tests/TimeZone_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,28 @@ void testUtc()
}
}

void testFixedTimezone()
{
TimeZone tz(8*3600, "CST");
TestCase cases[] =
{

{ "2014-04-03 00:00:00", "2014-04-03 08:00:00+0800(CST)", false},

};

for (size_t i = 0; i < sizeof cases / sizeof cases[0]; ++i)
{
test(tz, cases[i]);
}
}

int main()
{
testNewYork();
testLondon();
testSydney();
testHongKong();
testFixedTimezone();
testUtc();
}

0 comments on commit 2c80ccb

Please sign in to comment.