mp-coro main
Coroutine support tools
trace.h
Go to the documentation of this file.
1// The MIT License (MIT)
2//
3// Copyright (c) 2021 Mateusz Pusz
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#pragma once
24
25#if !defined(MP_CORO_TRACE_LEVEL) || MP_CORO_TRACE_LEVEL == 0
26
27#define TRACE_FUNC()
28
29#else // !defined(MP_CORO_TRACE_LEVEL) || MP_CORO_TRACE_LEVEL == 0
30
31#include <iostream>
32#include <source_location>
33#include <syncstream>
34
35namespace mp_coro {
36
37namespace detail {
38
39struct location {
40 std::source_location data;
41 friend std::ostream &operator<<(std::ostream &os, const location &loc) {
42 os << loc.data.file_name() << " (" << loc.data.line() << ") `" << loc.data.function_name()
43 << "`";
44 return os;
45 }
46};
47
48} // namespace detail
49
50#if MP_CORO_TRACE_LEVEL == 1
51
52void trace_func(std::source_location loc = std::source_location::current()) {
53 std::osyncstream(std::cout) << "[TRACE]: " << detail::location {loc} << '\n';
54}
55
56#define TRACE_FUNC() ::mp_coro::trace_func()
57
58#elif MP_CORO_TRACE_LEVEL == 2
59
60namespace detail {
61
62struct [[nodiscard]] trace_on_finish {
63 location loc;
64 ~trace_on_finish() { std::osyncstream(std::cout) << "[TRACE]: " << loc << ": FINISH\n"; }
65};
66
67} // namespace detail
68
69detail::trace_on_finish trace_func(std::source_location loc = std::source_location::current()) {
70 std::osyncstream(std::cout) << "[TRACE]: " << detail::location {loc} << ": START\n";
71 return detail::trace_on_finish {loc};
72}
73
74#define TRACE_FUNC() auto _ = ::mp_coro::trace_func()
75
76#endif /* MP_CORO_TRACE_LEVEL == 2 */
77
78} // namespace mp_coro
79
80#endif // !defined(MP_CORO_TRACE_LEVEL) || MP_CORO_TRACE_LEVEL == 0
Definition: async.h:31