MatchLib
nvhls_trace.h
1 /*
2  * Copyright (c) 2016-2019, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /*
17  * Copyright (c) 2015 Nvidia Corporation. All rights reserved.
18  *
19  * trace.hpp
20  *
21  * Created on: Sep 8, 2015
22  * Author: mpellauer
23  */
24 
25 #ifndef MATCH_TRACE_H_
26 #define MATCH_TRACE_H_
27 
28 #ifndef __SYNTHESIS__
29 
30 #include <iostream>
31 #include <sstream>
32 
33 #endif
34 
35 namespace match {
36 
37 // Dummy class indicating it's time to flush.
38 class Flusher {
39  public:
40  Flusher() {}
41  ~Flusher() {}
42 };
43 
44 // Buffers messages locally then dumps its local
45 // contents to the shared buffer.
46 // Also supports trace levels.
91 class Tracer {
92  protected:
93 #ifndef __SYNTHESIS__
94  std::ostream* ostr_;
95  int trace_level_;
96  int cur_level_;
97  bool fatal_;
98 #endif
99  public:
100 #ifndef __SYNTHESIS__
101  explicit Tracer(std::ostream* o = NULL)
102  : ostr_(o),
103  trace_level_(0),
104  cur_level_(0),
105  fatal_(false)
106 #else
107  explicit Tracer()
108 #endif
109  {
110 #ifndef __SYNTHESIS__
111  // Default to stdout
112  if (!ostr_) {
113  ostr_ = &std::cout;
114  }
115 #endif
116  }
117  Tracer(const Tracer& other)
118 #ifndef __SYNTHESIS__
119  : ostr_(other.ostr_),
120  trace_level_(other.trace_level_),
121  cur_level_(other.cur_level_),
122  fatal_(other.fatal_)
123 #endif
124  {
125  }
126  Tracer& operator=(const Tracer& other) {
127 #ifndef __SYNTHESIS__
128  ostr_ = other.ostr_;
129  trace_level_ = other.trace_level_;
130  cur_level_ = other.cur_level_;
131  fatal_ = other.fatal_;
132 #endif
133  return *this;
134  }
135 #ifndef __SYNTHESIS__
136  template <typename T_MSG>
137  Tracer& operator<<(T_MSG t) {
138  if (cur_level_ <= trace_level_) {
139  (*ostr_) << t;
140  }
141  return *this;
142  }
143 
144  Tracer& operator<<(std::ostream& (*f)(std::ostream&)) {
145  if (cur_level_ <= trace_level_) {
146  // Handle all the std::ostream stuff
147  (*ostr_) << f;
148  }
149  return *this;
150  }
151 
152  Tracer& operator<<(Tracer& (*f)(Tracer&)) {
153  // Just apply the function.
154  return f(*this);
155  }
156 
157  Tracer& operator<<(Flusher& endt) {
158  // Just flush the buffer.
159  return FlushBuffer();
160  }
161 
162  Tracer& FlushBuffer() {
163  if (cur_level_ <= trace_level_) {
164  (*ostr_) << std::endl;
165  }
166  if (fatal_)
167  exit(1);
168  return *this;
169  }
170 #else
171  template <typename T_MSG>
172  Tracer& operator<<(T_MSG t) {
173  return *this;
174  }
175 #endif
176  int GetTraceLevel() {
177 #ifndef __SYNTHESIS__
178  return trace_level_;
179 #else
180  return 0;
181 #endif
182  }
183 
184  void SetTraceLevel(int l) {
185 #ifndef __SYNTHESIS__
186  trace_level_ = l;
187 #endif
188  }
189  void SetCurrentLevel(int l) {
190 #ifndef __SYNTHESIS__
191  cur_level_ = l;
192 #endif
193  }
194  void SetFatal() {
195 #ifndef __SYNTHESIS__
196  fatal_ = true; // Exit after next buffer dump.
197 #endif
198  }
199 };
200 
201 } // namspace match
202 
203 #endif // INCLUDE_TRACE_H_
Tracer class to dump simulation stats to an output stream (stdout by default).
Definition: nvhls_trace.h:91