libutil++  1.9.3
 All Classes Functions Variables
LogFormat.cpp
1 /*
2 ** libutil++
3 ** $Id: LogFormat.cpp 1664 2016-04-04 03:57:40Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 static const char rcsid[] __attribute__((used)) = "$Id: LogFormat.cpp 1664 2016-04-04 03:57:40Z sella $";
10 
11 #include "LogFormat.h"
12 #include "Log.h"
13 #include "../Exception.h"
14 
15 #include <sys/time.h>
16 #include <string.h>
17 #include <stdio.h>
18 
19 #include <string>
20 
21 using namespace sella::util;
22 
23 const std::string LogFormat::DefaultStampFormat(DEFAULTSTAMPFORMAT);
24 
25 LogFormat::LogFormat(const std::string &stamp, bool micro) throw () :
26  stamp(stamp),
27  micro(micro)
28 { }
29 
30 std::string LogFormat::format(const struct timeval &stamp) throw () {
31  char buffer[512] = { 0 };
32  char micro[64] = { 0 };
33  struct tm time;
34 
35  localtime_r(&(stamp.tv_sec), &time);
36 
37  if (this->micro) {
38  size_t tlen = strftime(buffer, sizeof(buffer) - sizeof(micro), this->stamp.c_str(), &time);
39  size_t mlen = snprintf(micro, sizeof(micro), ".%06lu", stamp.tv_usec);
40 
41  strncpy(buffer + tlen, micro, mlen);
42  } else {
43  strftime(buffer, sizeof(buffer), this->stamp.c_str(), &time);
44  }
45 
46  return std::string(buffer);
47 }
48 
49 /*
50 ** vim: noet ts=3 sw=3
51 */