microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
tizen_dlog_sink.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Jarosław Pelczar <jarek@jpelczar.com>
2//
3// SPDX-License-Identifier: BSD-2-Clause
4
5#pragma once
6
9#include "../log/detail/tagged_log_sink_base.hpp"
10#include "../log/sink.hpp"
11#include <cstddef>
12#include <string_view>
13
14#if MICROFMT_HAS_DLOG
15#include <dlog.h>
16#else
17#include <cstdarg>
18#include <cstdio>
19
20// Fallback stubs for host-side unit testing / compilation outside the Tizen
21// SDK. Rather than being a silent no-op, this prints to stdout so demos and
22// manual testing still produce visible output on non-Tizen hosts.
23enum log_priority {
24 DLOG_UNKNOWN = 0,
25 DLOG_DEFAULT,
26 DLOG_VERBOSE,
27 DLOG_DEBUG,
28 DLOG_INFO,
29 DLOG_WARN,
30 DLOG_ERROR,
31 DLOG_FATAL,
32 DLOG_SILENT
33};
34RELOCO_BEGIN_UNSAFE_BUFFER_USAGE
35inline int dlog_print(log_priority prio, const char *tag, const char *fmt, ...) {
36 std::fprintf(stdout, "[%d] %s: ", static_cast<int>(prio), tag);
37 va_list args;
38 va_start(args, fmt);
39 const int result = std::vfprintf(stdout, fmt, args);
40 va_end(args);
41 std::fprintf(stdout, "\n");
42 return result;
43}
44RELOCO_END_UNSAFE_BUFFER_USAGE
45#endif
46
47namespace microfmt::log {
48
49template <std::size_t TagCapacity> class tizen_dlog_sink;
50
52template <std::size_t TagCapacity> struct tizen_dlog_sink_tag {};
53
55template <std::size_t TagCapacity = 64> class tizen_dlog_sink : public detail::tagged_log_sink_base<TagCapacity> {
57
58public:
59 using write_fn_t = void (*)(int priority, microfmt::string_view tag, microfmt::string_view message) noexcept;
60
61 explicit tizen_dlog_sink(microfmt::string_view tag = "microfmt", write_fn_t write_fn = write_to_dlog) noexcept
62 : tag_base(tag), write_fn_(write_fn) {}
63
64 [[nodiscard]] log_sink as_sink() & noexcept RELOCO_LIFETIMEBOUND {
66 }
67
68 void log_impl(const log_msg &msg) noexcept {
69 if (msg.lvl == level::off) {
70 return;
71 }
72
73 char scratch[TagCapacity];
74 const auto tag = this->resolve_tag(msg.logger_name, scratch);
75 write_fn_(priority_for(msg.lvl), tag, msg.payload);
76 }
77
78private:
79 static void write_to_dlog(int priority, microfmt::string_view tag, microfmt::string_view message) noexcept {
80 ::dlog_print(static_cast<::log_priority>(priority), tag.data(), "%.*s", static_cast<int>(message.size()),
81 message.data());
82 }
83
84 static int priority_for(level lvl) noexcept {
85 switch (lvl) {
86 case level::trace:
87 case level::debug:
88 return DLOG_DEBUG;
89 case level::info:
90 return DLOG_INFO;
91 case level::warn:
92 return DLOG_WARN;
93 case level::err:
94 return DLOG_ERROR;
95 case level::critical:
96 return DLOG_FATAL;
97 case level::off:
98 return DLOG_SILENT;
99 }
100 return DLOG_SILENT;
101 }
102
103 write_fn_t write_fn_;
104};
105
106// Explicit guides: GCC 11 cannot synthesize the implicit CTAD guide because the
107// constructor's default argument (write_to_dlog) names a member of the
108// still-incomplete class template.
110tizen_dlog_sink(microfmt::string_view) -> tizen_dlog_sink<>;
111tizen_dlog_sink(microfmt::string_view, void (*)(int, microfmt::string_view, microfmt::string_view) noexcept)
113
114template <std::size_t TagCapacity> struct log_sink_traits<tizen_dlog_sink_tag<TagCapacity>> {
116
117 static void log(value_ref<context_type> ctx, const log_msg &msg) noexcept { ctx->log_impl(msg); }
118};
119
120} // namespace microfmt::log
CRTP-free base providing fixed-capacity tag storage and truncating tag resolution,...
Definition tagged_log_sink_base.hpp:36
microfmt::string_view resolve_tag(microfmt::string_view logger_name, char(&scratch)[TagCapacity]) const &noexcept RELOCO_LIFETIMEBOUND
Resolves the tag to use for a record whose originating logger is named logger_name: that name (trunca...
Definition tagged_log_sink_base.hpp:65
Type-erased, two-word handle to a log sink backend, plus the severity threshold below which records a...
Definition sink.hpp:73
Adapter that writes structured records to Tizen DLOG.
Definition tizen_dlog_sink.hpp:55
void(*)(int priority, microfmt::string_view tag, microfmt::string_view message) noexcept write_fn_t
Definition tizen_dlog_sink.hpp:59
log_sink as_sink() &noexcept RELOCO_LIFETIMEBOUND
Definition tizen_dlog_sink.hpp:64
void log_impl(const log_msg &msg) noexcept
Definition tizen_dlog_sink.hpp:68
tizen_dlog_sink(microfmt::string_view tag="microfmt", write_fn_t write_fn=write_to_dlog) noexcept
Definition tizen_dlog_sink.hpp:61
Definition fmt.hpp:17
Definition default_log_sink.template.hpp:51
tizen_dlog_sink() -> tizen_dlog_sink<>
level
Shared log severity levels in ascending order.
Definition log_msg.hpp:22
Structured log record forwarded by a logger to each sink.
Definition log_msg.hpp:69
static void log(value_ref< context_type > ctx, const log_msg &msg) noexcept
Definition tizen_dlog_sink.hpp:117
Static customization point describing a log sink backend.
Definition sink.hpp:46
Tag selecting tizen_dlog_sink<TagCapacity> as a log_sink backend.
Definition tizen_dlog_sink.hpp:52