51 lines
957 B
C++
51 lines
957 B
C++
/*
|
|
* utility_seriallogger.hpp
|
|
*
|
|
* Created on: Apr 1, 2021
|
|
* Author: erki
|
|
*/
|
|
|
|
#ifndef SKULLC_UTILITY_SERIALLOGGER_HPP_
|
|
#define SKULLC_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 /* SKULLC_UTILITY_SERIALLOGGER_HPP_ */
|