Apply clang-format-11
[alexxy/gromacs.git] / src / gromacs / utility / loggerbuilder.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2016,2018,2019,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 #include "gmxpre.h"
36
37 #include "loggerbuilder.h"
38
39 #include <memory>
40 #include <vector>
41
42 #include "gromacs/utility/filestream.h"
43 #include "gromacs/utility/logger.h"
44 #include "gromacs/utility/stringutil.h"
45 #include "gromacs/utility/textwriter.h"
46
47 namespace gmx
48 {
49
50 class LogTargetCollection : public ILogTarget
51 {
52 public:
53     void addTarget(ILogTarget* target) { targets_.push_back(target); }
54
55     void writeEntry(const LogEntry& entry) override
56     {
57         for (ILogTarget* target : targets_)
58         {
59             target->writeEntry(entry);
60         }
61     }
62
63 private:
64     std::vector<ILogTarget*> targets_;
65 };
66
67 class LogTargetFormatter : public ILogTarget
68 {
69 public:
70     explicit LogTargetFormatter(TextOutputStream* stream) : writer_(stream) {}
71
72     void writeEntry(const LogEntry& entry) override;
73
74 private:
75     TextWriter writer_;
76 };
77
78
79 void LogTargetFormatter::writeEntry(const LogEntry& entry)
80 {
81     if (entry.asParagraph)
82     {
83         writer_.ensureEmptyLine();
84     }
85     writer_.writeLine(entry.text);
86     if (entry.asParagraph)
87     {
88         writer_.ensureEmptyLine();
89     }
90 }
91
92 /********************************************************************
93  * LoggerOwner::Impl
94  */
95
96 class LoggerOwner::Impl
97 {
98 public:
99     explicit Impl(ILogTarget* loggerTargets[MDLogger::LogLevelCount]) : logger_(loggerTargets) {}
100
101     MDLogger                                       logger_;
102     std::vector<std::unique_ptr<TextOutputStream>> streams_;
103     std::vector<std::unique_ptr<ILogTarget>>       targets_;
104 };
105
106 /********************************************************************
107  * LoggerOwner
108  */
109
110 LoggerOwner::LoggerOwner(std::unique_ptr<Impl> impl) :
111     impl_(impl.release()), logger_(&impl_->logger_)
112 {
113 }
114
115 LoggerOwner::LoggerOwner(LoggerOwner&& other) noexcept :
116     impl_(std::move(other.impl_)), logger_(&impl_->logger_)
117 {
118 }
119
120 LoggerOwner& LoggerOwner::operator=(LoggerOwner&& other) noexcept
121 {
122     impl_   = std::move(other.impl_);
123     logger_ = &impl_->logger_;
124     return *this;
125 }
126
127 LoggerOwner::~LoggerOwner() {}
128
129 /********************************************************************
130  * LoggerBuilder::Impl
131  */
132
133 class LoggerBuilder::Impl
134 {
135 public:
136     std::vector<std::unique_ptr<TextOutputStream>> streams_;
137     std::vector<std::unique_ptr<ILogTarget>>       targets_;
138     std::vector<ILogTarget*>                       loggerTargets_[MDLogger::LogLevelCount];
139 };
140
141 /********************************************************************
142  * LoggerBuilder
143  */
144
145 LoggerBuilder::LoggerBuilder() : impl_(new Impl) {}
146
147 LoggerBuilder::~LoggerBuilder() {}
148
149 void LoggerBuilder::addTargetStream(MDLogger::LogLevel level, TextOutputStream* stream)
150 {
151     impl_->targets_.push_back(std::unique_ptr<ILogTarget>(new LogTargetFormatter(stream)));
152     ILogTarget* target = impl_->targets_.back().get();
153     for (int i = 0; i <= static_cast<int>(level); ++i)
154     {
155         impl_->loggerTargets_[i].push_back(target);
156     }
157 }
158
159 void LoggerBuilder::addTargetFile(MDLogger::LogLevel level, FILE* fp)
160 {
161     std::unique_ptr<TextOutputStream> stream(new TextOutputFile(fp));
162     addTargetStream(level, stream.get());
163     impl_->streams_.push_back(std::move(stream));
164 }
165
166 LoggerOwner LoggerBuilder::build()
167 {
168     ILogTarget* loggerTargets[MDLogger::LogLevelCount];
169     for (int i = 0; i < MDLogger::LogLevelCount; ++i)
170     {
171         auto& levelTargets = impl_->loggerTargets_[i];
172         loggerTargets[i]   = nullptr;
173         if (!levelTargets.empty())
174         {
175             if (levelTargets.size() == 1)
176             {
177                 loggerTargets[i] = levelTargets[0];
178             }
179             else
180             {
181                 std::unique_ptr<LogTargetCollection> collection(new LogTargetCollection);
182                 for (auto& target : levelTargets)
183                 {
184                     collection->addTarget(target);
185                 }
186                 loggerTargets[i] = collection.get();
187                 impl_->targets_.push_back(std::move(collection));
188             }
189         }
190         levelTargets.clear();
191     }
192     std::unique_ptr<LoggerOwner::Impl> data(new LoggerOwner::Impl(loggerTargets));
193     data->targets_ = std::move(impl_->targets_);
194     data->streams_ = std::move(impl_->streams_);
195     return LoggerOwner(std::move(data));
196 }
197
198 } // namespace gmx