AMDiS  0.3
The Adaptive Multi-Dimensional Simulation Toolbox
Output.hpp
1 #pragma once
2 
3 #include <iostream>
4 #include <string>
5 
11 #ifdef AMDIS_NO_THROW
12  #include <cassert>
13 #else
14  #include <stdexcept>
15 #endif
16 
18 #include <fmt/core.h>
19 #include <fmt/ostream.h>
20 
21 #include <amdis/Environment.hpp>
22 #include <amdis/common/TypeTraits.hpp>
23 
33 #ifndef AMDIS_ENABLE_MSG_DBG
34  #ifndef NDEBUG
35  #define AMDIS_ENABLE_MSG_DBG 1
36  #else
37  #define AMDIS_ENABLE_MSG_DBG 0
38  #endif
39 #endif
40 
42 #ifndef AMDIS_INFO_LEVEL
43  #define AMDIS_INFO_LEVEL 1
44 #endif
45 
46 #define AMDIS_UNUSED(var) __attribute__((unused)) var
47 
48 #define AMDIS_FUNCNAME(nn) AMDIS_UNUSED(const char *funcName); funcName = nn;
49 
50 #ifdef NDEBUG
51  #define AMDIS_FUNCNAME_DBG(nn)
52  #define AMDIS_DBG_VAR(var)
53 #else
54  #define AMDIS_FUNCNAME_DBG(nn) AMDIS_UNUSED(const char *funcName); funcName = nn;
55  #define AMDIS_DBG_VAR(var) var
56 #endif
57 
58 
59 namespace AMDiS
60 {
61  namespace Impl
62  {
63  template <class OStream, class... Args>
64  OStream& msg_all(OStream& out, std::string const& str, Args&&... args)
65  {
66  if (Environment::mpiSize() > 1)
67  fmt::print(out, "[" + std::to_string(Environment::mpiRank()) + "] " + str, FWD(args)...);
68  else
69  fmt::print(out, str, FWD(args)...);
70  return out;
71  }
72 
73  template <class OStream, class... Args>
74  OStream& msg(OStream& out, std::string const& str, Args&&... args)
75  {
76 #ifdef AMDIS_MSG_ALL_RANKS
77  return msg_all(out, str, FWD(args)...);
78 #else
79  if (Environment::mpiSize() > 1 && Environment::mpiRank() == 0)
80  fmt::print(out, "[0] " + str, FWD(args)...);
81  else if (Environment::mpiSize() == 1)
82  fmt::print(out, str, FWD(args)...);
83  return out;
84 #endif
85  }
86 
87  } // end namespace Impl
88 
89 
91 
97  template <class... Args>
98  void msg(std::string const& str, Args&&... args)
99  {
100  Impl::msg(std::cout, str + "\n", FWD(args)...);
101  }
102 
104  template <class... Args>
105  void info(int noInfoLevel, std::string const& str, Args&&... args)
106  {
107  if (int(AMDIS_INFO_LEVEL) >= noInfoLevel)
108  Impl::msg(std::cout, str + "\n", FWD(args)...);
109  }
110 
111 
113 
119  template <class... Args>
120  void msg_(std::string const& str, Args&&... args)
121  {
122  Impl::msg(std::cout, str, FWD(args)...);
123  }
124 
126  template <class... Args>
127  void info_(int noInfoLevel, std::string const& str, Args&&... args)
128  {
129  if (int(AMDIS_INFO_LEVEL) >= noInfoLevel)
130  Impl::msg(std::cout, str, FWD(args)...);
131  }
132 
133 
134 
136 
141  template <class... Args>
142  void error_exit(std::string const& str, Args&&... args)
143  {
144 #ifdef AMDIS_NO_THROW
145  Impl::msg_all(std::cerr, "ERROR: " + str + "\n", FWD(args)...);
146  std::abort();
147 #else
148  throw std::runtime_error( std::string("ERROR: ") + fmt::format(str, FWD(args)...));
149 #endif
150  }
151 
152 
154 
162  template <class... Args>
163  void test_exit(bool condition, std::string const& str, Args&&... args)
164  {
165  if (!condition) { error_exit(str, FWD(args)...); }
166  }
167 
168 
169  template <class... Args>
170  void warning(std::string const& str, Args&&... args)
171  {
172  Impl::msg(std::cout, "WARNING: " + str + "\n", FWD(args)...);
173  }
174 
175 
177 
182  template <class... Args>
183  void test_warning(bool condition, std::string const& str, Args&&... args)
184  {
185  if (!condition) { warning(str, FWD(args)...); }
186  }
187 
188 
189 #if AMDIS_ENABLE_MSG_DBG
190 
195  template <class... Args>
196  void msg_dbg(Args&&... args) { msg(FWD(args)...); }
197 
198 
200 
204  template <class... Args>
205  void test_exit_dbg(bool condition, Args&&... args)
206  {
207  test_exit(condition, FWD(args)...);
208  }
209 #else
210  template <class... Args>
211  void msg_dbg(Args&&...) {}
212 
213  template <class... Args>
214  void test_exit_dbg(bool, Args&&...) {}
215 #endif
216 
217 } // end namespace AMDiS
void error_exit(std::string const &str, Args &&... args)
print a message and exit
Definition: Output.hpp:142
void test_exit_dbg(bool condition, Args &&... args)
call assert_msg, in debug mode only
Definition: Output.hpp:205
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
void info_(int noInfoLevel, std::string const &str, Args &&... args)
prints a message, if Environment::infoLevel() >= noInfoLevel
Definition: Output.hpp:127
static int mpiSize()
Return the MPI_Size of the group created by Dune::MPIHelper.
Definition: Environment.hpp:74
void msg(std::string const &str, Args &&... args)
print a message
Definition: Output.hpp:98
void msg_(std::string const &str, Args &&... args)
print a message (without appended newline)
Definition: Output.hpp:120
void info(int noInfoLevel, std::string const &str, Args &&... args)
prints a message, if Environment::infoLevel() >= noInfoLevel
Definition: Output.hpp:105
void test_exit(bool condition, std::string const &str, Args &&... args)
test for condition and in case of failure print message and exit
Definition: Output.hpp:163
void test_warning(bool condition, std::string const &str, Args &&... args)
test for condition and in case of failure print message
Definition: Output.hpp:183
void msg_dbg(Args &&... args)
print message, in debug mode only
Definition: Output.hpp:196
static int mpiRank()
Return the MPI_Rank of the current processor.
Definition: Environment.hpp:68