ArmarXLogBuf.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "ArmarXLogBuf.h"
25
26#include <SimoxUtility/algorithm/string/string_tools.h>
27
28armarx::ArmarXLogBuf::ArmarXLogBuf(const std::string& tagName,
29 MessageTypeT severity,
30 bool flushOnlyOnNewLine) :
31 _buffer(1024), flushOnlyOnNewLine(flushOnlyOnNewLine)
32{
33 _outBuffer.reserve(1024);
34 char* base = &_buffer.front();
35 setp(base, base + _buffer.size() - 1);
36 log = armarx::LogSender::createLogSender()->setTag(LogTag(tagName));
37 *log << severity;
38}
39
40int
41armarx::ArmarXLogBuf::overflow(int c)
42{
43 std::unique_lock lock(mutex);
44 if (c == EOF)
45 {
46 return EOF;
47 }
48
49 if (epptr() == pptr())
50 {
51 write();
52 }
53
54 *pptr() = c;
55 pbump(1);
56 return c;
57}
58
59int
60armarx::ArmarXLogBuf::sync()
61{
62 std::unique_lock lock(mutex);
63 flush();
64 return 0;
65}
66
67void
68armarx::ArmarXLogBuf::write()
69{
70 for (char *p = pbase(), *e = pptr(); p != e; ++p)
71 {
72 _outBuffer += (*p);
73 }
74 std::ptrdiff_t n = pptr() - pbase();
75 pbump(-n);
76}
77
78void
79armarx::ArmarXLogBuf::flush()
80{
81 write();
82 if (!flushOnlyOnNewLine)
83 {
84 simox::alg::trim(_outBuffer);
85 }
86 if (_outBuffer.size() == 0)
87 {
88 return;
89 }
90
91 try
92 {
93 *log << _outBuffer;
94 if (!flushOnlyOnNewLine || _outBuffer.find('\n') != std::string::npos)
95 {
96 log->flush();
97 }
98 _outBuffer.clear();
99 }
100 catch (...)
101 {
102 }
103}
constexpr T c
ArmarXLogBuf(const std::string &tagName, MessageTypeT severity, bool flushOnlyOnNewLine)
static LogSenderPtr createLogSender()
Definition LogSender.cpp:82
void write(WriterT &aron_w, const Eigen::Matrix< EigenT, rows, cols, options > &input, typename WriterT::ReturnType &ret, const armarx::aron::Path &aron_p=armarx::aron::Path())
Definition eigen.h:138
const LogSender::manipulator flush
Definition LogSender.h:251
MessageTypeT
Definition LogSender.h:46
constexpr auto n() noexcept