TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_trace.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-25 16:20:43 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
19
27#pragma once
28
29#include <ostream>
30#include <string>
31#include <mutex>
32
33#include "tec/tec_def.hpp" // IWYU pragma: keep
34#include "tec/tec_print.hpp"
35#include "tec/tec_utils.hpp"
36
37
38// Default time resolution for tracing.
39#if !defined(_TEC_TRACE_RES)
40#define _TEC_TRACE_RES MilliSec
41#endif
42
43
44namespace tec {
45
46namespace details {
47
59 static std::mutex& mtx() {
60 static std::mutex __mtx_trace;
61 return __mtx_trace;
62 }
63};
64
65} // namespace details
66
76template <typename Duration = _TEC_TRACE_RES>
77class Tracer {
78private:
79 std::string name_;
80 std::ostream* out_;
81
82public:
83 using Lock = std::lock_guard<std::mutex>;
84
91 Tracer(const char* name, std::ostream* out)
92 : name_{name}
93 , out_{out}
94 {}
95
103 auto tp = now<Duration>();
104 *out_ << "[" << tp.count() << "] - " << name_ << " exited.\n";
105 }
106
112 void enter() {
114 auto tp = now<Duration>();
115 *out_ << "[" << tp.count() << "] + " << name_ << " entered.\n";
116 }
117
125 template <typename T>
126 void trace(const T& arg) {
128 auto tp = now<Duration>().count();
129 *out_ << "[" << tp << "] " << name_ << ": ";
130 println<>(out_, arg);
131 }
132
143 template <typename T, typename... Targs>
144 void trace(const char* fmt, const T& value, Targs&&... Args) {
146 auto tp = now<Duration>().count();
147 *out_ << "[" << tp << "] " << name_ << ": ";
148 println<>(out_, fmt, value, Args...);
149 }
150}; // class Tracer
151
152} // namespace tec
153
171#if defined(_DEBUG) || defined(DEBUG)
172 #define _TEC_DEBUG 1
173#endif
174
175#if defined(_TEC_TRACE_ON)
176
184#if defined(__TEC_WINDOWS__)
185 #define TEC_ENTER(name) Tracer<> tracer__(name, &std::cout); tracer__.enter()
186#else
187 #define TEC_ENTER(name) tec::Tracer<> tracer__(name, &std::cout); tracer__.enter()
188#endif
189
198#define TEC_TRACE(...) tracer__.trace(__VA_ARGS__)
199
200#else
201// _TEC_TRACE_ON undefined.
202
211#define TEC_ENTER(name)
212
222#define TEC_TRACE(...)
223
224#endif
225
A thread-safe tracer for logging entry, exit, and custom messages.
Definition tec_trace.hpp:77
Tracer(const char *name, std::ostream *out)
Constructs a tracer with a name and output stream.
Definition tec_trace.hpp:91
void enter()
Logs an entry message for the tracer.
Definition tec_trace.hpp:112
void trace(const char *fmt, const T &value, Targs &&... Args)
Logs variadic arguments as a formatted trace message.
Definition tec_trace.hpp:144
void trace(const T &arg)
Logs a single argument as a trace message.
Definition tec_trace.hpp:126
~Tracer()
Destructor that logs the exit of the tracer.
Definition tec_trace.hpp:101
std::lock_guard< std::mutex > Lock
Alias for lock guard used for thread safety.
Definition tec_trace.hpp:83
Provides a global mutex for synchronizing trace output.
Definition tec_trace.hpp:53
static std::mutex & mtx()
Retrieves the global trace mutex.
Definition tec_trace.hpp:59
Common definitions and utilities for the tec namespace.
auto name(const Message &msg) noexcept
Retrieves the type name of a message's content for registering the corresponding message handler.
Definition tec_message.hpp:84
Provides variadic print and format utilities for the tec namespace. Implemented for compatibility wit...
Provides time-related utilities and system information functions for the tec namespace.