2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018,2019, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \libinternal \file
37 * Declares functionality for logging.
39 * See \ref page_logging for an overview of the functionality.
42 * \ingroup module_utility
44 #ifndef GMX_UTILITY_LOGGER_H
45 #define GMX_UTILITY_LOGGER_H
49 #include "gromacs/utility/stringutil.h"
56 LogEntry() : asParagraph(false) {}
62 /*! \libinternal \brief
63 * Target where log output can be written.
65 * \ingroup module_utility
70 virtual ~ILogTarget();
72 //! Writes a log entry to this target.
73 virtual void writeEntry(const LogEntry
& entry
) = 0;
76 /*! \libinternal \brief
77 * Helper class for creating log entries with ::GMX_LOG.
79 * \ingroup module_utility
84 //! Appends given text as a line in the log entry.
85 LogEntryWriter
& appendText(const char* text
)
87 entry_
.text
.append(text
);
90 //! Appends given text as a line in the log entry.
91 LogEntryWriter
& appendText(const std::string
& text
)
93 entry_
.text
.append(text
);
96 //! Appends given text as a line in the log entry, with printf-style formatting.
97 LogEntryWriter
& appendTextFormatted(gmx_fmtstr
const char* fmt
, ...) gmx_format(printf
, 2, 3);
98 //! Writes the log entry with empty lines before and after.
99 LogEntryWriter
& asParagraph()
101 entry_
.asParagraph
= true;
108 friend class LogWriteHelper
;
112 * Helper class for implementing ::GMX_LOG.
114 * \ingroup module_utility
119 //! Initializes a helper for writing to the given target.
120 explicit LogWriteHelper(ILogTarget
* target
) : target_(target
) {}
122 // Should be explicit, once that works in CUDA.
124 * Returns whether anything needs to be written.
126 * Note that the return value is unintuitively `false` when the target
127 * is active, to allow implementing ::GMX_LOG like it is now.
129 operator bool() const { return target_
== nullptr; }
132 * Writes the entry from the given writer to the log target.
134 * This is implemented as an assignment operator to get proper
135 * precedence for operations for the ::GMX_LOG macro; this is a common
136 * technique for implementing macros that allow streming information to
137 * them (see, e.g., Google Test).
139 LogWriteHelper
& operator=(const LogEntryWriter
& entryWriter
)
141 target_
->writeEntry(entryWriter
.entry_
);
149 /*! \libinternal \brief
150 * Represents a single logging level.
152 * Typically this type is not used directly, but instances in MDLogger are
153 * simply accessed through ::GMX_LOG in code that writes to the log.
155 * \ingroup module_utility
160 //! Initializes a helper for writing to the given target.
161 explicit LogLevelHelper(ILogTarget
* target
) : target_(target
) {}
163 // Both of the below should be explicit, once that works in CUDA.
164 //! Returns whether the output for this log level goes anywhere.
165 operator bool() const { return target_
!= nullptr; }
167 //! Creates a helper for ::GMX_LOG.
168 operator LogWriteHelper() const { return LogWriteHelper(target_
); }
174 /*! \libinternal \brief
175 * Declares a logging interface.
177 * Typically, this object is not created directly, but instead through
180 * For now, this is named MDLogger, since it is used only there, and it is not
181 * clear whether the logging levels can be the same throughout the code. It
182 * should be relatively straightforward to split this into multiple classes
183 * with different supported logging levels without changing calling code, or to
184 * rename it to Logger if we do not need any specialization.
186 * \ingroup module_utility
191 //! Supported logging levels.
201 //! Number of logging levels.
202 static const int LogLevelCount
= static_cast<int>(LogLevel::Count
);
205 //! Creates a logger with the given targets.
206 explicit MDLogger(ILogTarget
* targets
[LogLevelCount
]);
208 //! For writing at LogLevel::Warning level.
209 LogLevelHelper warning
;
210 //! For writing at LogLevel::Error level.
211 LogLevelHelper error
;
212 //! For writing at LogLevel::Debug level.
213 LogLevelHelper debug
;
214 //! For writing at LogLevel::VerboseDebug level.
215 LogLevelHelper verboseDebug
;
216 //! For writing at LogLevel::Info level.
221 * Helper to log information using gmx::MDLogger.
223 * \param logger LogLevelHelper instance to use for logging.
227 GMX_LOG(logger.warning).appendText(...);
229 * and ensures that the code to format the output is only executed when the
230 * output goes somewhere.
232 * See LogEntryWriter for functions that can be used with the macro (such as
233 * the appendText() in the example).
235 * \ingroup module_utility
237 #define GMX_LOG(logger) \
238 if (::gmx::LogWriteHelper helper = ::gmx::LogWriteHelper(logger)) {} \
240 helper = ::gmx::LogEntryWriter()