Fix undefined behavior flagged by UBSAN
[gromacs.git] / src / gromacs / utility / loggerbuilder.h
blob5948a01e6c5c21ef1b6459ab302fee613ce60f9d
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2016,2018,2019,2020, 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
36 * \brief
37 * Declares functionality for initializing logging.
39 * See \ref page_logging for an overview of the functionality.
41 * \inlibraryapi
42 * \ingroup module_utility
44 #ifndef GMX_UTILITY_LOGGERBUILDER_H
45 #define GMX_UTILITY_LOGGERBUILDER_H
47 #include <memory>
48 #include <string>
50 #include "gromacs/utility/classhelpers.h"
51 #include "gromacs/utility/logger.h"
53 namespace gmx
56 class TextOutputStream;
58 class LoggerFormatterBuilder;
59 class LoggerOwner;
61 /*! \libinternal \brief
62 * Initializes loggers.
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.
70 * \ingroup module_utility
72 class LoggerBuilder
74 public:
75 LoggerBuilder();
76 ~LoggerBuilder();
78 /*! \brief
79 * Adds a stream to which log output is written.
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.
85 void addTargetStream(MDLogger::LogLevel level, TextOutputStream* stream);
86 /*! \brief
87 * Adds a file to which log output is written.
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.
93 void addTargetFile(MDLogger::LogLevel level, FILE* fp);
95 /*! \brief
96 * Builds the logger with the targets set for this builder.
98 * After this function has been called, the builder can (and should) be
99 * discarded.
101 LoggerOwner build();
103 private:
104 class Impl;
106 PrivateImplPointer<Impl> impl_;
109 /*! \libinternal \brief
110 * Manages memory for a logger built with LoggerBuilder.
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 keeping the actual logger simple
115 * and streamlined.
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.
121 * \ingroup module_utility
123 class LoggerOwner
125 public:
126 //! Move-constructs the owner.
127 LoggerOwner(LoggerOwner&& other) noexcept;
128 ~LoggerOwner();
130 //! Move-assings the owner.
131 LoggerOwner& operator=(LoggerOwner&& other) noexcept;
133 //! Returns the logger for writing the logs.
134 const MDLogger& logger() const { return *logger_; }
136 private:
137 class Impl;
139 LoggerOwner(std::unique_ptr<Impl> impl);
141 PrivateImplPointer<Impl> impl_;
142 const MDLogger* logger_;
144 friend class LoggerBuilder;
147 } // namespace gmx
149 #endif