logger
Logfile.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005-2006 LAAS/CNRS <openrobots@laas.fr>
3  * Sylvain Joyeux <sylvain.joyeux@m4x.org>
4  *
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29  * DAMAGE.
30  */
31 
32 
33 #ifndef LOGOUTPUT_H
34 #define LOGOUTPUT_H
35 
36 #include "Logging.hh"
37 
38 #include <vector>
39 #include <iosfwd>
40 #include <typelib/endian_swap.hh>
41 #include <logger/LoggerTypes.hpp>
42 
43 namespace Typelib {
44  class Registry;
45 }
46 namespace Logging
47 {
48  class Logfile
49  {
50  template<class T> friend Logfile& operator << (Logfile& output, const T& value);
51  static const size_t nstream = static_cast<size_t>(-1);
52 
53  std::ostream& m_stream;
54  int m_stream_idx;
55 
56  private:
57  template<class T>
58  void write(const T& data)
59  {
60  T little_endian = Typelib::Endian::to_little(data);
61  m_stream.write( reinterpret_cast<const char*>(&little_endian), sizeof(T) );
62  }
63 
64 
65  public:
66  Logfile(std::ostream& stream);
67 
68  std::ostream& getStream();
69 
70  int newStreamIndex();
71 
72  void writeStreamDeclaration(int stream_index, StreamType type,
73  std::string const& name, std::string const& type_name,
74  std::string const& cxx_type_name, std::string const& type_def,
75  std::vector<logger::StreamMetadata> const& metadata);
76  void writeSampleHeader(int stream_index, base::Time const& realtime, base::Time const& logical, size_t size);
77  void writeSample(int stream_index, base::Time const& realtime, base::Time const& logical, void* payload_data, size_t payload_size);
78  };
79 
80  namespace details
81  {
82  template <typename T> struct static_failure;
83  }
84 
86  void writePrologue(std::ostream& stream);
87 
88  template<class T>
89  Logfile& operator << (Logfile& output, const T& value)
90  {
91  details::static_failure<T> IF_YOU_HAVE_AN_ERROR_HERE_YOU_USED_THE_STREAM_OPERATOR_WITH_AN_INVALID_TYPE;
92  return output;
93  }
94 
95  template<>
96  inline Logfile& operator << (Logfile& output, const uint8_t& value)
97  { output.write(value); return output; }
98 
99  template<>
100  inline Logfile& operator << (Logfile& output, const uint16_t& value)
101  { output.write(value); return output; }
102 
103  template<>
104  inline Logfile& operator << (Logfile& output, const uint32_t& value)
105  { output.write(value); return output; }
106 
107  template<>
108  inline Logfile& operator << (Logfile& output, const std::string& value)
109  {
110  uint32_t length(value.length());
111  output.write(length);
112  output.m_stream.write(reinterpret_cast<const char*>(value.c_str()), length);
113  return output;
114  }
115 
116  template<>
117  inline Logfile& operator << (Logfile& output, const base::Time& time)
118  {
119  timeval tv = time.toTimeval();
120  output << (uint32_t)tv.tv_sec << (uint32_t)tv.tv_usec;
121  return output;
122  }
123 
124  template<>
125  inline Logfile& operator << (Logfile& output, const BlockHeader& header)
126  {
127  output
128  << header.type
129  << header.padding
130  << header.stream_idx
131  << header.data_size;
132  return output;
133  }
134 
135  template<>
136  inline Logfile& operator << (Logfile& output, const SampleHeader& header)
137  {
138  output
139  << header.realtime
140  << header.timestamp
141  << header.data_size
142  << header.compressed;
143  return output;
144  }
145 
146  template<>
147  inline Logfile& operator << (Logfile& output, const BlockType& type)
148  { return output << static_cast<uint8_t>(type); }
149 
150  template<>
151  inline Logfile& operator << (Logfile& output, const StreamType& type)
152  { return output << static_cast<uint8_t>(type); }
153 
154  template<>
155  inline Logfile& operator << (Logfile& output, const CommandType& type)
156  { return output << static_cast<uint8_t>(type); }
157 
158 
163  {
164  std::string const m_name;
165  std::string const m_type_name;
166  std::string const m_cxx_type_name;
167  std::string const m_type_def;
168  std::vector<logger::StreamMetadata> m_metadata;
169  int const m_stream_idx;
170  size_t const m_type_size;
171  base::Time m_sampling;
172  base::Time m_last;
173 
174  Logfile m_file;
175 
176  public:
186  StreamLogger(std::string const& name, std::string const& type_name, std::string const& cxx_type_name, std::vector<logger::StreamMetadata> const& metadata, Logfile& file);
187 
199  StreamLogger(std::string const& name, std::string const& type_name, std::string const& cxx_type_name, Typelib::Registry const& type_def, std::vector<logger::StreamMetadata> const& metadata, Logfile& file);
200 
202  void registerStream();
203 
207  void setSampling(base::Time const& period);
208 
209  bool writeSampleHeader(const base::Time& timestamp, size_t size);
210 
211  std::ostream& getStream();
212  };
213 }
214 
215 #endif
216 
uint8_t type
Definition: Logging.hh:122
Logfile & operator<<(Logfile &output, const CommandType &type)
Definition: Logfile.hpp:155
base::Time realtime
Definition: Logging.hh:131
Definition: Logfile.cpp:63
BlockType
Definition: Logging.hh:138
uint16_t stream_idx
Definition: Logging.hh:124
void writePrologue(std::ostream &stream)
Definition: Logfile.cpp:49
Definition: Logging.hh:129
Definition: Logfile.hpp:162
Definition: Logging.hh:120
Definition: Logfile.hpp:43
base::Time timestamp
Definition: Logging.hh:132
uint8_t padding
Definition: Logging.hh:123
CommandType
Definition: Logging.hh:151
uint8_t compressed
Definition: Logging.hh:134
uint32_t data_size
Definition: Logging.hh:133
uint32_t data_size
Definition: Logging.hh:125
StreamType
Definition: Logging.hh:145
Definition: Logfile.hpp:82
Definition: Logfile.hpp:48