f9f4f6d2759e69b5e95ae6f4ce575600dec1f7b5
[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, 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_ == NULL; }
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         void operator=(const LogEntryWriter &entryWriter)
138         {
139             target_->writeEntry(entryWriter.entry_);
140         }
141
142     private:
143         ILogTarget *target_;
144 };
145
146 /*! \libinternal \brief
147  * Represents a single logging level.
148  *
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.
151  *
152  * \ingroup module_utility
153  */
154 class LogLevelHelper
155 {
156     public:
157         //! Initializes a helper for writing to the given target.
158         explicit LogLevelHelper(ILogTarget *target) : target_(target) {}
159
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_ != NULL; }
163
164         //! Creates a helper for ::GMX_LOG.
165         operator LogWriteHelper() const { return LogWriteHelper(target_); }
166
167     private:
168         ILogTarget *target_;
169 };
170
171 /*! \libinternal \brief
172  * Declares a logging interface.
173  *
174  * Typically, this object is not created directly, but instead through
175  * LoggerBuilder.
176  *
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.
182  *
183  * \ingroup module_utility
184  */
185 class MDLogger
186 {
187     public:
188         //! Supported logging levels.
189         enum LogLevel
190         {
191             Warning,
192             Info
193         };
194         //! Number of logging levels.
195         static const int LogLevelCount = static_cast<int>(Info) + 1;
196
197         MDLogger();
198         //! Creates a logger with the given targets.
199         explicit MDLogger(ILogTarget *targets[LogLevelCount]);
200
201         //! For writing at LogLevel::Warning level.
202         LogLevelHelper warning;
203         //! For writing at LogLevel::Info level.
204         LogLevelHelper info;
205 };
206
207 /*! \brief
208  * Helper to log information using gmx::MDLogger.
209  *
210  * \param  logger  LogLevelHelper instance to use for logging.
211  *
212  * Used as
213  * \code
214    GMX_LOG(logger.warning).appendText(...);
215    \endcode
216  * and ensures that the code to format the output is only executed when the
217  * output goes somewhere.
218  *
219  * See LogEntryWriter for functions that can be used with the macro (such as
220  * the appendText() in the example).
221  *
222  * \ingroup module_utility
223  */
224 #define GMX_LOG(logger) \
225     if (::gmx::LogWriteHelper helper = ::gmx::LogWriteHelper(logger)) { } else \
226         helper = ::gmx::LogEntryWriter()
227
228 } // namespace gmx
229
230 #endif