Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / utility / loggerbuilder.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,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.
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 initializing 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_LOGGERBUILDER_H
45 #define GMX_UTILITY_LOGGERBUILDER_H
46
47 #include <memory>
48 #include <string>
49
50 #include "gromacs/utility/classhelpers.h"
51 #include "gromacs/utility/logger.h"
52
53 namespace gmx
54 {
55
56 class TextOutputStream;
57
58 class LoggerFormatterBuilder;
59 class LoggerOwner;
60
61 /*! \libinternal \brief
62  * Initializes loggers.
63  *
64  * This class provides methods for specifying logging targets for a logger and
65  * building the logger after all targets have been specified.  Having this
66  * separate from the logger allows using different internal data structures
67  * during initialization and operation, and simplifies the responsibilities of
68  * the involved classes.
69  *
70  * \ingroup module_utility
71  */
72 class LoggerBuilder
73 {
74 public:
75     LoggerBuilder();
76     ~LoggerBuilder();
77
78     /*! \brief
79      * Adds a stream to which log output is written.
80      *
81      * All output at level \p level or above it is written to \p stream.
82      * The caller is responsible of closing and freeing \p stream once the
83      * logger is discarded.
84      */
85     void addTargetStream(MDLogger::LogLevel level, TextOutputStream* stream);
86     /*! \brief
87      * Adds a file to which log output is written.
88      *
89      * All output at level \p level or above it is written to \p fp.
90      * The caller is responsible of closing \p fp once the logger is
91      * discarded.
92      */
93     void addTargetFile(MDLogger::LogLevel level, FILE* fp);
94
95     /*! \brief
96      * Builds the logger with the targets set for this builder.
97      *
98      * After this function has been called, the builder can (and should) be
99      * discarded.
100      */
101     LoggerOwner build();
102
103 private:
104     class Impl;
105
106     PrivateImplPointer<Impl> impl_;
107 };
108
109 /*! \libinternal \brief
110  * Manages memory for a logger built with LoggerBuilder.
111  *
112  * This class is responsible of managing all memory allocated by LoggerBuilder
113  * that is needed for operation of the actual logger.  Also the actual logger
114  * instance is owned by this class.  This allows keeing the actual logger simple
115  * and streamlined.
116  *
117  * This class supports move construction and assignment, which allows
118  * initializing it on the stack and assigning a new instance if the targets
119  * need to be changed.
120  *
121  * \ingroup module_utility
122  */
123 class LoggerOwner
124 {
125 public:
126     //! Move-constructs the owner.
127     LoggerOwner(LoggerOwner&& other) noexcept;
128     ~LoggerOwner();
129
130     //! Move-assings the owner.
131     LoggerOwner& operator=(LoggerOwner&& other) noexcept;
132
133     //! Returns the logger for writing the logs.
134     const MDLogger& logger() const { return *logger_; }
135
136 private:
137     class Impl;
138
139     LoggerOwner(std::unique_ptr<Impl> impl);
140
141     PrivateImplPointer<Impl> impl_;
142     const MDLogger*          logger_;
143
144     friend class LoggerBuilder;
145 };
146
147 } // namespace gmx
148
149 #endif