ABLATE Source Documentation  0.12.34
loggable.hpp
1 #ifndef ABLATELIBRARY_LOGGABLE_HPP
2 #define ABLATELIBRARY_LOGGABLE_HPP
3 #include <petsc.h>
4 #include "demangler.hpp"
5 #include "petscUtilities.hpp"
6 
7 namespace ablate::utilities {
8 template <class T>
9 class Loggable {
10  private:
11  inline static PetscClassId petscClassId = 0;
12 
13  // Store a single depth active event
14  mutable PetscLogEvent activeEvent = PETSC_DECIDE;
15 
16  protected:
17  Loggable() {
18  if (petscClassId == 0) {
19  auto className = utilities::Demangler::Demangle(typeid(T).name());
20  PetscClassIdRegister(className.c_str(), &petscClassId) >> utilities::PetscUtilities::checkError;
21  }
22  }
23 
24  inline const PetscClassId& GetPetscClassId() const { return petscClassId; }
25 
26  inline PetscLogEvent RegisterEvent(const char* eventName) {
27  PetscLogEvent eventId;
28  PetscLogEventRegister(eventName, petscClassId, &eventId) >> utilities::PetscUtilities::checkError;
29  return eventId;
30  }
31 
32  inline void StartEvent(const char* eventName) const {
33  if (activeEvent == PETSC_DECIDE) {
34  PetscLogEventRegister(eventName, petscClassId, &activeEvent) >> utilities::PetscUtilities::checkError;
35  PetscLogEventBegin(activeEvent, 0, 0, 0, 0) >> utilities::PetscUtilities::checkError;
36  } else {
37  throw std::runtime_error("Cannot Start Event, an event is already active.");
38  }
39  }
40 
41  inline void EndEvent() const {
42  if (activeEvent > 0) {
43  PetscLogEventEnd(activeEvent, 0, 0, 0, 0);
44  activeEvent = PETSC_DECIDE;
45  } else {
46  throw std::runtime_error("Cannot End Event. No active event.");
47  }
48  }
49 };
50 }; // namespace ablate::utilities
51 
52 #endif // ABLATELIBRARY_LOGGABLE_HPP
Definition: loggable.hpp:9
static utilities::PetscUtilities::ErrorChecker checkError
Definition: petscUtilities.hpp:46