svFSIplus
DebugMsg.h
1 /* Copyright (c) Stanford University, The Regents of the University of California, and others.
2  *
3  * All Rights Reserved.
4  *
5  * See Copyright-SimVascular.txt for additional details.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject
13  * to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
19  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef DEBUG_MSG_H
32 #define DEBUG_MSG_H
33 
34 #include <iostream>
35 
36 /// @brief The DebugMsg is class is used to print debugging messages.
37 class DebugMsg
38 {
39  public:
40  DebugMsg() = delete;
41  DebugMsg(const char* function, const int task_id, bool add_endl=true)
42  {
43  function_name_ = function;
44  task_id_ = task_id;
45  prefix_ = "[" + function_name_ + ":" + std::to_string(task_id) + "] ";
46  banner_ = prefix_ + "=============== " + function_name_ + " ===============";
47  add_endl_ = add_endl;
48  }
49 
50  void banner() { std::cout << banner_ << std::endl; };
51  std::string prefix() { return prefix_; };
52 
53  template <class T> DebugMsg& operator<< (const T& x)
54  {
55  if (start_) {
56  std::cout << prefix_;
57  }
58 
59  std::cout << x;
60  count_ += 1;
61 
62  if (add_endl_ && count_ == 2) {
63  std::cout << std::endl;
64  start_ = true;
65  count_ = 0;
66  } else {
67  start_ = false;
68  }
69 
70  return *this;
71  };
72 
73  DebugMsg& operator<<(std::ostream& (*f)(std::ostream& o))
74  {
75  std::cout << f;
76  start_ = true;
77  count_ = 0;
78  return *this;
79  };
80 
81  private:
82  bool add_endl_ = true;
83  std::string banner_;
84  int count_ = 0;
85  std::string function_name_;
86  std::string prefix_;
87  bool start_ = true;
88  int task_id_;
89 };
90 
91 #endif
92 
The DebugMsg is class is used to print debugging messages.
Definition: DebugMsg.h:38