51 lines
972 B
C++
51 lines
972 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;
|
|
};
|
|
|
|
}// namespace Utility
|
|
|
|
#endif /* UTILITY_INC_UTILITY_SERIALLOGGER_HPP_ */
|