BioDynaMo  v1.05.119-a4ff3934
log.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_UTIL_LOG_H_
16 #define CORE_UTIL_LOG_H_
17 
18 #include <TError.h>
19 
20 #include <cstdio>
21 #include <cstdlib>
22 #include <functional>
23 #include <iostream>
24 #include <string>
25 
26 #include "core/util/string.h"
27 
28 namespace bdm {
29 
32 class Log {
33  public:
39  template <typename... Args>
40  static void Debug(const std::string& location, const Args&... parts) {
41  // kPrint has the highest level of verbosity
42  if (gErrorIgnoreLevel <= kPrint) {
43  std::string message = Concat(parts...);
44  // Mimic ROOT logging output
45  fprintf(stderr, "Debug in <%s>: %s\n", location.c_str(), message.c_str());
46  }
47  }
48 
54  template <typename... Args>
55  static void Info(const std::string& location, const Args&... parts) {
56  std::string message = Concat(parts...);
57  // ROOT function
58  ::Info(location.c_str(), "%s", message.c_str());
59  }
60 
66  template <typename... Args>
67  static void Warning(const std::string& location, const Args&... parts) {
68  std::string message = Concat(parts...);
69  // ROOT function
70  ::Warning(location.c_str(), "%s", message.c_str());
71  }
72 
78  template <typename... Args>
79  static void Error(const std::string& location, const Args&... parts) {
80  std::string message = Concat(parts...);
81  // ROOT function
82  ::Error(location.c_str(), "%s", message.c_str());
83  }
84 
90  template <typename... Args>
91  static void Break(const std::string& location, const Args&... parts) {
92  std::string message = Concat(parts...);
93  // ROOT function
94  ::Break(location.c_str(), "%s", message.c_str());
95  }
96 
102  template <typename... Args>
103  static void SysError(const std::string& location, const Args&... parts) {
104  std::string message = Concat(parts...);
105  // ROOT function
106  ::SysError(location.c_str(), "%s", message.c_str());
107  }
108 
114  template <typename... Args>
115  static void Fatal(const std::string& location, const Args&... parts) {
116  std::string message = Concat(parts...);
117  // ROOT function
118  ::Error(location.c_str(), "%s", message.c_str());
119  // std::runtime_error() will fail the DeathTests
120  exit(1);
121  }
122 
123  template <typename... Args>
124  static void Condition(const std::function<bool()>& lambda,
125  const Args&... parts) {
126  // kPrint has the highest level of verbosity
127  if (lambda()) {
128  std::string message = Concat(parts...);
129  // Mimic ROOT logging output
130  fprintf(stdout, "%s\n", message.c_str());
131  }
132  }
133 };
134 } // namespace bdm
135 
136 #endif // CORE_UTIL_LOG_H_
bdm
Definition: agent.cc:39
string.h
bdm::Log::SysError
static void SysError(const std::string &location, const Args &... parts)
Prints system error message.
Definition: log.h:103
bdm::Log::Warning
static void Warning(const std::string &location, const Args &... parts)
Prints warning message.
Definition: log.h:67
bdm::Log::Info
static void Info(const std::string &location, const Args &... parts)
Prints information message.
Definition: log.h:55
bdm::Concat
std::string Concat(const Args &... parts)
Concatenates all arguments into a string. Equivalent to streaming all arguments into a stringstream a...
Definition: string.h:70
bdm::Log::Debug
static void Debug(const std::string &location, const Args &... parts)
Prints debug message.
Definition: log.h:40
bdm::Log::Fatal
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
Definition: log.h:115
bdm::Log::Error
static void Error(const std::string &location, const Args &... parts)
Prints error message.
Definition: log.h:79
bdm::Log::Condition
static void Condition(const std::function< bool()> &lambda, const Args &... parts)
Definition: log.h:124
bdm::Log::Break
static void Break(const std::string &location, const Args &... parts)
Prints break message.
Definition: log.h:91
bdm::Log
Wrapper class over ROOT logging module.
Definition: log.h:32