AMDiS  2.10
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, fmt::runtime("[" + std::to_string(Environment::mpiRank()) + "] " + str), FWD(args)...);
61  else
62  fmt::print(out, fmt::runtime(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, fmt::runtime("[0] " + str), FWD(args)...);
74  else if (Environment::mpiSize() == 1)
75  fmt::print(out, fmt::runtime(str), FWD(args)...);
76  return out;
77  }
78 
79  template <class T>
80  void printType (T const&)
81  {
82  std::cout << __PRETTY_FUNCTION__ << std::endl;
83  }
84 
85  template <class T>
86  void printType ()
87  {
88  std::cout << __PRETTY_FUNCTION__ << std::endl;
89  }
90 
91  } // end namespace Impl
92 
93 
95 
101  template <class... Args>
102  void msg(std::string const& str, Args&&... args)
103  {
104  Impl::msg(std::cout, str + "\n", FWD(args)...);
105  }
106 
108  template <class... Args>
109  void info(int noInfoLevel, std::string const& str, Args&&... args)
110  {
111  if (Environment::infoLevel() >= noInfoLevel)
112  Impl::msg(std::cout, str + "\n", FWD(args)...);
113  }
114 
115 
117 
123  template <class... Args>
124  void msg_(std::string const& str, Args&&... args)
125  {
126  Impl::msg(std::cout, str, FWD(args)...);
127  }
128 
130  template <class... Args>
131  void info_(int noInfoLevel, std::string const& str, Args&&... args)
132  {
133  if (Environment::infoLevel() >= noInfoLevel)
134  Impl::msg(std::cout, str, FWD(args)...);
135  }
136 
137 
138 
140 
145  template <class... Args>
146  void error_exit(std::string const& str, Args&&... args)
147  {
148 #ifdef AMDIS_NO_THROW
149  Impl::msg_all(std::cerr, "ERROR: " + str + "\n", FWD(args)...);
150  std::abort();
151 #else
152  throw std::runtime_error( std::string("ERROR: ") + fmt::format(fmt::runtime(str), FWD(args)...));
153 #endif
154  }
155 
156 
158 
166  template <class... Args>
167  void test_exit(bool condition, std::string const& str, Args&&... args)
168  {
169  if (!condition) { error_exit(str, FWD(args)...); }
170  }
171 
172 
173  template <class... Args>
174  void warning(std::string const& str, Args&&... args)
175  {
176  Impl::msg(std::cout, "WARNING: " + str + "\n", FWD(args)...);
177  }
178 
179 
181 
186  template <class... Args>
187  void test_warning(bool condition, std::string const& str, Args&&... args)
188  {
189  if (!condition) { warning(str, FWD(args)...); }
190  }
191 
192 
193 #if AMDIS_ENABLE_MSG_DBG
194 
199  template <class... Args>
200  void msg_dbg(Args&&... args) { msg(FWD(args)...); }
201 
202 
204 
208  template <class... Args>
209  void test_exit_dbg(bool condition, Args&&... args)
210  {
211  test_exit(condition, FWD(args)...);
212  }
213 #else
214  template <class... Args>
215  void msg_dbg(Args&&...) {}
216 
217  template <class... Args>
218  void test_exit_dbg(bool, Args&&...) {}
219 #endif
220 
221 } // 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