timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
printer.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#pragma once
26
29
30#include <iostream>
31#include <sstream>
32#include <string>
33
34namespace tim
35{
36namespace component
37{
38/// \struct tim::component::printer
39/// \brief A diagnostic component when prints messages via start(...) and stores messages
40/// via store(...). The stored messages are returned via the get() member function. If
41/// bundled alongside the timestamp component, the timestamp will be added to the stored
42/// message
43struct printer
46{
47 using value_type = void;
48
49 static std::string label();
50 static std::string description();
51 static std::string get_label();
53
54 printer() = default;
55 ~printer() = default;
56 printer(printer&&) = default;
57 printer(const printer&);
58
59 printer& operator=(printer&&) = default;
60 printer& operator =(const printer&);
61
62 std::string get() const;
63 void set_prefix(const char*);
64 bool assemble(timestamp*);
65
66 template <typename... Args>
67 auto start(Args&&... args)
68 -> decltype(TIMEMORY_FOLD_EXPRESSION(std::declval<std::stringstream>() << args),
69 void());
70
71 template <typename... Args>
72 auto store(Args&&... args)
73 -> decltype(TIMEMORY_FOLD_EXPRESSION(std::declval<std::stringstream>() << args),
74 void());
75
76private:
77 const char* m_prefix = nullptr;
78 timestamp* m_ts = nullptr;
79 std::stringstream m_stream{};
80};
81} // namespace component
82} // namespace tim
83
84//--------------------------------------------------------------------------------------//
85
86#include "timemory/components/timestamp/timestamp.hpp" // for timestamp::as_string
87#include "timemory/variadic/macros.hpp" // for TIMEMORY_JOIN
88
89template <typename... Args>
90auto
92 -> decltype(TIMEMORY_FOLD_EXPRESSION(std::declval<std::stringstream>() << args),
93 void())
94{
95 // only print message if arguments provided
96 if(sizeof...(Args) == 0)
97 return;
98
99 std::cerr << std::flush;
100 if(m_prefix)
101 std::cerr << "[" << m_prefix << "]";
102 std::cerr << "> " << TIMEMORY_JOIN("", std::forward<Args>(args)...) << '\n';
103}
104
105template <typename... Args>
106auto
108 -> decltype(TIMEMORY_FOLD_EXPRESSION(std::declval<std::stringstream>() << args),
109 void())
110{
111 // only store message if arguments provided
112 if(sizeof...(Args) == 0)
113 return;
114
115 std::string _tp{};
116 if(m_prefix)
117 _tp += TIMEMORY_JOIN("", '[', m_prefix, ']');
118 if(m_ts)
119 _tp += "[" + timestamp::as_string(m_ts->get()) + "]";
120 m_stream << _tp << "> " << TIMEMORY_JOIN("", std::forward<Args>(args)...) << '\n';
121}
122
123#if defined(TIMEMORY_COMPONENT_PRINTER_HEADER_ONLY_MODE) && \
124 TIMEMORY_COMPONENT_PRINTER_HEADER_ONLY_MODE > 0
126#endif
std::string as_string()
Definition: type_traits.hpp:62
Definition: kokkosp.cpp:39
tim::mpl::apply< std::string > string
Definition: macros.hpp:53
A very lightweight base which provides no storage.
Definition: declaration.hpp:65
A diagnostic component when prints messages via start(...) and stores messages via store(....
Definition: printer.hpp:46
printer(printer &&)=default
auto store(Args &&... args) -> decltype(TIMEMORY_FOLD_EXPRESSION(std::declval< std::stringstream >()<< args), void())
Definition: printer.hpp:107
static std::string get_description()
Definition: printer.cpp:64
bool assemble(timestamp *)
Definition: printer.cpp:106
printer & operator=(printer &&)=default
static std::string get_label()
Definition: printer.cpp:57
std::string get() const
Definition: printer.cpp:92
static std::string label()
Definition: printer.cpp:43
static std::string description()
Definition: printer.cpp:50
auto start(Args &&... args) -> decltype(TIMEMORY_FOLD_EXPRESSION(std::declval< std::stringstream >()<< args), void())
Definition: printer.hpp:91
void set_prefix(const char *)
Definition: printer.cpp:99
this component stores the timestamp of when a bundle was started and is specialized such that the "ti...
Definition: timestamp.hpp:50
#define TIMEMORY_FOLD_EXPRESSION(...)
Definition: types.hpp:56
#define TIMEMORY_JOIN(delim,...)
Definition: macros.hpp:90