YARP
Yet Another Robot Platform
LogStream.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2018 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/Os.h>
16 
17 #include <cstdio>
18 #include <cstdlib>
19 #include <iosfwd>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23 
24 namespace std {
25 template <typename T>
26 std::ostream& operator<<(std::ostream& os, const std::vector<T>& t);
27 }
28 
29 namespace yarp {
30 namespace os {
31 
33 {
34  struct Stream
35  {
36  Stream(Log::LogType t, const char* fn, unsigned int l, const char* f) :
37  type(t), file(fn), line(l), func(f), ref(1)
38  {
39  }
40  std::ostringstream oss;
42  const char* file;
43  unsigned int line;
44  const char* func;
45  int ref;
46  } * stream;
47 
48 public:
49  inline LogStream(Log::LogType type,
50  const char* file,
51  unsigned int line,
52  const char* func) :
53  stream(new Stream(type, file, line, func))
54  {
55  }
56 
57  inline LogStream(const LogStream& o) :
58  stream(o.stream)
59  {
60  ++stream->ref;
61  }
62 
63  inline ~LogStream()
64  {
65  if (!--stream->ref) {
66  if (Log::print_callback) {
67  Log::print_callback(stream->type,
68  stream->oss.str().c_str(),
69  stream->file,
70  stream->line,
71  stream->func);
72  }
73  if (Log::forward_callback) {
74  Log::forward_callback(stream->type,
75  stream->oss.str().c_str(),
76  stream->file,
77  stream->line,
78  stream->func);
79  }
80  if (stream->type == yarp::os::Log::FatalType) {
81  yarp_print_trace(stderr, stream->file, stream->line);
82  delete stream;
83  std::exit(-1);
84  }
85  delete stream;
86  }
87  }
88 
89  inline LogStream& operator<<(bool t)
90  {
91  stream->oss << (t ? "true" : "false");
92  stream->oss << ' ';
93  return *this;
94  }
95  inline LogStream& operator<<(char t)
96  {
97  stream->oss << t;
98  stream->oss << ' ';
99  return *this;
100  }
101  inline LogStream& operator<<(signed short t)
102  {
103  stream->oss << t;
104  stream->oss << ' ';
105  return *this;
106  }
107  inline LogStream& operator<<(unsigned short t)
108  {
109  stream->oss << t;
110  stream->oss << ' ';
111  return *this;
112  }
113  inline LogStream& operator<<(signed int t)
114  {
115  stream->oss << t;
116  stream->oss << ' ';
117  return *this;
118  }
119  inline LogStream& operator<<(unsigned int t)
120  {
121  stream->oss << t;
122  stream->oss << ' ';
123  return *this;
124  }
125  inline LogStream& operator<<(signed long t)
126  {
127  stream->oss << t;
128  stream->oss << ' ';
129  return *this;
130  }
131  inline LogStream& operator<<(unsigned long t)
132  {
133  stream->oss << t;
134  stream->oss << ' ';
135  return *this;
136  }
137  inline LogStream& operator<<(signed long long t)
138  {
139  stream->oss << t;
140  stream->oss << ' ';
141  return *this;
142  }
143  inline LogStream& operator<<(unsigned long long t)
144  {
145  stream->oss << t;
146  stream->oss << ' ';
147  return *this;
148  }
149  inline LogStream& operator<<(float t)
150  {
151  stream->oss << t;
152  stream->oss << ' ';
153  return *this;
154  }
155  inline LogStream& operator<<(double t)
156  {
157  stream->oss << t;
158  stream->oss << ' ';
159  return *this;
160  }
161  inline LogStream& operator<<(const char* t)
162  {
163  stream->oss << t;
164  stream->oss << ' ';
165  return *this;
166  }
167  inline LogStream& operator<<(const void* t)
168  {
169  stream->oss << t;
170  stream->oss << ' ';
171  return *this;
172  }
173 
174  inline LogStream& operator<<(const std::string& t)
175  {
176  stream->oss << t.c_str();
177  stream->oss << ' ';
178  return *this;
179  }
180 
181  template <typename T>
182  inline LogStream& operator<<(const std::vector<T>& t)
183  {
184  stream->oss << t;
185  stream->oss << ' ';
186  return *this;
187  }
188 }; // class LogStream
189 
190 } // namespace os
191 } // namespace yarp
192 
193 
194 template <typename T>
195 inline std::ostream& std::operator<<(std::ostream& os, const std::vector<T>& t)
196 {
197  os << '[';
198  for (typename std::vector<T>::const_iterator it = t.begin(); it != t.end(); ++it) {
199  const T& p = *it;
200  if (it != t.begin()) {
201  os << ", ";
202  }
203  os << p;
204  }
205  os << ']';
206  return os;
207 }
208 
209 #endif // YARP_OS_LOGSTREAM_H
LogStream & operator<<(unsigned short t)
Definition: LogStream.h:107
LogStream & operator<<(const void *t)
Definition: LogStream.h:167
LogStream & operator<<(float t)
Definition: LogStream.h:149
Stream(Log::LogType t, const char *fn, unsigned int l, const char *f)
Definition: LogStream.h:36
LogStream & operator<<(char t)
Definition: LogStream.h:95
LogStream & operator<<(signed int t)
Definition: LogStream.h:113
LogStream & operator<<(unsigned int t)
Definition: LogStream.h:119
LogStream & operator<<(signed short t)
Definition: LogStream.h:101
STL namespace.
#define YARP_OS_API
Definition: api.h:19
LogStream & operator<<(bool t)
Definition: LogStream.h:89
The main, catch-all namespace for YARP.
Definition: numeric.h:47
LogStream(Log::LogType type, const char *file, unsigned int line, const char *func)
Definition: LogStream.h:49
LogStream & operator<<(double t)
Definition: LogStream.h:155
LogStream & operator<<(const std::string &t)
Definition: LogStream.h:174
float t
void yarp_print_trace(FILE *out, const char *file, int line)
Low level function for printing a stack trace, if implemented (ACE or gcc/Linux). ...
Definition: Log.cpp:420
LogStream & operator<<(unsigned long t)
Definition: LogStream.h:131
std::ostringstream oss
Definition: LogStream.h:40
LogStream & operator<<(unsigned long long t)
Definition: LogStream.h:143
LogStream & operator<<(const char *t)
Definition: LogStream.h:161
LogStream & operator<<(signed long t)
Definition: LogStream.h:125
LogStream & operator<<(signed long long t)
Definition: LogStream.h:137
LogStream(const LogStream &o)
Definition: LogStream.h:57