Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
sink.hpp
Go to the documentation of this file.
1/**
2 * @file sink.hpp
3 * @brief The logger's two contracts: an output sink and a timestamp provider
4 *
5 * The logger, the tagged lines and the fault handlers write through a sink
6 * and never name a device, so this directory includes nothing from hw/,
7 * usb/ or system/. A device's sink lives with the device:
8 *
9 * - sbl::uart::LogOutput, sbl::uart::FaultOutput <sbl/hw/hal/uart/log_output.hpp>
10 * - sbl::usb::CdcOutput <sbl/usb/cdc.hpp>
11 * - sbl::system::timer::MillisTimestamp <sbl/system/timer.hpp>
12 *
13 * <sbl/log.hpp> picks a build's defaults among them.
14 *
15 * Contracts:
16 * - sink: `static void write(const char* str)`, a NUL-terminated string
17 * - timestamp: `static uint32_t now()`, milliseconds
18 */
19
20#ifndef SBL_COMMON_LOG_SINK_HPP_
21#define SBL_COMMON_LOG_SINK_HPP_
22
23#include <cstdint>
24#include <type_traits>
25#include <utility>
26
27namespace sbl::log {
28
29/** A sink that discards what it is given. */
30struct NullOutput {
31 static void write(const char*) {}
32};
33
34/** A timestamp provider that returns 0. */
36 static uint32_t now() { return 0; }
37};
38
39namespace detail {
40
41template<typename T, typename = void>
42struct is_sink : std::false_type {};
43template<typename T>
44struct is_sink<T, std::void_t<decltype(T::write(std::declval<const char*>()))>> : std::true_type {};
45
46template<typename T, typename = void>
47struct is_timestamp : std::false_type {};
48template<typename T>
49struct is_timestamp<T, std::void_t<decltype(T::now())>> : std::is_convertible<decltype(T::now()), uint32_t> {};
50
51} // namespace detail
52
53/** True for a type that meets the sink contract. */
54template<typename T>
55inline constexpr bool is_sink_v = detail::is_sink<T>::value;
56
57/** True for a type that meets the timestamp contract. */
58template<typename T>
59inline constexpr bool is_timestamp_v = detail::is_timestamp<T>::value;
60
61} // namespace sbl::log
62
63#endif // SBL_COMMON_LOG_SINK_HPP_
Logging.
Definition banner.hpp:53
constexpr bool is_sink_v
Definition sink.hpp:55
constexpr bool is_timestamp_v
Definition sink.hpp:59