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 <iosfwd>
13 #include <sstream>
14 #include <vector>
15 
16 #include <yarp/os/api.h>
17 #include <string>
18 #include <yarp/os/Log.h>
19 #include <yarp/os/Os.h>
20 
21 #include <cstdio>
22 #include <cstdlib>
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  struct Stream {
34  Stream(Log::LogType t, const char *fn, unsigned int l, const char *f) : type(t), file(fn), line(l), func(f), ref(1) {}
35  std::ostringstream oss;
37  const char *file;
38  unsigned int line;
39  const char *func;
40  int ref;
41  } *stream;
42 public:
43 
44  inline LogStream(Log::LogType type,
45  const char *file,
46  unsigned int line,
47  const char *func) :
48  stream(new Stream(type, file, line, func))
49  {
50  }
51 
52  inline LogStream(const LogStream &o) : stream(o.stream) {
53  ++stream->ref;
54  }
55 
56  inline ~LogStream() {
57  if (!--stream->ref) {
58  if (Log::print_callback) {
59  Log::print_callback(stream->type, stream->oss.str().c_str(), stream->file, stream->line, stream->func);
60  }
61  if (Log::forward_callback) {
62  Log::forward_callback(stream->type, stream->oss.str().c_str(), stream->file, stream->line, stream->func);
63  }
64  if (stream->type == yarp::os::Log::FatalType) {
65  yarp_print_trace(stderr, stream->file, stream->line);
66  delete stream;
67  std::exit(-1);
68  }
69  delete stream;
70  }
71  }
72 
73  inline LogStream& operator<<(bool t) {
74  stream->oss << (t ? "true" : "false");
75  stream->oss << ' ';
76  return *this;
77  }
78  inline LogStream& operator<<(char t) {
79  stream->oss << t;
80  stream->oss << ' ';
81  return *this;
82  }
83  inline LogStream& operator<<(signed short t) {
84  stream->oss << t;
85  stream->oss << ' ';
86  return *this;
87  }
88  inline LogStream& operator<<(unsigned short t) {
89  stream->oss << t;
90  stream->oss << ' ';
91  return *this;
92  }
93  inline LogStream& operator<<(signed int t) {
94  stream->oss << t;
95  stream->oss << ' ';
96  return *this;
97  }
98  inline LogStream& operator<<(unsigned int t) {
99  stream->oss << t;
100  stream->oss << ' ';
101  return *this;
102  }
103  inline LogStream& operator<<(signed long t) {
104  stream->oss << t;
105  stream->oss << ' ';
106  return *this;
107  }
108  inline LogStream& operator<<(unsigned long t) {
109  stream->oss << t;
110  stream->oss << ' ';
111  return *this;
112  }
113  inline LogStream& operator<<(signed long long t) {
114  stream->oss << t;
115  stream->oss << ' ';
116  return *this;
117  }
118  inline LogStream& operator<<(unsigned long long t) {
119  stream->oss << t;
120  stream->oss << ' ';
121  return *this;
122  }
123  inline LogStream& operator<<(float t) {
124  stream->oss << t;
125  stream->oss << ' ';
126  return *this;
127  }
128  inline LogStream& operator<<(double t) {
129  stream->oss << t;
130  stream->oss << ' ';
131  return *this;
132  }
133  inline LogStream& operator<<(const char* t) {
134  stream->oss << t;
135  stream->oss << ' ';
136  return *this;
137  }
138  inline LogStream& operator<<(const void * t) {
139  stream->oss << t;
140  stream->oss << ' ';
141  return *this;
142  }
143 
144  inline LogStream& operator<<(const std::string &t) {
145  stream->oss << t.c_str();
146  stream->oss << ' ';
147  return *this;
148  }
149 
150  template <typename T>
151  inline LogStream& operator<<(const std::vector<T> &t)
152  {
153  stream->oss << t;
154  stream->oss << ' ';
155  return *this;
156  }
157 }; // class LogStream
158 
159 } // namespace os
160 } // namespace yarp
161 
162 
163 
164 template <typename T>
165 inline std::ostream& std::operator<<(std::ostream &os, const std::vector<T> &t)
166 {
167  os << '[';
168  for (typename std::vector<T>::const_iterator it = t.begin(); it != t.end(); ++it) {
169  const T &p = *it;
170  if (it != t.begin()) {
171  os << ", ";
172  }
173  os << p;
174  }
175  os << ']';
176  return os;
177 }
178 
179 #endif // YARP_OS_LOGSTREAM_H
LogStream & operator<<(unsigned short t)
Definition: LogStream.h:88
LogStream & operator<<(const void *t)
Definition: LogStream.h:138
LogStream & operator<<(float t)
Definition: LogStream.h:123
Stream(Log::LogType t, const char *fn, unsigned int l, const char *f)
Definition: LogStream.h:34
LogStream & operator<<(char t)
Definition: LogStream.h:78
LogStream & operator<<(signed int t)
Definition: LogStream.h:93
LogStream & operator<<(unsigned int t)
Definition: LogStream.h:98
LogStream & operator<<(signed short t)
Definition: LogStream.h:83
STL namespace.
#define YARP_OS_API
Definition: api.h:19
LogStream & operator<<(bool t)
Definition: LogStream.h:73
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:44
LogStream & operator<<(double t)
Definition: LogStream.h:128
LogStream & operator<<(const std::string &t)
Definition: LogStream.h:144
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:108
std::ostringstream oss
Definition: LogStream.h:35
LogStream & operator<<(unsigned long long t)
Definition: LogStream.h:118
LogStream & operator<<(const char *t)
Definition: LogStream.h:133
LogStream & operator<<(signed long t)
Definition: LogStream.h:103
LogStream & operator<<(signed long long t)
Definition: LogStream.h:113
LogStream(const LogStream &o)
Definition: LogStream.h:52