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 
28 armarx::ArmarXLogBuf::ArmarXLogBuf(const std::string& tagName, MessageTypeT severity, bool flushOnlyOnNewLine) :
29  _buffer(1024),
30  flushOnlyOnNewLine(flushOnlyOnNewLine)
31 {
32  _outBuffer.reserve(1024);
33  char* base = &_buffer.front();
34  setp(base, base + _buffer.size() - 1);
36  *log << severity;
37 }
38 
39 int armarx::ArmarXLogBuf::overflow(int c)
40 {
41  std::unique_lock lock(mutex);
42  if (c == EOF)
43  {
44  return EOF;
45  }
46 
47  if (epptr() == pptr())
48  {
49  write();
50  }
51 
52  *pptr() = c;
53  pbump(1);
54  return c;
55 }
56 
57 int armarx::ArmarXLogBuf::sync()
58 {
59  std::unique_lock lock(mutex);
60  flush();
61  return 0;
62 }
63 
64 void armarx::ArmarXLogBuf::write()
65 {
66  for (char* p = pbase(), *e = pptr(); p != e; ++p)
67  {
68  _outBuffer += (*p);
69  }
70  std::ptrdiff_t n = pptr() - pbase();
71  pbump(-n);
72 }
73 
74 void armarx::ArmarXLogBuf::flush()
75 {
76  write();
77  if (!flushOnlyOnNewLine)
78  {
79  simox::alg::trim(_outBuffer);
80  }
81  if (_outBuffer.size() == 0)
82  {
83  return;
84  }
85 
86  try
87  {
88  *log << _outBuffer;
89  if (!flushOnlyOnNewLine || _outBuffer.find('\n') != std::string::npos)
90  {
91  log->flush();
92  }
93  _outBuffer.clear();
94  }
95  catch (...)
96  {
97 
98  }
99 }
armarx::MessageTypeT
MessageTypeT
Definition: LogSender.h:45
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
ArmarXLogBuf.h
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::control::hardware_config::tagName
std::string tagName(ConfigTag tag)
Definition: Config.cpp:302
armarx::ArmarXLogBuf::ArmarXLogBuf
ArmarXLogBuf(const std::string &tagName, MessageTypeT severity, bool flushOnlyOnNewLine)
Definition: ArmarXLogBuf.cpp:28
armarx::aron::write
requires data::isWriter< WriterT > 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:130
armarx::LogSender::createLogSender
static LogSenderPtr createLogSender()
Definition: LogSender.cpp:86
armarx::LogTag
Definition: LoggingUtil.h:66