DCCL v3
logger.cpp
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 #include <ctime>
23 
24 #include "dccl/logger.h"
25 
26 dccl::Logger dccl::dlog;
27 
28 int dccl::internal::LogBuffer::sync() {
29  // all but last one
30  while(buffer_.size() > 1) {
31  display(buffer_.front());
32  buffer_.pop_front();
33  }
34  verbosity_ = logger::INFO;
35  group_ = logger::GENERAL;
36 
37  return 0;
38 }
39 
40 int dccl::internal::LogBuffer::overflow(int c) {
41  if (c == EOF) { return c; }
42  else if(c == '\n') { buffer_.push_back(std::string()); }
43  else { buffer_.back().push_back(c); }
44  return c;
45 }
46 
47 void dccl::to_ostream(const std::string& msg, dccl::logger::Verbosity vrb,
48  dccl::logger::Group grp, std::ostream* os,
49  bool add_timestamp)
50 {
51  std::string grp_str;
52  switch(grp)
53  {
54  default:
55  case logger::GENERAL: break;
56  case logger::ENCODE: grp_str = "{encode}: "; break;
57  case logger::DECODE: grp_str = "{decode}: "; break;
58  case logger::SIZE: grp_str = "{size}: "; break;
59  }
60 
61  std::time_t now = std::time(0);
62  std::tm* t = std::gmtime(&now);
63 
64  if(add_timestamp)
65  {
66  *os << "[ " << (t->tm_year+1900) << "-"
67  << std::setw(2) << std::setfill('0') << (t->tm_mon+1) << "-"
68  << std::setw(2) << t->tm_mday
69  << " "
70  << std::setw(2) << t->tm_hour << ":"
71  << std::setw(2) << t->tm_min << ":"
72  << std::setw(2) << t->tm_sec << " ]: "
73  << std::setfill(' ');
74  }
75 
76  *os << grp_str << msg << std::endl;
77 
78 }
dccl::Logger
The DCCL Logger class. Do not instantiate this class directly. Rather, use the dccl::dlog object.
Definition: logger.h:131