PX4 Firmware
PX4 Autopilot Software http://px4.io
log_writer_file.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 <px4_platform_common/defines.h>
37 #include <stdint.h>
38 #include <pthread.h>
39 #include <drivers/drv_hrt.h>
40 #include <perf/perf_counter.h>
41 
42 namespace px4
43 {
44 namespace logger
45 {
46 
47 /**
48  * @enum LogType
49  * Defines different log (file) types
50  */
51 enum class LogType {
52  Full = 0, //!< Normal, full size log
53  Mission, //!< reduced mission log (e.g. for geotagging)
54 
55  Count
56 };
57 
58 const char *log_type_str(LogType type);
59 
60 /**
61  * @class LogWriterFile
62  * Writes logging data to a file
63  */
65 {
66 public:
67  LogWriterFile(size_t buffer_size);
68  ~LogWriterFile();
69 
70  bool init();
71 
72  /**
73  * start the thread
74  * @return 0 on success, error number otherwise (@see pthread_create)
75  */
76  int thread_start();
77 
78  void thread_stop();
79 
80  void start_log(LogType type, const char *filename);
81 
82  void stop_log(LogType type);
83 
84  bool is_started(LogType type) const { return _buffers[(int)type]._should_run; }
85 
86  /** @see LogWriter::write_message() */
87  int write_message(LogType type, void *ptr, size_t size, uint64_t dropout_start = 0);
88 
89  void lock()
90  {
91  pthread_mutex_lock(&_mtx);
92  }
93 
94  void unlock()
95  {
96  pthread_mutex_unlock(&_mtx);
97  }
98 
99  void notify()
100  {
101  pthread_cond_broadcast(&_cv);
102  }
103 
104  size_t get_total_written(LogType type) const
105  {
106  return _buffers[(int)type].total_written();
107  }
108 
109  size_t get_buffer_size(LogType type) const
110  {
111  return _buffers[(int)type].buffer_size();
112  }
113 
114  size_t get_buffer_fill_count(LogType type) const
115  {
116  return _buffers[(int)type].count();
117  }
118 
119  void set_need_reliable_transfer(bool need_reliable)
120  {
121  _need_reliable_transfer = need_reliable;
122  }
123 
125  {
126  return _need_reliable_transfer;
127  }
128 
129  pthread_t thread_id() const { return _thread; }
130 
131 private:
132  static void *run_helper(void *);
133 
134  void run();
135 
136  /**
137  * permanently store the ulog file name for the hardfault crash handler, so that it can
138  * append crash logs to the last ulog file.
139  * @param log_file path to the log file
140  * @return 0 on success, <0 errno otherwise
141  */
142  int hardfault_store_filename(const char *log_file);
143 
144  /**
145  * write w/o waiting/blocking
146  */
147  int write(LogType type, void *ptr, size_t size, uint64_t dropout_start);
148 
149  /* 512 didn't seem to work properly, 4096 should match the FAT cluster size */
150  static constexpr size_t _min_write_chunk = 4096;
151 
153  {
154  public:
155  LogFileBuffer(size_t log_buffer_size, perf_counter_t perf_write, perf_counter_t perf_fsync);
156 
157  ~LogFileBuffer();
158 
159  bool start_log(const char *filename);
160 
161  void close_file();
162 
163  size_t get_read_ptr(void **ptr, bool *is_part);
164 
165  /**
166  * Write to the buffer but assuming there is enough space
167  */
168  inline void write_no_check(void *ptr, size_t size);
169 
170  size_t available() const { return _buffer_size - _count; }
171 
172  int fd() const { return _fd; }
173 
174  inline ssize_t write_to_file(const void *buffer, size_t size, bool call_fsync) const;
175 
176  inline void fsync() const;
177 
178  void mark_read(size_t n) { _count -= n; _total_written += n; }
179 
180  size_t total_written() const { return _total_written; }
181  size_t buffer_size() const { return _buffer_size; }
182  size_t count() const { return _count; }
183 
184  bool _should_run = false;
185 
186  private:
187  const size_t _buffer_size;
188  int _fd = -1;
189  uint8_t *_buffer = nullptr;
190  size_t _head = 0; ///< next position to write to
191  size_t _count = 0; ///< number of bytes in _buffer to be written
192  size_t _total_written = 0;
195  };
196 
198 
199  bool _exit_thread = false;
200  bool _need_reliable_transfer = false;
201  pthread_mutex_t _mtx;
202  pthread_cond_t _cv;
203  pthread_t _thread = 0;
204 };
205 
206 }
207 }
size_t get_total_written(LogType type) const
static unsigned long _count
LogType
Defines different log (file) types.
reduced mission log (e.g. for geotagging)
High-resolution timer with callouts and timekeeping.
Header common to all counters.
void init()
Activates/configures the hardware registers.
Writes logging data to a file.
size_t get_buffer_size(LogType type) const
Normal, full size log.
size_t get_buffer_fill_count(LogType type) const
bool is_started(LogType type) const
static void write(bootloader_app_shared_t *pshared)
void set_need_reliable_transfer(bool need_reliable)
Definition: bst.cpp:62
pthread_t thread_id() const
const char * log_type_str(LogType type)
Performance measuring tools.