tin  1.5.9
logging.h
Go to the documentation of this file.
1 // Copyright (c) 2026 Tinverse LLC. All rights reserved.
2 // SPDX-License-Identifier: LicenseRef-Tinverse-Commercial
3 
10 
11 #pragma once
12 
13 #include <cstdint>
14 
15 #include "tsm/core_algorithms.h"
16 #include "tsm/ticks.h"
17 
18 namespace tsm::log {
19 
25 enum class Kind : std::uint8_t {
26  Event,
27  Transition,
29  Action,
30  Entry,
31  Exit,
32  Unhandled,
33 };
34 
35 enum class Level : std::uint8_t {
36  Debug,
37  Info,
38  Warn,
39  Error,
40 };
41 
42 struct Record {
45  std::uint16_t machine{ tsm::core::npos };
46  std::uint16_t from{ tsm::core::npos };
47  std::uint16_t event{ tsm::core::npos };
48  std::uint16_t to{ tsm::core::npos };
49  std::uint16_t transition{ tsm::core::npos };
50 };
51 
52 } // namespace tsm::log
53 
54 namespace tsm::policy {
55 
60 struct NoLogging {
61  static constexpr bool enabled = false;
62 };
63 
69 template<typename Logger, tsm::log::Level Threshold = tsm::log::Level::Error>
70 struct Logging {
71  static constexpr bool enabled = true;
72  static constexpr auto threshold = Threshold;
73 
74  constexpr Logging() = default;
75  constexpr explicit Logging(Logger logger)
76  : logger_(logger) {}
77 
78  constexpr void debug(tsm::log::Kind kind, tsm::log::Record const& record) {
79  if constexpr (enabled_for<tsm::log::Level::Debug>()) {
80  logger_.debug(kind, record);
81  }
82  }
83 
84  constexpr void info(tsm::log::Kind kind, tsm::log::Record const& record) {
85  if constexpr (enabled_for<tsm::log::Level::Info>()) {
86  logger_.info(kind, record);
87  }
88  }
89 
90  constexpr void warn(tsm::log::Kind kind, tsm::log::Record const& record) {
91  if constexpr (enabled_for<tsm::log::Level::Warn>()) {
92  logger_.warn(kind, record);
93  }
94  }
95 
96  constexpr void error(tsm::log::Kind kind, tsm::log::Record const& record) {
97  if constexpr (enabled_for<tsm::log::Level::Error>()) {
98  logger_.error(kind, record);
99  }
100  }
101 
102  private:
103  template<tsm::log::Level Level>
104  static consteval bool enabled_for() {
105  return static_cast<std::uint8_t>(Level) >=
106  static_cast<std::uint8_t>(Threshold);
107  }
108 
109  [[no_unique_address]] Logger logger_{};
110 };
111 
112 } // namespace tsm::policy
constexpr hierarchy algorithms used by the HSM runtime.
constexpr std::uint16_t npos
Definition: core_algorithms.h:31
Definition: logging.h:18
Kind
Definition: logging.h:25
Level
Definition: logging.h:35
Definition: logging.h:54
std::chrono::duration< tick_rep, tick_period > tick_duration
Chrono duration type used for semantic scheduler ticks.
Definition: ticks.h:50
Definition: logging.h:42
tsm::tick_duration tick
Definition: logging.h:43
std::uint16_t from
Definition: logging.h:46
std::uint16_t machine
Definition: logging.h:45
Kind kind
Definition: logging.h:44
std::uint16_t to
Definition: logging.h:48
std::uint16_t transition
Definition: logging.h:49
Definition: logging.h:70
static constexpr auto threshold
Definition: logging.h:72
constexpr Logging()=default
constexpr void error(tsm::log::Kind kind, tsm::log::Record const &record)
Definition: logging.h:96
constexpr Logging(Logger logger)
Definition: logging.h:75
static constexpr bool enabled
Definition: logging.h:71
constexpr void warn(tsm::log::Kind kind, tsm::log::Record const &record)
Definition: logging.h:90
constexpr void debug(tsm::log::Kind kind, tsm::log::Record const &record)
Definition: logging.h:78
constexpr void info(tsm::log::Kind kind, tsm::log::Record const &record)
Definition: logging.h:84
Definition: logging.h:60
static constexpr bool enabled
Definition: logging.h:61
Target-neutral tick value type.