Distributed Ranges
Loading...
Searching...
No Matches
logger.hpp
1// SPDX-FileCopyrightText: Intel Corporation
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#pragma once
6
7#include <chrono>
8#include <fstream>
9#include <iostream>
10
11#include <vendor/source_location/source_location.hpp>
12
13#include <dr/detail/format_shim.hpp>
14#include <dr/detail/ranges_shim.hpp>
15
16namespace dr {
17
18class timer {
19public:
20 timer() : begin_(std::chrono::high_resolution_clock::now()) {}
21
22 auto elapsed() {
23 auto end = std::chrono::high_resolution_clock::now();
24 return std::chrono::duration<double>(end - begin_).count();
25 }
26
27private:
28 std::chrono::time_point<std::chrono::high_resolution_clock> begin_;
29};
30
31class logger {
32public:
33 enum filters { base, for_each, transpose, mdspan_view, mpi, last };
34
35 logger() { rng::fill(enabled_, true); }
36
37 void set_file(std::ofstream &fout) { fout_ = &fout; }
38
39 void filter(const std::vector<std::string> &names) {
40 if (names.size() == 0) {
41 return;
42 }
43
44 // Disable everything
45 rng::fill(enabled_, false);
46
47 // Enabled selected filters
48 for (const auto &name : names) {
49 std::size_t index = filters::last;
50 for (std::size_t i = 0; i < filter_names_.size(); i++) {
51 if (name == filter_names_[i]) {
52 index = i;
53 }
54 }
55 if (index == filters::last) {
56 std::cerr << "Ignoring unrecognized filter: " << name << "\n";
57 } else {
58 enabled_[index] = true;
59 }
60 }
61 }
62
63#ifdef DR_FORMAT
64
65 template <typename... Args>
66 void debug(const nostd::source_location &location,
67 fmt::format_string<Args...> format, Args &&...args) {
68 if (fout_ && enabled_[filters::base]) {
69 *fout_ << fmt::format(format, std::forward<Args>(args)...) << " <"
70 << location.file_name() << ":" << location.line() << ">\n";
71 fout_->flush();
72 }
73 }
74
75 template <typename... Args>
76 void debug(fmt::format_string<Args...> format, Args &&...args) {
77 debug(filters::base, format, std::forward<Args>(args)...);
78 }
79
80 template <typename... Args>
81 void debug(filters filter, fmt::format_string<Args...> format,
82 Args &&...args) {
83 if (fout_ && enabled_[filter]) {
84 *fout_ << fmt::format(format, std::forward<Args>(args)...);
85 fout_->flush();
86 }
87 }
88
89#else
90
91 template <typename... Args>
92 void debug(const nostd::source_location &location, std::string format,
93 Args &&...args) {}
94
95 template <typename... Args> void debug(std::string format, Args &&...args) {}
96
97 template <typename... Args>
98 void debug(filters filter, std::string format, Args &&...args) {}
99
100#endif
101
102private:
103 std::ofstream *fout_ = nullptr;
104 std::array<bool, filters::last> enabled_;
105 std::array<std::string, filters::last> filter_names_ = {
106 "base", "for_each", "transpose", "mdspan_view", "mpi"};
107};
108
109inline logger drlog;
110
111#define DRLOG(...) \
112 dr::drlog.debug(nostd::source_location::current(), __VA_ARGS__)
113
114} // namespace dr
Definition: index.hpp:34
Definition: logger.hpp:31
Definition: logger.hpp:18
Definition: source_location.hpp:13