YARP
Yet Another Robot Platform
LogStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #ifndef YARP_OS_LOGSTREAM_H
10 #define YARP_OS_LOGSTREAM_H
11 
12 #include <yarp/os/api.h>
13 
14 #include <yarp/os/Log.h>
15 #include <yarp/os/LogComponent.h>
16 #include <yarp/os/Os.h>
17 #include <yarp/os/SystemClock.h>
18 #include <yarp/os/Time.h>
19 
20 #include <cstdio>
21 #include <cstdlib>
22 #include <iosfwd>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 
27 
28 namespace std {
29 template <typename T>
30 std::ostream& operator<<(std::ostream& os, const std::vector<T>& t);
31 }
32 
33 namespace yarp {
34 namespace os {
35 
37 {
38  struct Stream
39  {
40  Stream(Log::LogType t,
41  const char* fn,
42  unsigned int l,
43  const char* f,
44  const double ct,
45  const yarp::os::Log::Predicate pred,
46  const LogComponent& c) :
47  type(t),
48  file(fn),
49  line(l),
50  func(f),
53  externaltime(ct),
54  pred(pred),
55  comp(c),
56  ref(1)
57  {
58  }
59  std::ostringstream oss; // NOLINT(misc-non-private-member-variables-in-classes)
60  Log::LogType type; // NOLINT(misc-non-private-member-variables-in-classes)
61  const char* file; // NOLINT(misc-non-private-member-variables-in-classes)
62  unsigned int line; // NOLINT(misc-non-private-member-variables-in-classes)
63  const char* func; // NOLINT(misc-non-private-member-variables-in-classes)
64  double systemtime; // NOLINT(misc-non-private-member-variables-in-classes)
65  double networktime; // NOLINT(misc-non-private-member-variables-in-classes)
66  double externaltime; // NOLINT(misc-non-private-member-variables-in-classes)
67  const yarp::os::Log::Predicate pred; // NOLINT(misc-non-private-member-variables-in-classes)
68  const LogComponent& comp; // NOLINT(misc-non-private-member-variables-in-classes)
69  int ref; // NOLINT(misc-non-private-member-variables-in-classes)
70  } * stream;
71 
72 public:
73  inline LogStream(Log::LogType type,
74  const char* file,
75  unsigned int line,
76  const char* func,
77  const double externaltime,
78  const yarp::os::Log::Predicate pred = nullptr,
79  const LogComponent& comp = Log::defaultLogComponent()) :
80  stream(new Stream(type, file, line, func, externaltime, pred, comp))
81  {
82  }
83 
84  inline LogStream(const LogStream& o) :
85  stream(o.stream)
86  {
87  ++stream->ref;
88  }
89 
90  inline ~LogStream()
91  {
92  if (!--stream->ref) {
93  if (!stream->pred || stream->pred()) {
94  std::string s = stream->oss.str();
95  if (!s.empty()) {
96  // remove the last character if it an empty space (i.e.
97  // always unless the user defined an operator<< that
98  // does not add an empty space.
99  if (s.back() == ' ') {
100  s.pop_back();
101  } else {
102  yarp::os::Log(stream->file, stream->line, stream->func, nullptr, yarp::os::Log::logInternalComponent()).warning(
103  "' ' was expected. Some `operator<<` does not add an extra space at the end");
104  }
105  // remove the last character if it is a \n
106  if (s.back() == '\n') {
107  yarp::os::Log(stream->file, stream->line, stream->func, nullptr, yarp::os::Log::logInternalComponent()).warning(
108  "Removing extra \\n (stream-style)");
109  s.pop_back();
110  }
111  }
112  Log::do_log(stream->type,
113  s.c_str(),
114  stream->file,
115  stream->line,
116  stream->func,
117  stream->systemtime,
118  stream->networktime,
119  stream->externaltime,
120  stream->comp);
121  }
122 
123  if (stream->type == yarp::os::Log::FatalType) {
124  yarp_print_trace(stderr, stream->file, stream->line);
125  delete stream;
126  std::exit(-1);
127  }
128  delete stream;
129  }
130  }
131 
132  inline LogStream& operator<<(bool t)
133  {
134  stream->oss << (t ? "true" : "false");
135  stream->oss << ' ';
136  return *this;
137  }
138  inline LogStream& operator<<(char t)
139  {
140  stream->oss << t;
141  stream->oss << ' ';
142  return *this;
143  }
144  inline LogStream& operator<<(signed short t)
145  {
146  stream->oss << t;
147  stream->oss << ' ';
148  return *this;
149  }
150  inline LogStream& operator<<(unsigned short t)
151  {
152  stream->oss << t;
153  stream->oss << ' ';
154  return *this;
155  }
156  inline LogStream& operator<<(signed int t)
157  {
158  stream->oss << t;
159  stream->oss << ' ';
160  return *this;
161  }
162  inline LogStream& operator<<(unsigned int t)
163  {
164  stream->oss << t;
165  stream->oss << ' ';
166  return *this;
167  }
168  inline LogStream& operator<<(signed long t)
169  {
170  stream->oss << t;
171  stream->oss << ' ';
172  return *this;
173  }
174  inline LogStream& operator<<(unsigned long t)
175  {
176  stream->oss << t;
177  stream->oss << ' ';
178  return *this;
179  }
180  inline LogStream& operator<<(signed long long t)
181  {
182  stream->oss << t;
183  stream->oss << ' ';
184  return *this;
185  }
186  inline LogStream& operator<<(unsigned long long t)
187  {
188  stream->oss << t;
189  stream->oss << ' ';
190  return *this;
191  }
192  inline LogStream& operator<<(float t)
193  {
194  stream->oss << t;
195  stream->oss << ' ';
196  return *this;
197  }
198  inline LogStream& operator<<(double t)
199  {
200  stream->oss << t;
201  stream->oss << ' ';
202  return *this;
203  }
204  inline LogStream& operator<<(const char* t)
205  {
206  stream->oss << t;
207  stream->oss << ' ';
208  return *this;
209  }
210  inline LogStream& operator<<(const void* t)
211  {
212  stream->oss << t;
213  stream->oss << ' ';
214  return *this;
215  }
216 
217  inline LogStream& operator<<(const std::string& t)
218  {
219  stream->oss << t.c_str();
220  stream->oss << ' ';
221  return *this;
222  }
223 
224  template <typename T>
225  inline LogStream& operator<<(const std::vector<T>& t)
226  {
227  stream->oss << t;
228  stream->oss << ' ';
229  return *this;
230  }
231 }; // class LogStream
232 
233 } // namespace os
234 } // namespace yarp
235 
236 
237 template <typename T>
238 inline std::ostream& std::operator<<(std::ostream& os, const std::vector<T>& t)
239 {
240  os << '[';
241  for (typename std::vector<T>::const_iterator it = t.begin(); it != t.end(); ++it) {
242  const T& p = *it;
243  if (it != t.begin()) {
244  os << ", ";
245  }
246  os << p;
247  }
248  os << ']';
249  return os;
250 }
251 
252 #endif // YARP_OS_LOGSTREAM_H
SystemClock.h
yarp::os::LogStream::operator<<
LogStream & operator<<(const std::vector< T > &t)
Definition: LogStream.h:225
operator<<
std::ostream & operator<<(std::ostream &os, StrStream &sstr)
Definition: utility.cpp:89
yarp::os::LogStream::operator<<
LogStream & operator<<(unsigned short t)
Definition: LogStream.h:150
yarp::os::LogStream::operator<<
LogStream & operator<<(char t)
Definition: LogStream.h:138
yarp::os::Log::warning
void warning(const char *msg,...) const
Definition: Log.cpp:1008
t
float t
Definition: FfmpegWriter.cpp:74
yarp::os::LogStream::operator<<
LogStream & operator<<(float t)
Definition: LogStream.h:192
yarp::os::LogStream::operator<<
LogStream & operator<<(signed int t)
Definition: LogStream.h:156
api.h
YARP_os_API
#define YARP_os_API
Definition: api.h:19
yarp::os::LogStream::operator<<
LogStream & operator<<(bool t)
Definition: LogStream.h:132
yarp::os::LogStream::operator<<
LogStream & operator<<(unsigned int t)
Definition: LogStream.h:162
yarp::os::LogStream::operator<<
LogStream & operator<<(signed short t)
Definition: LogStream.h:144
yarp::os::LogStream::~LogStream
~LogStream()
Definition: LogStream.h:90
yarp::os::Time::now
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition: Time.cpp:124
yarp::os::SystemClock::nowSystem
static double nowSystem()
Definition: SystemClock.cpp:37
yarp::os::Log::LogType
LogType
Definition: Log.h:75
yarp::os::LogStream::operator<<
LogStream & operator<<(double t)
Definition: LogStream.h:198
yarp::os::LogStream::LogStream
LogStream(Log::LogType type, const char *file, unsigned int line, const char *func, const double externaltime, const yarp::os::Log::Predicate pred=nullptr, const LogComponent &comp=Log::defaultLogComponent())
Definition: LogStream.h:73
Log.h
yarp::os::LogComponent
Definition: LogComponent.h:21
yarp::os::LogStream::operator<<
LogStream & operator<<(const std::string &t)
Definition: LogStream.h:217
Os.h
yarp::os::Log::Predicate
bool(*)() Predicate
Definition: Log.h:55
yarp_print_trace
void yarp_print_trace(FILE *out, const char *file, unsigned int line)
Low level function for printing a stack trace, if implemented (ACE or gcc/Linux).
Definition: Log.cpp:1068
yarp::os::LogStream::operator<<
LogStream & operator<<(unsigned long t)
Definition: LogStream.h:174
LogComponent.h
yarp::os::LogStream::operator<<
LogStream & operator<<(unsigned long long t)
Definition: LogStream.h:186
yarp::os::LogStream::operator<<
LogStream & operator<<(const char *t)
Definition: LogStream.h:204
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::Time::isSystemClock
bool isSystemClock()
Check if YARP is providing system time.
Definition: Time.cpp:265
yarp::os::Time::isClockInitialized
bool isClockInitialized()
Check if YARP clock is initialized.
Definition: Time.cpp:260
yarp::os::LogStream::operator<<
LogStream & operator<<(signed long long t)
Definition: LogStream.h:180
Time.h
yarp::os::LogStream::operator<<
LogStream & operator<<(signed long t)
Definition: LogStream.h:168
yarp::os::LogStream
Definition: LogStream.h:37
yarp::os::Log
Definition: Log.h:53
yarp::os::Log::FatalType
@ FatalType
Definition: Log.h:82
yarp::os::LogStream::operator<<
LogStream & operator<<(const void *t)
Definition: LogStream.h:210
yarp::os::LogStream::LogStream
LogStream(const LogStream &o)
Definition: LogStream.h:84