Skyward boardcore
Loading...
Searching...
No Matches
LogSink.h
Go to the documentation of this file.
1/* Copyright (c) 2021 Skyward Experimental Rocketry
2 * Authors: Luca Erbetta, Luca Conterio
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 <logger/Logger.h>
26
27#include "PrintLoggerData.h"
28
29namespace Boardcore
30{
31
33{
34public:
36 LogSink(const LogSink&) = delete;
37 LogSink& operator=(const LogSink&) = delete;
38
39 virtual ~LogSink() {}
40
41 void log(const LogRecord& record);
42
43 void enable() { enabled = true; }
44
45 void disable() { enabled = false; }
46
47 bool isEnabled() { return enabled; }
48
49 void setLevel(uint8_t level) { minimumLevel = level; }
50
51 int getLevel() { return minimumLevel; }
52
53 void setFormatString(const std::string& format) { this->format = format; }
54
55protected:
56 virtual void logImpl(const std::string& l) = 0;
57
58private:
59 bool enabled = true; // enabled by the default when created
60 uint8_t minimumLevel = LOGL_NOTSET;
61 std::string format = "{ts} {file}:{line} {fun} {lvl} [{name}] {msg}\n";
62};
63
67class FileLogSink : public LogSink
68{
69public:
71
72 explicit FileLogSink(FILE* f) : f(f) {}
73
74 void setFile(FILE* f_) { f = f_; }
75
76protected:
77 void logImpl(const std::string& l) override;
78
79 FILE* f;
80 miosix::FastMutex mutex;
81};
82
89{
90public:
91 FileLogSinkBuffered() : logger(Logger::getInstance()) {}
92
93protected:
94 void logImpl(const std::string& l) override;
95
96private:
97 Logger& logger;
98};
99
100} // namespace Boardcore
void logImpl(const std::string &l) override
void setFile(FILE *f_)
Definition LogSink.h:74
void logImpl(const std::string &l) override
miosix::FastMutex mutex
Definition LogSink.h:80
void log(const LogRecord &record)
void setLevel(uint8_t level)
Definition LogSink.h:49
virtual ~LogSink()
Definition LogSink.h:39
virtual void logImpl(const std::string &l)=0
void setFormatString(const std::string &format)
Definition LogSink.h:53
LogSink(const LogSink &)=delete
LogSink & operator=(const LogSink &)=delete
Buffered logger. Needs to be started before it can be used.
Definition Logger.h:55
This file includes all the types the logdecoder script will decode.