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