Galois
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Tracer.h
Go to the documentation of this file.
1 /*
2  * This file belongs to the Galois project, a C++ library for exploiting
3  * parallelism. The code is being released under the terms of the 3-Clause BSD
4  * License (a copy is located in LICENSE.txt at the top-level directory).
5  *
6  * Copyright (C) 2018, The University of Texas at Austin. All rights reserved.
7  * UNIVERSITY EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES CONCERNING THIS
8  * SOFTWARE AND DOCUMENTATION, INCLUDING ANY WARRANTIES OF MERCHANTABILITY,
9  * FITNESS FOR ANY PARTICULAR PURPOSE, NON-INFRINGEMENT AND WARRANTIES OF
10  * PERFORMANCE, AND ANY WARRANTY THAT MIGHT OTHERWISE ARISE FROM COURSE OF
11  * DEALING OR USAGE OF TRADE. NO WARRANTY IS EITHER EXPRESS OR IMPLIED WITH
12  * RESPECT TO THE USE OF THE SOFTWARE OR DOCUMENTATION. Under no circumstances
13  * shall University be liable for incidental, special, indirect, direct or
14  * consequential damages or loss of profits, interruption of business, or
15  * related expenses which may arise from use of Software or Documentation,
16  * including but not limited to those resulting from defects in Software and/or
17  * Documentation, or loss or inaccuracy of data of any kind.
18  */
19 
25 #ifndef GALOIS_RUNTIME_TRACER_H
26 #define GALOIS_RUNTIME_TRACER_H
27 
28 #include <functional>
29 #include <sstream>
30 
31 #include "galois/config.h"
34 
35 namespace galois {
36 namespace runtime {
37 
38 namespace internal {
39 
43 static inline void traceImpl(std::ostringstream& os) { os << "\n"; }
44 
48 template <typename T, typename... Args>
49 static inline void traceImpl(std::ostringstream& os, T&&, Args&&... args) {
50  // os << value << " ";
51  traceImpl(os, std::forward<Args>(args)...);
52 }
53 
57 static inline void traceFormatImpl(std::ostringstream& os, const char* format) {
58  os << format;
59 }
60 
64 template <typename T, typename... Args>
65 static inline void traceFormatImpl(std::ostringstream& os, const char* format,
66  T&& value, Args&&... args) {
67  for (; *format != '\0'; format++) {
68  if (*format == '%') {
69  os << value;
70  traceFormatImpl(os, format + 1, std::forward<Args>(args)...);
71  return;
72  }
73  os << *format;
74  }
75 }
76 
80 template <typename T>
81 class vecPrinter {
83 
84 public:
85  vecPrinter(const galois::PODResizeableArray<T>& _v) : v(_v) {}
86  void print(std::ostream& os) const {
87  os << "< " << v.size() << " : ";
88  for (auto& i : v)
89  os << " " << (int)i;
90  os << ">";
91  }
92 };
93 
97 template <typename T>
98 std::ostream& operator<<(std::ostream& os, const vecPrinter<T>& vp) {
99  vp.print(os);
100  return os;
101 }
102 
106 void printTrace(std::ostringstream&);
107 
111 void print_output_impl(std::ostringstream&);
112 
113 extern bool doTrace;
114 extern bool initTrace;
115 
116 } // namespace internal
117 
122 template <typename T>
123 internal::vecPrinter<T> printVec(const galois::PODResizeableArray<T>& v) {
124  return internal::vecPrinter<T>(v);
125 };
126 
130 #ifdef NDEBUG
131 template <typename... Args>
132 static inline void trace(Args&&...) {}
133 #else
134 template <typename... Args>
135 static inline void trace(Args&&... args) {
136  if (!internal::initTrace) {
137  internal::doTrace = substrate::EnvCheck("GALOIS_DEBUG_TRACE");
138  internal::initTrace = true;
139  }
140  if (internal::doTrace) {
141  std::ostringstream os;
142  internal::traceImpl(os, std::forward<Args>(args)...);
143  internal::printTrace(os);
144  }
145 }
146 #endif
147 
154 template <typename... Args>
155 static inline void printOutput(const char* format, Args&&... args) {
156  std::ostringstream os;
157  internal::traceFormatImpl(os, format, std::forward<Args>(args)...);
158  internal::print_output_impl(os);
159 }
160 } // namespace runtime
161 } // namespace galois
162 
163 #endif
bool EnvCheck(const char *varName)
Return true if the Enviroment variable is set.
Definition: EnvCheck.cpp:24
internal::vecPrinter< T > printVec(const galois::PODResizeableArray< T > &v)
Given a vector, returns a vector printer object that is able to print the vector out onto an output s...
Definition: Tracer.h:123
This is a container that encapsulates a resizeable array of plain-old-datatype (POD) elements...
Definition: PODResizeableArray.h:40