Enable clang-tidy for headers
[alexxy/gromacs.git] / src / gromacs / utility / logger.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \libinternal \file
36  * \brief
37  * Declares functionality for logging.
38  *
39  * See \ref page_logging for an overview of the functionality.
40  *
41  * \inlibraryapi
42  * \ingroup module_utility
43  */
44 #ifndef GMX_UTILITY_LOGGER_H
45 #define GMX_UTILITY_LOGGER_H
46
47 #include <string>
48
49 namespace gmx
50 {
51
52 struct LogEntry
53 {
54     LogEntry() : asParagraph(false) {}
55
56     std::string text;
57     bool        asParagraph;
58 };
59
60 /*! \libinternal \brief
61  * Target where log output can be written.
62  *
63  * \ingroup module_utility
64  */
65 class ILogTarget
66 {
67     public:
68         virtual ~ILogTarget();
69
70         //! Writes a log entry to this target.
71         virtual void writeEntry(const LogEntry &entry) = 0;
72 };
73
74 /*! \libinternal \brief
75  * Helper class for creating log entries with ::GMX_LOG.
76  *
77  * \ingroup module_utility
78  */
79 class LogEntryWriter
80 {
81     public:
82         //! Appends given text to the log entry.
83         LogEntryWriter &appendText(const char *text)
84         {
85             entry_.text.append(text);
86             return *this;
87         }
88         //! Appends given text to the log entry.
89         LogEntryWriter &appendText(const std::string &text)
90         {
91             entry_.text.append(text);
92             return *this;
93         }
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()
98         {
99             entry_.asParagraph = true;
100             return *this;
101         }
102
103     private:
104         LogEntry    entry_;
105
106         friend class LogWriteHelper;
107 };
108
109 /*! \internal \brief
110  * Helper class for implementing ::GMX_LOG.
111  *
112  * \ingroup module_utility
113  */
114 class LogWriteHelper
115 {
116     public:
117         //! Initializes a helper for writing to the given target.
118         explicit LogWriteHelper(ILogTarget *target) : target_(target) {}
119
120         // Should be explicit, once that works in CUDA.
121         /*! \brief
122          * Returns whether anything needs to be written.
123          *
124          * Note that the return value is unintuitively `false` when the target
125          * is active, to allow implementing ::GMX_LOG like it is now.
126          */
127         operator bool() const { return target_ == nullptr; }
128
129         /*! \brief
130          * Writes the entry from the given writer to the log target.
131          *
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).
136          */
137         LogWriteHelper &operator=(const LogEntryWriter &entryWriter)
138         {
139             target_->writeEntry(entryWriter.entry_);
140             return *this;
141         }
142
143     private:
144         ILogTarget *target_;
145 };
146
147 /*! \libinternal \brief
148  * Represents a single logging level.
149  *
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.
152  *
153  * \ingroup module_utility
154  */
155 class LogLevelHelper
156 {
157     public:
158         //! Initializes a helper for writing to the given target.
159         explicit LogLevelHelper(ILogTarget *target) : target_(target) {}
160
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; }
164
165         //! Creates a helper for ::GMX_LOG.
166         operator LogWriteHelper() const { return LogWriteHelper(target_); }
167
168     private:
169         ILogTarget *target_;
170 };
171
172 /*! \libinternal \brief
173  * Declares a logging interface.
174  *
175  * Typically, this object is not created directly, but instead through
176  * LoggerBuilder.
177  *
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.
183  *
184  * \ingroup module_utility
185  */
186 class MDLogger
187 {
188     public:
189         //! Supported logging levels.
190         enum LogLevel
191         {
192             Warning,
193             Info
194         };
195         //! Number of logging levels.
196         static const int LogLevelCount = static_cast<int>(Info) + 1;
197
198         MDLogger();
199         //! Creates a logger with the given targets.
200         explicit MDLogger(ILogTarget *targets[LogLevelCount]);
201
202         //! For writing at LogLevel::Warning level.
203         LogLevelHelper warning;
204         //! For writing at LogLevel::Info level.
205         LogLevelHelper info;
206 };
207
208 /*! \brief
209  * Helper to log information using gmx::MDLogger.
210  *
211  * \param  logger  LogLevelHelper instance to use for logging.
212  *
213  * Used as
214  * \code
215    GMX_LOG(logger.warning).appendText(...);
216    \endcode
217  * and ensures that the code to format the output is only executed when the
218  * output goes somewhere.
219  *
220  * See LogEntryWriter for functions that can be used with the macro (such as
221  * the appendText() in the example).
222  *
223  * \ingroup module_utility
224  */
225 #define GMX_LOG(logger) \
226     if (::gmx::LogWriteHelper helper = ::gmx::LogWriteHelper(logger)) { } else \
227         helper = ::gmx::LogEntryWriter()
228
229 } // namespace gmx
230
231 #endif