2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2017, 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 void operator=(const LogEntryWriter &entryWriter)
139 target_->writeEntry(entryWriter.entry_);
146 /*! \libinternal \brief
147 * Represents a single logging level.
149 * Typically this type is not used directly, but instances in MDLogger are
150 * simply accessed through ::GMX_LOG in code that writes to the log.
152 * \ingroup module_utility
157 //! Initializes a helper for writing to the given target.
158 explicit LogLevelHelper(ILogTarget *target) : target_(target) {}
160 // Both of the below should be explicit, once that works in CUDA.
161 //! Returns whether the output for this log level goes anywhere.
162 operator bool() const { return target_ != nullptr; }
164 //! Creates a helper for ::GMX_LOG.
165 operator LogWriteHelper() const { return LogWriteHelper(target_); }
171 /*! \libinternal \brief
172 * Declares a logging interface.
174 * Typically, this object is not created directly, but instead through
177 * For now, this is named MDLogger, since it is used only there, and it is not
178 * clear whether the logging levels can be the same throughout the code. It
179 * should be relatively straightforward to split this into multiple classes
180 * with different supported logging levels without changing calling code, or to
181 * rename it to Logger if we do not need any specialization.
183 * \ingroup module_utility
188 //! Supported logging levels.
194 //! Number of logging levels.
195 static const int LogLevelCount = static_cast<int>(Info) + 1;
198 //! Creates a logger with the given targets.
199 explicit MDLogger(ILogTarget *targets[LogLevelCount]);
201 //! For writing at LogLevel::Warning level.
202 LogLevelHelper warning;
203 //! For writing at LogLevel::Info level.
208 * Helper to log information using gmx::MDLogger.
210 * \param logger LogLevelHelper instance to use for logging.
214 GMX_LOG(logger.warning).appendText(...);
216 * and ensures that the code to format the output is only executed when the
217 * output goes somewhere.
219 * See LogEntryWriter for functions that can be used with the macro (such as
220 * the appendText() in the example).
222 * \ingroup module_utility
224 #define GMX_LOG(logger) \
225 if (::gmx::LogWriteHelper helper = ::gmx::LogWriteHelper(logger)) { } else \
226 helper = ::gmx::LogEntryWriter()