All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
mfLoggingClass.h
Go to the documentation of this file.
1 /**
2  * @file icarusalg/Utilities/mfLoggingClass.h
3  * @brief Base class facilitating logging to message facility.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date September 16, 2020
6  */
7 
8 #ifndef ICARUSALG_UTILITIES_MFLOGGINGCLASS_H
9 #define ICARUSALG_UTILITIES_MFLOGGINGCLASS_H
10 
11 // framework libraries
12 #include "messagefacility/MessageLogger/MessageLogger.h"
13 
14 // C/C++ standard libraries
15 #include <string>
16 #ifdef __cpp_lib_source_location
17 # include <source_location>
18 #endif // __cpp_lib_source_location
19 
20 
21 //------------------------------------------------------------------------------
22 namespace icarus::ns::util { class mfLoggingClass; }
23 
24 /**
25  * @brief Helper for logging classes.
26  *
27  * A derived class can utilize the member functions of this class for easier
28  * tracking of the boilerplate category:
29  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
30  * struct Algorithm: private icarus::mfLoggingClass {
31  *
32  * Algorithm(): icarus::ns::util::mfLoggingClass("Algorithm") {}
33  *
34  * double compute() const
35  * {
36  * mfLogInfo() << "Starting computation()";
37  * return 0.0;
38  * }
39  *
40  * }; // class Algorithm
41  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42  *
43  */
45 
46  std::string fLogCategory; ///< Logging category string used for the messages.
47 
48  public:
49 
50  /// Constructor: initializes with the specified log category.
51  mfLoggingClass(std::string const& logCategory): fLogCategory(logCategory) {}
52 
53  /// Returns the logging category string for this object.
54  std::string logCategory() const { return fLogCategory; }
55 
56  /// Returns this object (as a logging class object).
57  mfLoggingClass const& loggingClass() const { return *this; }
58 
59  // --- BEGIN -- Access to temporary loggers ----------------------------------
60  /**
61  * @name Access to temporary loggers
62  *
63  * These methods return a temporary logger for fast logging:
64  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
65  * mfLogError() << "That was not a smart thing to do!";
66  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
67  *
68  * The returned log can also be made a bit less temporary if some more complex
69  * output is required:
70  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
71  * if (!reasons.empty()) {
72  * auto log = mfLogError();
73  * log << "That was not a smart thing to do, for "
74  * << size(reasons) << " reasons:";
75  * for (auto const& [ iReason, reason ]: util::enumerate(reasons))
76  * log << "\n " << (iReason+1) << ": " << reason;
77  * } // if
78  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
79  *
80  * The `file` and `lineNumber` argument are optional and passed directly to
81  * the logger on construction. If specified, they provide information about
82  * the location of the message source:
83  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
84  * mfLogError(__FILE__, __LINE__) << "That was not a smart thing to do!";
85  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86  *
87  * @note With C++20 it will be possible to extend the implementation to
88  * allow for automatic detection of the call location.
89  */
90  /// @{
91 
92  /// Returns a mf::LogError() stream for logging.
93  mf::LogError mfLogError
94  (std::string const& file = {}, int const lineNumber = 0) const
95  { return { logCategory(), file, lineNumber }; }
96 
97  /// Returns a mf::LogWarning() stream for logging.
98  mf::LogWarning mfLogWarning
99  (std::string const& file = {}, int const lineNumber = 0) const
100  { return { logCategory(), file, lineNumber }; }
101 
102  /// Returns a mf::LogProblem() stream for logging.
103  mf::LogProblem mfLogProblem
104  (std::string const& file = {}, int const lineNumber = 0) const
105  { return { logCategory(), file, lineNumber }; }
106 
107  /// Returns a mf::LogInfo() stream for logging.
108  mf::LogInfo mfLogInfo
109  (std::string const& file = {}, int const lineNumber = 0) const
110  { return { logCategory(), file, lineNumber }; }
111 
112  /// Returns a mf::LogVerbatim() stream for logging.
113  mf::LogVerbatim mfLogVerbatim
114  (std::string const& file = {}, int const lineNumber = 0) const
115  { return { logCategory(), file, lineNumber }; }
116 
117  /// Returns a mf::LogDebug() stream for logging.
118  mf::LogDebug mfLogDebug
119  (std::string const& file = {}, int const lineNumber = 0) const
120  { return { logCategory(), file, lineNumber }; }
121 
122  /// Returns a mf::LogTrace() stream for logging.
123  mf::LogTrace mfLogTrace
124  (std::string const& file = {}, int const lineNumber = 0) const
125  { return { logCategory(), file, lineNumber }; }
126 
127 #ifdef __cpp_lib_source_location
128 
129  /// Returns a mf::LogDebug() with information about the calling location.
130  mf::LogDebug mfLogDebugLine
131  (std::source_location const loc = std::source_location::current()) const
132  { return { logCategory(), loc.file_name(), loc.line_number() }; }
133 
134 #endif // __cpp_lib_source_location
135 
136  /// @}
137  // --- END -- Access to temporary loggers ------------------------------------
138 
139 }; // class icarus::ns::util::mfLoggingClass
140 
141 
142 #endif // ICARUSALG_UTILITIES_MFLOGGINGCLASS_H
* file
Definition: file_to_url.sh:69
mf::LogDebug mfLogDebug(std::string const &file={}, int const lineNumber=0) const
Returns a mf::LogDebug() stream for logging.
Helper for logging classes.
mfLoggingClass(std::string const &logCategory)
Constructor: initializes with the specified log category.
mf::LogVerbatim mfLogVerbatim(std::string const &file={}, int const lineNumber=0) const
Returns a mf::LogVerbatim() stream for logging.
mf::LogInfo mfLogInfo(std::string const &file={}, int const lineNumber=0) const
Returns a mf::LogInfo() stream for logging.
mf::LogTrace mfLogTrace(std::string const &file={}, int const lineNumber=0) const
Returns a mf::LogTrace() stream for logging.
mf::LogWarning mfLogWarning(std::string const &file={}, int const lineNumber=0) const
Returns a mf::LogWarning() stream for logging.
mf::LogProblem mfLogProblem(std::string const &file={}, int const lineNumber=0) const
Returns a mf::LogProblem() stream for logging.
std::string fLogCategory
Logging category string used for the messages.
mfLoggingClass const & loggingClass() const
Returns this object (as a logging class object).
mf::LogError mfLogError(std::string const &file={}, int const lineNumber=0) const
Returns a mf::LogError() stream for logging.
std::string logCategory() const
Returns the logging category string for this object.