PX4 Firmware
PX4 Autopilot Software http://px4.io
log_writer.cpp
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 #include "log_writer.h"
35 
36 namespace px4
37 {
38 namespace logger
39 {
40 
41 LogWriter::LogWriter(Backend configured_backend, size_t file_buffer_size)
42  : _backend(configured_backend)
43 {
44  if (configured_backend & BackendFile) {
46 
47  if (!_log_writer_file) {
48  PX4_ERR("LogWriterFile allocation failed");
49  }
50  }
51 
52  if (configured_backend & BackendMavlink) {
54 
55  if (!_log_writer_mavlink) {
56  PX4_ERR("LogWriterMavlink allocation failed");
57  }
58  }
59 }
60 
62 {
63  if (_log_writer_file) {
64  if (!_log_writer_file->init()) {
65  PX4_ERR("alloc failed");
66  return false;
67  }
68 
69  int ret = _log_writer_file->thread_start();
70 
71  if (ret) {
72  PX4_ERR("failed to create writer thread (%i)", ret);
73  return false;
74  }
75  }
76 
77  if (_log_writer_mavlink) {
78  if (!_log_writer_mavlink->init()) {
79  PX4_ERR("mavlink init failed");
80  return false;
81  }
82  }
83 
84  return true;
85 }
86 
88 {
89  if (_log_writer_file) {
90  delete (_log_writer_file);
91  }
92 
93  if (_log_writer_mavlink) {
94  delete (_log_writer_mavlink);
95  }
96 }
97 
99 {
100  bool ret = false;
101 
102  if (_log_writer_file) {
103  ret = _log_writer_file->is_started(type);
104  }
105 
106  if (_log_writer_mavlink && type == LogType::Full) {
107  ret = ret || _log_writer_mavlink->is_started();
108  }
109 
110  return ret;
111 }
112 
113 bool LogWriter::is_started(LogType type, Backend query_backend) const
114 {
115  if (query_backend == BackendFile && _log_writer_file) {
116  return _log_writer_file->is_started(type);
117  }
118 
119  if (query_backend == BackendMavlink && _log_writer_mavlink && type == LogType::Full) {
121  }
122 
123  return false;
124 }
125 
126 void LogWriter::start_log_file(LogType type, const char *filename)
127 {
128  if (_log_writer_file) {
129  _log_writer_file->start_log(type, filename);
130  }
131 }
132 
134 {
135  if (_log_writer_file) {
136  _log_writer_file->stop_log(type);
137  }
138 }
139 
141 {
142  if (_log_writer_mavlink) {
144  }
145 }
146 
148 {
149  if (_log_writer_mavlink) {
151  }
152 }
153 
155 {
156  if (_log_writer_file) {
158  }
159 }
160 
161 int LogWriter::write_message(LogType type, void *ptr, size_t size, uint64_t dropout_start)
162 {
163  int ret_file = 0, ret_mavlink = 0;
164 
166  ret_file = _log_writer_file_for_write->write_message(type, ptr, size, dropout_start);
167  }
168 
170  ret_mavlink = _log_writer_mavlink_for_write->write_message(ptr, size);
171  }
172 
173  // file backend errors takes precedence
174  if (ret_file != 0) {
175  return ret_file;
176  }
177 
178  return ret_mavlink;
179 }
180 
182 {
183  if (sel_backend & BackendFile) {
185 
186  } else {
187  _log_writer_file_for_write = nullptr;
188  }
189 
190  if (sel_backend & BackendMavlink) {
192 
193  } else {
195  }
196 }
197 
198 }
199 }
uint8_t Backend
bitfield to specify a backend
Definition: log_writer.h:53
void start_log(LogType type, const char *filename)
void thread_stop()
stop all running threads and wait for them to exit
Definition: log_writer.cpp:154
LogType
Defines different log (file) types.
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
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
int thread_start()
start the thread
void stop_log_file(LogType type)
Definition: log_writer.cpp:133
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.
Normal, full size log.
LogWriter(Backend configured_backend, size_t file_buffer_size)
Definition: log_writer.cpp:41
int write_message(LogType type, void *ptr, size_t size, uint64_t dropout_start=0)
LogWriterMavlink * _log_writer_mavlink
Definition: log_writer.h:172
bool is_started(LogType type) const
LogWriterFile * _log_writer_file
Definition: log_writer.h:171
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
Definition: bst.cpp:62
static constexpr Backend BackendMavlink
Definition: log_writer.h:55