DCCL v4
Loading...
Searching...
No Matches
logger.cpp
1// Copyright 2012-2023:
2// GobySoft, LLC (2013-)
3// Massachusetts Institute of Technology (2007-2014)
4// Community contributors (see AUTHORS file)
5// File authors:
6// Toby Schneider <toby@gobysoft.org>
7// Chris Murphy <cmurphy@aphysci.com>
8//
9//
10// This file is part of the Dynamic Compact Control Language Library
11// ("DCCL").
12//
13// DCCL is free software: you can redistribute it and/or modify
14// it under the terms of the GNU Lesser General Public License as published by
15// the Free Software Foundation, either version 2.1 of the License, or
16// (at your option) any later version.
17//
18// DCCL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU Lesser General Public License for more details.
22//
23// You should have received a copy of the GNU Lesser General Public License
24// along with DCCL. If not, see <http://www.gnu.org/licenses/>.
25#include <ctime>
26
27#include "logger.h"
28
29dccl::Logger dccl::dlog;
30
31int dccl::internal::LogBuffer::sync()
32{
33#if DCCL_THREAD_SUPPORT
34 if (verbosity() == logger::UNKNOWN)
35 {
36 std::cerr
37 << "Must use 'dlog.is(...) && dlog << ... << std::endl;' expression when running in "
38 "thread safe mode to allow the dlog mutex to be correctly locked and unlocked. "
39 "Offending line: "
40 << buffer_.front() << std::endl;
41 exit(EXIT_FAILURE);
42 }
43
44#endif
45
46 // all but last one
47 while (buffer_.size() > 1)
48 {
49 display(buffer_.front());
50 buffer_.pop_front();
51 }
52
53 if (!verbosity_.empty())
54 verbosity_.pop();
55 if (!group_.empty())
56 group_.pop();
57
58#if DCCL_THREAD_SUPPORT
59 g_dlog_mutex.unlock();
60#endif
61 return 0;
62}
63
64int dccl::internal::LogBuffer::overflow(int c)
65{
66 if (c == EOF)
67 {
68 return c;
69 }
70 else if (c == '\n')
71 {
72 buffer_.emplace_back();
73 }
74 else
75 {
76 buffer_.back().push_back(c);
77 }
78 return c;
79}
80
81void dccl::to_ostream(const std::string& msg, dccl::logger::Verbosity /*vrb*/,
82 dccl::logger::Group grp, std::ostream* os, bool add_timestamp)
83{
84 std::string grp_str;
85 switch (grp)
86 {
87 default:
88 case logger::GENERAL: break;
89 case logger::ENCODE: grp_str = "{encode}: "; break;
90 case logger::DECODE: grp_str = "{decode}: "; break;
91 case logger::SIZE: grp_str = "{size}: "; break;
92 }
93
94 std::time_t now = std::time(nullptr);
95 std::tm* t = std::gmtime(&now);
96
97 if (add_timestamp)
98 {
99 *os << "[ " << (t->tm_year + 1900) << "-" << std::setw(2) << std::setfill('0')
100 << (t->tm_mon + 1) << "-" << std::setw(2) << t->tm_mday << " " << std::setw(2)
101 << t->tm_hour << ":" << std::setw(2) << t->tm_min << ":" << std::setw(2) << t->tm_sec
102 << " ]: " << std::setfill(' ');
103 }
104
105 *os << grp_str << msg << std::endl;
106}
The DCCL Logger class. Do not instantiate this class directly. Rather, use the dccl::dlog object.
Definition logger.h:175