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 
41 #define AMDIS_FUNCNAME(nn) [[maybe_unused]] const char *funcName = nn;
42 
43 #ifdef NDEBUG
44  #define AMDIS_FUNCNAME_DBG(nn)
45  #define AMDIS_DBG_VAR(var)
46 #else
47  #define AMDIS_FUNCNAME_DBG(nn) [[maybe_unused]] const char *funcName = nn;
48  #define AMDIS_DBG_VAR(var) var
49 #endif
50 
51 
52 namespace AMDiS
53 {
54  namespace Impl
55  {
56  template <class OStream, class... Args>
57  OStream& msg_all(OStream& out, std::string const& str, Args&&... args)
58  {
59  if (Environment::mpiSize() > 1)
60  fmt::print(out, "[" + std::to_string(Environment::mpiRank()) + "] " + str, FWD(args)...);
61  else
62  fmt::print(out, str, FWD(args)...);
63  return out;
64  }
65 
66  template <class OStream, class... Args>
67  OStream& msg(OStream& out, std::string const& str, Args&&... args)
68  {
70  return msg_all(out, str, FWD(args)...);
71 
72  if (Environment::mpiSize() > 1 && Environment::mpiRank() == 0)
73  fmt::print(out, "[0] " + str, FWD(args)...);
74  else if (Environment::mpiSize() == 1)
75  fmt::print(out, str, FWD(args)...);
76  return out;
77  }
78 
79  } // end namespace Impl
80 
81 
83 
89  template <class... Args>
90  void msg(std::string const& str, Args&&... args)
91  {
92  Impl::msg(std::cout, str + "\n", FWD(args)...);
93  }
94 
96  template <class... Args>
97  void info(int noInfoLevel, std::string const& str, Args&&... args)
98  {
99  if (Environment::infoLevel() >= noInfoLevel)
100  Impl::msg(std::cout, str + "\n", FWD(args)...);
101  }
102 
103 
105 
111  template <class... Args>
112  void msg_(std::string const& str, Args&&... args)
113  {
114  Impl::msg(std::cout, str, FWD(args)...);
115  }
116 
118  template <class... Args>
119  void info_(int noInfoLevel, std::string const& str, Args&&... args)
120  {
121  if (Environment::infoLevel() >= noInfoLevel)
122  Impl::msg(std::cout, str, FWD(args)...);
123  }
124 
125 
126 
128 
133  template <class... Args>
134  void error_exit(std::string const& str, Args&&... args)
135  {
136 #ifdef AMDIS_NO_THROW
137  Impl::msg_all(std::cerr, "ERROR: " + str + "\n", FWD(args)...);
138  std::abort();
139 #else
140  throw std::runtime_error( std::string("ERROR: ") + fmt::format(str, FWD(args)...));
141 #endif
142  }
143 
144 
146 
154  template <class... Args>
155  void test_exit(bool condition, std::string const& str, Args&&... args)
156  {
157  if (!condition) { error_exit(str, FWD(args)...); }
158  }
159 
160 
161  template <class... Args>
162  void warning(std::string const& str, Args&&... args)
163  {
164  Impl::msg(std::cout, "WARNING: " + str + "\n", FWD(args)...);
165  }
166 
167 
169 
174  template <class... Args>
175  void test_warning(bool condition, std::string const& str, Args&&... args)
176  {
177  if (!condition) { warning(str, FWD(args)...); }
178  }
179 
180 
181 #if AMDIS_ENABLE_MSG_DBG
182 
187  template <class... Args>
188  void msg_dbg(Args&&... args) { msg(FWD(args)...); }
189 
190 
192 
196  template <class... Args>
197  void test_exit_dbg(bool condition, Args&&... args)
198  {
199  test_exit(condition, FWD(args)...);
200  }
201 #else
202  template <class... Args>
203  void msg_dbg(Args&&...) {}
204 
205  template <class... Args>
206  void test_exit_dbg(bool, Args&&...) {}
207 #endif
208 
209 } // end namespace AMDiS
static int infoLevel()
Return the info level for messages in info()
Definition: Environment.cpp:63
Definition: AdaptBase.hpp:6
static int mpiSize()
Return the MPI_Size of the group created by Dune::MPIHelper.
Definition: Environment.hpp:74
static bool msgAllRanks()
Return whether all ranks print msg()
Definition: Environment.cpp:71
static int mpiRank()
Return the MPI_Rank of the current processor.
Definition: Environment.hpp:68