skullc-peripherals/Utility/Inc/utility_seriallogger.hpp
Erki ce4f8eb8f5
All checks were successful
continuous-integration/drone/push Build is passing
New logging interfaces
2021-04-02 00:50:59 +03:00

53 lines
948 B
C++

/*
* utility_seriallogger.hpp
*
* Created on: Apr 1, 2021
* Author: erki
*/
#ifndef UTILITY_INC_UTILITY_SERIALLOGGER_HPP_
#define UTILITY_INC_UTILITY_SERIALLOGGER_HPP_
#include <array>
#include <cstdarg>
#include <cstdio>
#include "utility_ilogger.hpp"
namespace Utility
{
template<typename T, std::size_t N>
class SerialLogger : public ILogger
{
public:
using serial_interface = T;
SerialLogger() = delete;
explicit SerialLogger(const serial_interface& serial)
: _serial(serial)
{ }
void log(const char* format, ...) override
{
std::va_list args;
va_start(args, format);
const std::int32_t len = vsnprintf(_buffer.data(), _buffer.size(), format, args);
if (len > 0)
_serial.Transmit(reinterpret_cast<std::uint8_t*>(_buffer.data()), len);
va_end(args);
}
private:
serial_interface _serial;
std::array<char, N> _buffer;
};
}
#endif /* UTILITY_INC_UTILITY_SERIALLOGGER_HPP_ */