CEPS  24.01
Cardiac ElectroPhysiology Simulator
CepsFlags.cpp
Go to the documentation of this file.
1 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2  This file is part of CEPS.
3 
4  CEPS is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  CEPS is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with CEPS (see file LICENSE at root of project).
16  If not, see <https://www.gnu.org/licenses/>.
17 
18 
19  Copyright 2019-2024 Inria, Universite de Bordeaux
20 
21  Authors, in alphabetical order:
22 
23  Pierre-Elliott BECUE, Florian CARO, Yves COUDIERE(*), Andjela DAVIDOVIC,
24  Charlie DOUANLA-LONTSI, Marc FUENTES, Mehdi JUHOOR, Michael LEGUEBE(*),
25  Pauline MIGERDITICHAN, Valentin PANNETIER(*), Nejib ZEMZEMI.
26  * : currently active authors
27 
28 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
32 
33 #include <sys/types.h> //*nix only !
34 #include <unistd.h>
35 #include <iomanip>
36 #include <ctime>
37 #include <csignal>
38 
39 Flags::Flags(int argc, CepsChar** argv) :
40  m_profiling(false),
41  m_verbose (false),
42  m_warnings (false),
43  m_testing (false)
44 {
45  #ifdef CEPS_DEBUG_ENABLED
46  enableDebug();
47  #endif
48 
49  int optionChar;
50  optind = 1;
51  while ((optionChar=getopt(argc,argv,"vqwph"))!=EOF)
52  {
53  switch (optionChar)
54  {
55  case 'p':
56  {
58  break;
59  }
60  case 'v':
61  m_verbose = true;
62  break;
63  case 'q':
64  m_verbose = false;
65  break;
66  case 'w':
67  enableWarnings();
68  break;
69  case 'h':
70  case '?':
71  default:
72  printHelp(argv);
73  exit(EXIT_SUCCESS);
74  }
75  }
76 }
77 
78 void
80 {
81  m_verbose = v;
82 }
83 
84 void
86 {
87  auto now = std::time(nullptr);
88  auto tm = *std::localtime(&now);
89 
90  if (m_debugLog.is_open())
91  return;
92 
93  if (ceps::isMaster())
94  {
95  std::stringstream ss;
96  ss << "debug_" << getpid() << ".log";
97  m_debugLogName = ss.str();
98  m_debugLog.open(m_debugLogName.c_str());
99 
100  m_debugLog << std::string(77, '-') << std::endl;
101  m_debugLog << " Debug log file opened at " << std::put_time(&tm,"%d-%m-%Y %H-%M-%S")
102  << std::endl;
103  m_debugLog << std::string(77, '-') << std::endl;
104  }
105 }
106 
107 void
109 {
110  auto now = std::time(nullptr);
111  auto tm = *std::localtime(&now);
112 
113  if (m_profiling)
114  return;
115 
116  m_profiling = true;
117 
118  if (ceps::isMaster())
119  {
120  std::stringstream ss;
121  ss << "profiling_" << getpid() << ".log";
122  m_profilingLogName = ss.str();
123  m_profilingLog.open(m_profilingLogName.c_str ());
124  m_profilingLog << "# " << std::string(77, '-') << std::endl;
125  m_profilingLog << "# Profiling log file opened at " << std::put_time(&tm, "%d-%m-%Y %H-%M-%S")
126  << std::endl;
127  m_profilingLog << "# " << std::string(77, '-') << std::endl;
128  }
129 }
130 
131 void
133 {
134  m_testing = true;
135 }
136 
137 void
139 {
140  m_testing = true;
141 }
142 
143 void
145 {
146  m_warnings = true;
147 }
148 
149 void
151 {
152  m_warnings = false;
153 }
154 
156 {
157  auto now = std::time(nullptr);
158  auto tm = *std::localtime(&now);
159 
160  m_debugLog << std::string(77, '-') << std::endl;
161  m_debugLog << " Debug log file closed at " << std::put_time(&tm,"%d-%m-%Y %H-%M-%S")
162  << std::endl;
163  m_debugLog << std::string(77, '-') << std::endl;
164 
165  if (m_profiling and ceps::isMaster())
166  {
167  if (m_verbose)
168  {
169  getProfiler()->display();
170  std::cout << CepsString(77,'=') << std::endl;
171  }
173 
174 
175  m_profilingLog << "# " << std::string(77, '-') << std::endl;
176  m_profilingLog << "# Profiling log file closed at " << std::put_time(&tm,"%d-%m-%Y %H-%M-%S")
177  << std::endl;
178  m_profilingLog << "# " << std::string(77, '-') << std::endl;
179  }
180 }
181 
182 CepsBool
184 {
185  return m_profiling;
186 }
187 
188 CepsBool
190 {
191  return m_testing;
192 }
193 
194 CepsBool
196 {
197  return m_verbose;
198 }
199 
200 CepsBool
202 {
203  return m_warnings;
204 }
205 
206 void
208 {
209  std::cout << " Usage:\n " << argv[0] << " <paramFile>" << std::endl;
210  std::cout << std::string(77, '-') << std::endl;
211  std::cout << " Options :" << std::endl
212  << " -v verbose mode, prints stuff on screen" << std::endl
213  << " -q quiet, no output on stdout (default)" << std::endl
214  << " -w prints all warnings" << std::endl
215  << " -p profiling mode, writes a profiling_XXXX.log file" << std::endl
216  << " -h prints this help and quits" << std::endl;
217  std::cout << std::string(77, '-') << std::endl;
218 }
219 
222 {
223  return m_debugLogName;
224 }
225 
228 {
229  return m_profilingLogName;
230 }
231 
232 std::ofstream&
234 {
235  return m_debugLog;
236 }
237 
238 std::ofstream&
240 {
241  return m_profilingLog;
242 }
243 
244 std::ofstream&
246 {
247  return flags->getDebugLog();
248 }
249 
250 std::ofstream&
252 {
253  return flags->getProfilingLog();
254 }
255 
256 CepsBool
258 {
259  return flags->profiling();
260 }
261 
262 CepsBool
264 {
265  return ceps::isMaster() and flags->verbose();
266 }
267 
268 CepsBool
270 {
271  return ceps::isMaster() and flags->warnings();
272 }
273 
274 CepsBool
276 {
277  #ifdef CEPS_DEBUG_ENABLED
278  return ceps::isMaster();
279  #else
280  return false;
281  #endif
282 }
283 
284 void
286 {
287  flags->enableTesting();
288 }
289 
290 void
292 {
294 }
std::basic_string< CepsChar > CepsString
C++ format string.
Definition: CepsTypes.hpp:128
char CepsChar
Char.
Definition: CepsTypes.hpp:125
bool CepsBool
Booleans.
Definition: CepsTypes.hpp:124
Flags * flags
Global variable, used in every application.
Definition: ceps.cpp:34
Profiler * getProfiler() const
Access to profiler.
Definition: CepsObject.cpp:46
std::ofstream m_debugLog
Debug file stream (opened in init, closed at finalize)
Definition: CepsFlags.hpp:146
void enableProfiling()
Activates profiling, opens profiling log stream.
Definition: CepsFlags.cpp:108
CepsBool m_testing
Used for unit tests.
Definition: CepsFlags.hpp:143
void disableWarnings()
Disables warning messages.
Definition: CepsFlags.cpp:150
CepsBool warnings() const
True if option -w was set in command line.
Definition: CepsFlags.cpp:201
std::ofstream & getDebugLog()
the Log stream
Definition: CepsFlags.cpp:233
CepsBool testing() const
Tells if currently running in a test suite (switched in CepsGlobalFixture.hpp)
Definition: CepsFlags.cpp:189
CepsString m_profilingLogName
Profiling file name.
Definition: CepsFlags.hpp:147
CepsString getDebugLogName() const
Name has the form debug_PID.log (PID of master node)
Definition: CepsFlags.cpp:221
std::ofstream m_profilingLog
Profiling file stream (opened in init, closed at finalize)
Definition: CepsFlags.hpp:148
std::ofstream & getProfilingLog()
The Profiling stream.
Definition: CepsFlags.cpp:239
void disableTesting()
Disables testing (CepsException for errors now cause abort)
Definition: CepsFlags.cpp:138
CepsBool verbose() const
True if option -d was set in command line.
Definition: CepsFlags.cpp:195
void printHelp(CepsChar **argv)
Display info on how to type in command line.
Definition: CepsFlags.cpp:207
CepsString m_debugLogName
Debug file name.
Definition: CepsFlags.hpp:145
CepsBool m_verbose
Blah blah if true.
Definition: CepsFlags.hpp:141
CepsString getProfilingLogName() const
Name has the form profiling_PID.log (PID of master node)
Definition: CepsFlags.cpp:227
void enableTesting()
Enables testing (CepsException for errors do not cause abort)
Definition: CepsFlags.cpp:132
Flags()=delete
CepsBool m_warnings
More blah blah if true.
Definition: CepsFlags.hpp:142
CepsBool m_profiling
Profiling option.
Definition: CepsFlags.hpp:140
CepsBool profiling() const
True if option -p was set in command line.
Definition: CepsFlags.cpp:183
~Flags()
If needed closes the logs with current time.
Definition: CepsFlags.cpp:155
void enableDebug()
Activates profiling, opens debug log stream.
Definition: CepsFlags.cpp:85
void enableWarnings()
Enables warning messages.
Definition: CepsFlags.cpp:144
void setVerbose(CepsBool v)
Enable/disable verbosity.
Definition: CepsFlags.cpp:79
void display(const CepsString &lbl, std::ostream &os) const
Prints time spent by a specific chronometer.
CepsBool isVerbose()
Check if the verbosity is enabled on the master proc (always false on slave procs).
Definition: CepsFlags.cpp:263
std::ofstream & debugLog()
Get the DebugLog used in Ceps.
Definition: CepsFlags.cpp:245
CepsBool isWarnings()
Check if we enable the warnings on the master proc (always false on slave procs).
Definition: CepsFlags.cpp:269
CepsBool isMaster()
Is calling process the master ?
CepsBool isDebug()
Check if we enable the debug on the master proc (always false on slave procs).
Definition: CepsFlags.cpp:275
std::ofstream & profilingLog()
Get the ProfilingLog used in Ceps.
Definition: CepsFlags.cpp:251
void disableAbortOnError()
Set testing to true. Exceptions do not terminate program.
Definition: CepsFlags.cpp:285
void enableAbortOnError()
Set testing to false. Exceptions terminate program.
Definition: CepsFlags.cpp:291
CepsBool isProfiling()
Check if we are currently profiling on the master proc (always false on slave procs).
Definition: CepsFlags.cpp:257