default_logger.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD-3 License)
3  *
4  * Copyright (c) 2020 Jacob Willis.
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 met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
38 #ifndef MAVROSFLIGHT_DEFAULT_LOGGER_H
39 #define MAVROSFLIGHT_DEFAULT_LOGGER_H
40 
41 #include <cstdio>
42 #include <sstream>
43 
45 
46 namespace mavrosflight
47 {
53 class DefaultLogger : public LoggerInterface<DefaultLogger>
54 {
55 public:
56  template <typename... Args>
57  inline void debug(const char* format, const Args&... args)
58  {
59  _log(stdout, "DEBUG", format, args...);
60  }
61  template <typename... Args>
62  inline void debug_throttle(float period, const char* format, const Args&... args)
63  {
64  debug(format, args...);
65  }
66 
67  template <typename... Args>
68  inline void info(const char* format, const Args&... args)
69  {
70  _log(stdout, "INFO", format, args...);
71  }
72  template <typename... Args>
73  inline void info_throttle(float period, const char* format, const Args&... args)
74  {
75  info(format, args...);
76  }
77 
78  template <typename... Args>
79  inline void warn(const char* format, const Args&... args)
80  {
81  _log(stderr, "WARN", format, args...);
82  }
83  template <typename... Args>
84  inline void warn_throttle(float period, const char* format, const Args&... args)
85  {
86  warn(format, args...);
87  }
88 
89  template <typename... Args>
90  inline void error(const char* format, const Args&... args)
91  {
92  _log(stderr, "ERROR", format, args...);
93  }
94  template <typename... Args>
95  inline void error_throttle(float period, const char* format, const Args&... args)
96  {
97  error(format, args...);
98  }
99 
100  template <typename... Args>
101  inline void fatal(const char* format, const Args&... args)
102  {
103  _log(stderr, "FATAL", format, args...);
104  }
105  template <typename... Args>
106  inline void fatal_throttle(float period, const char* format, const Args&... args)
107  {
108  fatal(format, args...);
109  }
110 
111 private:
112  template <typename... Args>
113  inline void _log(FILE* fs, const char* name, const char* format, const Args&... args)
114  {
115  std::stringstream ss;
116  ss << "[mavrosflight][" << name << "]: " << format << std::endl;
117 
118 #pragma GCC diagnostic push
119 #pragma GCC diagnostic ignored "-Wformat-security"
120  fprintf(fs, ss.str().c_str(), args...);
121 #pragma GCC diagnostic pop
122  }
123 };
124 
125 } // namespace mavrosflight
126 #endif // MAVROSFLIGHT_DEFAULT_LOGGER_H
void error(const char *format, const Args &...args)
void debug(const char *format, const Args &...args)
void info_throttle(float period, const char *format, const Args &...args)
Default logger that outputs to stdout and stderr. Throttling is ignored to reduce timing complexity...
void _log(FILE *fs, const char *name, const char *format, const Args &...args)
void info(const char *format, const Args &...args)
Abstract base class for message handler.
void warn(const char *format, const Args &...args)
void debug_throttle(float period, const char *format, const Args &...args)
void fatal(const char *format, const Args &...args)
void fatal_throttle(float period, const char *format, const Args &...args)
void error_throttle(float period, const char *format, const Args &...args)
void warn_throttle(float period, const char *format, const Args &...args)


rosflight
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:09:25