Skip to content

File logger_impl.h

File List > external-docs > libDaisy > src > hid > logger_impl.h

Go to the documentation of this file

Source Code

#pragma once
#ifndef __DSY_LOGGER_IMPL_H
#define __DSY_LOGGER_IMPL_H
#include <unistd.h>
#include <cassert>
#include "hid/usb.h"
#include "sys/system.h"


namespace daisy
{
enum LoggerDestination
{
    LOGGER_NONE,     
    LOGGER_INTERNAL, 
    LOGGER_EXTERNAL, 
    LOGGER_SEMIHOST, 
};

template <LoggerDestination dest>
class LoggerImpl
{
  public:
    static void Init() {}

    static bool Transmit(const void* buffer, size_t bytes) { return true; }
};


template <>
class LoggerImpl<LOGGER_INTERNAL>
{
  public:
    static void Init()
    {
        static_assert(1u == sizeof(usb_handle_), "UsbHandle is not static");
        usb_handle_.Init(UsbHandle::FS_INTERNAL);
    }

    static bool Transmit(const void* buffer, size_t bytes)
    {
        return UsbHandle::Result::OK
               == usb_handle_.TransmitInternal((uint8_t*)buffer, bytes);
    }

  protected:
    static UsbHandle usb_handle_;
};


template <>
class LoggerImpl<LOGGER_EXTERNAL>
{
  public:
    static void Init()
    {
        static_assert(1u == sizeof(usb_handle_), "UsbHandle is not static");
        usb_handle_.Init(UsbHandle::FS_EXTERNAL);
    }

    static bool Transmit(const void* buffer, size_t bytes)
    {
        return UsbHandle::Result::OK
               == usb_handle_.TransmitExternal((uint8_t*)buffer, bytes);
    }

  protected:
    static UsbHandle usb_handle_;
};


template <>
class LoggerImpl<LOGGER_SEMIHOST>
{
  public:
    static void Init() {}

    static bool Transmit(const void* buffer, size_t bytes)
    {
        write(STDOUT_FILENO, buffer, bytes);
        return true;
    }
};


} /* namespace daisy */

#endif //__DSY_LOGGER_IMPL_H