PX4 Firmware
PX4 Autopilot Software http://px4.io
log_writer.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2016 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 #pragma once
35 
36 #include "log_writer_file.h"
37 #include "log_writer_mavlink.h"
38 
39 namespace px4
40 {
41 namespace logger
42 {
43 
44 /**
45  * @class LogWriter
46  * Manages starting, stopping & writing of logged data using the configured backend.
47  */
48 class LogWriter
49 {
50 public:
51 
52  /** bitfield to specify a backend */
53  typedef uint8_t Backend;
54  static constexpr Backend BackendFile = 1 << 0;
55  static constexpr Backend BackendMavlink = 1 << 1;
56  static constexpr Backend BackendAll = BackendFile | BackendMavlink;
57 
58  LogWriter(Backend configured_backend, size_t file_buffer_size);
59  ~LogWriter();
60 
61  bool init();
62 
63  Backend backend() const { return _backend; }
64 
65  /** stop all running threads and wait for them to exit */
66  void thread_stop();
67 
68  void start_log_file(LogType type, const char *filename);
69 
70  void stop_log_file(LogType type);
71 
72  void start_log_mavlink();
73 
74  void stop_log_mavlink();
75 
76  /**
77  * whether logging is currently active or not (any of the selected backends).
78  */
79  bool is_started(LogType type) const;
80 
81  /**
82  * whether logging is currently active or not for a specific backend.
83  */
84  bool is_started(LogType type, Backend query_backend) const;
85 
86  /**
87  * Write a single ulog message (including header). The caller must call lock() before calling this.
88  * @param dropout_start timestamp when lastest dropout occured. 0 if no dropout at the moment.
89  * @return 0 on success (or if no logging started),
90  * -1 if not enough space in the buffer left (file backend), -2 mavlink backend failed
91  * add type -> pass through, but not to mavlink if mission log
92  */
93  int write_message(LogType type, void *ptr, size_t size, uint64_t dropout_start = 0);
94 
95  /**
96  * Select a backend, so that future calls to write_message() only write to the selected
97  * sel_backend, until unselect_write_backend() is called.
98  * @param backend
99  */
100  void select_write_backend(Backend sel_backend);
102 
103  /* file logging methods */
104 
105  void lock()
106  {
108  }
109 
110  void unlock()
111  {
113  }
114 
115  void notify()
116  {
118  }
119 
120  size_t get_total_written_file(LogType type) const
121  {
122  if (_log_writer_file) { return _log_writer_file->get_total_written(type); }
123 
124  return 0;
125  }
126 
127  size_t get_buffer_size_file(LogType type) const
128  {
129  if (_log_writer_file) { return _log_writer_file->get_buffer_size(type); }
130 
131  return 0;
132  }
133 
135  {
137 
138  return 0;
139  }
140 
141  pthread_t thread_id_file() const
142  {
143  if (_log_writer_file) { return _log_writer_file->thread_id(); }
144 
145  return (pthread_t)0;
146  }
147 
148 
149  /**
150  * Indicate to the underlying backend whether future write_message() calls need a reliable
151  * transfer. Needed for header integrity.
152  */
153  void set_need_reliable_transfer(bool need_reliable)
154  {
156 
158  }
159 
161  {
163 
165 
166  return false;
167  }
168 
169 private:
170 
173 
175  nullptr; ///< pointer that is used for writing, to temporarily select write backends
177 
178  const Backend _backend;
179 };
180 
181 
182 }
183 }
Backend backend() const
Definition: log_writer.h:63
size_t get_total_written(LogType type) const
uint8_t Backend
bitfield to specify a backend
Definition: log_writer.h:53
Manages starting, stopping & writing of logged data using the configured backend. ...
Definition: log_writer.h:48
void thread_stop()
stop all running threads and wait for them to exit
Definition: log_writer.cpp:154
LogType
Defines different log (file) types.
pthread_t thread_id_file() const
Definition: log_writer.h:141
static constexpr Backend BackendFile
Definition: log_writer.h:54
LogWriterMavlink * _log_writer_mavlink_for_write
Definition: log_writer.h:176
int write_message(LogType type, void *ptr, size_t size, uint64_t dropout_start=0)
Write a single ulog message (including header).
Definition: log_writer.cpp:161
bool need_reliable_transfer() const
Definition: log_writer.h:160
void start_log_file(LogType type, const char *filename)
Definition: log_writer.cpp:126
bool is_started(LogType type) const
whether logging is currently active or not (any of the selected backends).
Definition: log_writer.cpp:98
void stop_log_file(LogType type)
Definition: log_writer.cpp:133
size_t get_total_written_file(LogType type) const
Definition: log_writer.h:120
LogWriterFile * _log_writer_file_for_write
pointer that is used for writing, to temporarily select write backends
Definition: log_writer.h:174
Writes logging data to a file.
size_t get_buffer_size(LogType type) const
size_t get_buffer_fill_count(LogType type) const
LogWriter(Backend configured_backend, size_t file_buffer_size)
Definition: log_writer.cpp:41
static constexpr Backend BackendAll
Definition: log_writer.h:56
size_t get_buffer_size_file(LogType type) const
Definition: log_writer.h:127
LogWriterMavlink * _log_writer_mavlink
Definition: log_writer.h:172
LogWriterFile * _log_writer_file
Definition: log_writer.h:171
size_t get_buffer_fill_count_file(LogType type) const
Definition: log_writer.h:134
void select_write_backend(Backend sel_backend)
Select a backend, so that future calls to write_message() only write to the selected sel_backend...
Definition: log_writer.cpp:181
void set_need_reliable_transfer(bool need_reliable)
Indicate to the underlying backend whether future write_message() calls need a reliable transfer...
Definition: log_writer.h:153
void set_need_reliable_transfer(bool need_reliable)
Definition: bst.cpp:62
const Backend _backend
Definition: log_writer.h:178
pthread_t thread_id() const
static constexpr Backend BackendMavlink
Definition: log_writer.h:55