Skyward boardcore
Loading...
Searching...
No Matches
PrintLogger.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Author: Luca Erbetta
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#pragma once
24
25#include <ActiveObject.h>
26#include <Singleton.h>
27#include <fmt/format.h>
28#include <logger/Logger.h>
29#include <utils/Constants.h>
31
32#include <memory>
33#include <string>
34#include <vector>
35
36#include "LogSink.h"
37#include "PrintLoggerData.h"
38
39using std::string;
40using std::unique_ptr;
41using std::vector;
42
43using miosix::ConditionVariable;
44using miosix::FastMutex;
45
46namespace Boardcore
47{
48
49#ifndef DEFAULT_STDOUT_LOG_LEVEL
50#define DEFAULT_STDOUT_LOG_LEVEL 0
51#endif
52
53static constexpr unsigned int ASYNC_LOG_BUFFER_SIZE = 100;
54
55class Logging;
56
58{
59public:
60 PrintLogger(Logging& logging, const string& name)
61 : parent(logging), name(name)
62 {
63 }
64
65 PrintLogger getChild(const string& name);
66
67 template <typename... Args>
68 void log(uint8_t level, const string& function, const string& file,
69 int line, string format, Args&&... args)
70 {
71 vlog(level, function, file, line, format,
72 fmt::make_args_checked<Args...>(format, args...));
73 }
74
75 template <typename... Args>
76 void logAsync(uint8_t level, const string& function, const string& file,
77 int line, string format, Args&&... args)
78 {
79 vlogAsync(level, function, file, line, format,
80 fmt::make_args_checked<Args...>(format, args...));
81 }
82
83private:
84 void vlog(uint8_t level, const string& function, const string& file,
85 int line, fmt::string_view format, fmt::format_args args);
86 void vlogAsync(uint8_t level, const string& function, const string& file,
87 int line, fmt::string_view format, fmt::format_args args);
88
89 LogRecord buildLogRecord(uint8_t level, const string& function,
90 const string& file, int line,
91 fmt::string_view format, fmt::format_args args);
92
93 Logging& parent;
94 string name;
95};
96
97class Logging : public Singleton<Logging>
98{
99 friend class Singleton<Logging>;
100 friend class PrintLogger;
101
102public:
103 static PrintLogger getLogger(const string& name)
104 {
105 return PrintLogger(getInstance(), name);
106 }
107
108 static void addLogSink(unique_ptr<LogSink>& sink)
109 {
110 getInstance().sinks.push_back(std::move(sink));
111 }
112
113 static LogSink& getStdOutLogSink() { return *getInstance().sinks.at(0); }
114
115 static void startAsyncLogger() { getInstance().asyncLog.start(); }
116
117private:
118 void log(const LogRecord& record);
119 void logAsync(const LogRecord& record);
120
121 class AsyncLogger : public ActiveObject
122 {
123 public:
124 explicit AsyncLogger(Logging& parent);
125 void log(const LogRecord& record);
126
127 protected:
128 void run() override;
129
130 private:
131 Logging& parent;
132 CircularBuffer<LogRecord, ASYNC_LOG_BUFFER_SIZE> records;
133 FastMutex mutex;
134 ConditionVariable cv;
135 };
136
137 Logging() : asyncLog(*this)
138 {
139 unique_ptr<FileLogSink> serial = std::make_unique<FileLogSink>(stdout);
140 serial->setLevel(DEFAULT_STDOUT_LOG_LEVEL);
141#ifndef DEBUG // do not output to serial if not in DEBUG mode
142 serial->disable();
143#endif
144 sinks.push_back(std::move(serial));
145 }
146
147 AsyncLogger asyncLog;
148 vector<unique_ptr<LogSink>> sinks;
149};
150
151} // namespace Boardcore
152
153#define LOG(logger, level, ...) \
154 logger.log(level, __FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
155
156#define LOG_DEBUG(logger, ...) \
157 LOG(logger, Boardcore::LogLevel::LOGL_DEBUG, __VA_ARGS__)
158
159#define LOG_INFO(logger, ...) \
160 LOG(logger, Boardcore::LogLevel::LOGL_INFO, __VA_ARGS__)
161
162#define LOG_WARN(logger, ...) \
163 LOG(logger, Boardcore::LogLevel::LOGL_WARNING, __VA_ARGS__)
164
165#define LOG_ERR(logger, ...) \
166 LOG(logger, Boardcore::LogLevel::LOGL_ERROR, __VA_ARGS__)
167
168#define LOG_CRIT(logger, ...) \
169 LOG(logger, Boardcore::LogLevel::LOGL_CRITICAL, __VA_ARGS__)
170
171#define LOG_ASYNC(logger, level, ...) \
172 logger.logAsync(level, __FUNCTION__, __FILE__, __LINE__, __VA_ARGS__)
173
174#define LOG_DEBUG_ASYNC(logger, ...) \
175 LOG_ASYNC(logger, Boardcore::LogLevel::LOGL_DEBUG, __VA_ARGS__)
176
177#define LOG_INFO_ASYNC(logger, ...) \
178 LOG_ASYNC(logger, Boardcore::LogLevel::LOGL_INFO, __VA_ARGS__)
179
180#define LOG_WARN_ASYNC(logger, ...) \
181 LOG_ASYNC(logger, Boardcore::LogLevel::LOGL_WARNING, __VA_ARGS__)
182
183#define LOG_ERR_ASYNC(logger, ...) \
184 LOG_ASYNC(logger, Boardcore::LogLevel::LOGL_ERROR, __VA_ARGS__)
185
186#define LOG_CRIT_ASYNC(logger, ...) \
187 LOG_ASYNC(logger, Boardcore::LogLevel::LOGL_CRITICAL, __VA_ARGS__)
#define DEFAULT_STDOUT_LOG_LEVEL
Definition PrintLogger.h:50
static void startAsyncLogger()
friend class PrintLogger
static PrintLogger getLogger(const string &name)
static void addLogSink(unique_ptr< LogSink > &sink)
static LogSink & getStdOutLogSink()
void log(uint8_t level, const string &function, const string &file, int line, string format, Args &&... args)
Definition PrintLogger.h:68
PrintLogger(Logging &logging, const string &name)
Definition PrintLogger.h:60
PrintLogger getChild(const string &name)
void logAsync(uint8_t level, const string &function, const string &file, int line, string format, Args &&... args)
Definition PrintLogger.h:76
static Logging & getInstance()
Definition Singleton.h:52
This file includes all the types the logdecoder script will decode.