2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017,2018, 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
54 LogEntry() : asParagraph(false) {}
60 /*! \libinternal \brief
61 * Target where log output can be written.
63 * \ingroup module_utility
68 virtual ~ILogTarget();
70 //! Writes a log entry to this target.
71 virtual void writeEntry(const LogEntry
&entry
) = 0;
74 /*! \libinternal \brief
75 * Helper class for creating log entries with ::GMX_LOG.
77 * \ingroup module_utility
82 //! Appends given text to the log entry.
83 LogEntryWriter
&appendText(const char *text
)
85 entry_
.text
.append(text
);
88 //! Appends given text to the log entry.
89 LogEntryWriter
&appendText(const std::string
&text
)
91 entry_
.text
.append(text
);
94 //! Appends given text to the log entry, with printf-style formatting.
95 LogEntryWriter
&appendTextFormatted(const char *fmt
, ...);
96 //! Writes the log entry with empty lines before and after.
97 LogEntryWriter
&asParagraph()
99 entry_
.asParagraph
= true;
106 friend class LogWriteHelper
;
110 * Helper class for implementing ::GMX_LOG.
112 * \ingroup module_utility
117 //! Initializes a helper for writing to the given target.
118 explicit LogWriteHelper(ILogTarget
*target
) : target_(target
) {}
120 // Should be explicit, once that works in CUDA.
122 * Returns whether anything needs to be written.
124 * Note that the return value is unintuitively `false` when the target
125 * is active, to allow implementing ::GMX_LOG like it is now.
127 operator bool() const { return target_
== nullptr; }
130 * Writes the entry from the given writer to the log target.
132 * This is implemented as an assignment operator to get proper
133 * precedence for operations for the ::GMX_LOG macro; this is a common
134 * technique for implementing macros that allow streming information to
135 * them (see, e.g., Google Test).
137 LogWriteHelper
&operator=(const LogEntryWriter
&entryWriter
)
139 target_
->writeEntry(entryWriter
.entry_
);
147 /*! \libinternal \brief
148 * Represents a single logging level.
150 * Typically this type is not used directly, but instances in MDLogger are
151 * simply accessed through ::GMX_LOG in code that writes to the log.
153 * \ingroup module_utility
158 //! Initializes a helper for writing to the given target.
159 explicit LogLevelHelper(ILogTarget
*target
) : target_(target
) {}
161 // Both of the below should be explicit, once that works in CUDA.
162 //! Returns whether the output for this log level goes anywhere.
163 operator bool() const { return target_
!= nullptr; }
165 //! Creates a helper for ::GMX_LOG.
166 operator LogWriteHelper() const { return LogWriteHelper(target_
); }
172 /*! \libinternal \brief
173 * Declares a logging interface.
175 * Typically, this object is not created directly, but instead through
178 * For now, this is named MDLogger, since it is used only there, and it is not
179 * clear whether the logging levels can be the same throughout the code. It
180 * should be relatively straightforward to split this into multiple classes
181 * with different supported logging levels without changing calling code, or to
182 * rename it to Logger if we do not need any specialization.
184 * \ingroup module_utility
189 //! Supported logging levels.
195 //! Number of logging levels.
196 static const int LogLevelCount
= static_cast<int>(Info
) + 1;
199 //! Creates a logger with the given targets.
200 explicit MDLogger(ILogTarget
*targets
[LogLevelCount
]);
202 //! For writing at LogLevel::Warning level.
203 LogLevelHelper warning
;
204 //! For writing at LogLevel::Info level.
209 * Helper to log information using gmx::MDLogger.
211 * \param logger LogLevelHelper instance to use for logging.
215 GMX_LOG(logger.warning).appendText(...);
217 * and ensures that the code to format the output is only executed when the
218 * output goes somewhere.
220 * See LogEntryWriter for functions that can be used with the macro (such as
221 * the appendText() in the example).
223 * \ingroup module_utility
225 #define GMX_LOG(logger) \
226 if (::gmx::LogWriteHelper helper = ::gmx::LogWriteHelper(logger)) { } else \
227 helper = ::gmx::LogEntryWriter()