Write TPR body as opaque XDR data in big-endian format
[gromacs.git] / src / gromacs / utility / loggerbuilder.cpp
blob91e366f2f0af6bb345fe64c0ef40865845213048
1 /*
2 * This file is part of the GROMACS molecular simulation package.
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.
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 #include "gmxpre.h"
37 #include "loggerbuilder.h"
39 #include <memory>
40 #include <vector>
42 #include "gromacs/utility/filestream.h"
43 #include "gromacs/utility/logger.h"
44 #include "gromacs/utility/stringutil.h"
45 #include "gromacs/utility/textwriter.h"
47 namespace gmx
50 class LogTargetCollection : public ILogTarget
52 public:
53 void addTarget(ILogTarget* target) { targets_.push_back(target); }
55 void writeEntry(const LogEntry& entry) override
57 for (ILogTarget* target : targets_)
59 target->writeEntry(entry);
63 private:
64 std::vector<ILogTarget*> targets_;
67 class LogTargetFormatter : public ILogTarget
69 public:
70 explicit LogTargetFormatter(TextOutputStream* stream) : writer_(stream) {}
72 void writeEntry(const LogEntry& entry) override;
74 private:
75 TextWriter writer_;
79 void LogTargetFormatter::writeEntry(const LogEntry& entry)
81 if (entry.asParagraph)
83 writer_.ensureEmptyLine();
85 writer_.writeLine(entry.text);
86 if (entry.asParagraph)
88 writer_.ensureEmptyLine();
92 /********************************************************************
93 * LoggerOwner::Impl
96 class LoggerOwner::Impl
98 public:
99 explicit Impl(ILogTarget* loggerTargets[MDLogger::LogLevelCount]) : logger_(loggerTargets) {}
101 MDLogger logger_;
102 std::vector<std::unique_ptr<TextOutputStream>> streams_;
103 std::vector<std::unique_ptr<ILogTarget>> targets_;
106 /********************************************************************
107 * LoggerOwner
110 LoggerOwner::LoggerOwner(std::unique_ptr<Impl> impl) :
111 impl_(impl.release()),
112 logger_(&impl_->logger_)
116 LoggerOwner::LoggerOwner(LoggerOwner&& other) noexcept :
117 impl_(std::move(other.impl_)),
118 logger_(&impl_->logger_)
122 LoggerOwner& LoggerOwner::operator=(LoggerOwner&& other) noexcept
124 impl_ = std::move(other.impl_);
125 logger_ = &impl_->logger_;
126 return *this;
129 LoggerOwner::~LoggerOwner() {}
131 /********************************************************************
132 * LoggerBuilder::Impl
135 class LoggerBuilder::Impl
137 public:
138 std::vector<std::unique_ptr<TextOutputStream>> streams_;
139 std::vector<std::unique_ptr<ILogTarget>> targets_;
140 std::vector<ILogTarget*> loggerTargets_[MDLogger::LogLevelCount];
143 /********************************************************************
144 * LoggerBuilder
147 LoggerBuilder::LoggerBuilder() : impl_(new Impl) {}
149 LoggerBuilder::~LoggerBuilder() {}
151 void LoggerBuilder::addTargetStream(MDLogger::LogLevel level, TextOutputStream* stream)
153 impl_->targets_.push_back(std::unique_ptr<ILogTarget>(new LogTargetFormatter(stream)));
154 ILogTarget* target = impl_->targets_.back().get();
155 for (int i = 0; i <= static_cast<int>(level); ++i)
157 impl_->loggerTargets_[i].push_back(target);
161 void LoggerBuilder::addTargetFile(MDLogger::LogLevel level, FILE* fp)
163 std::unique_ptr<TextOutputStream> stream(new TextOutputFile(fp));
164 addTargetStream(level, stream.get());
165 impl_->streams_.push_back(std::move(stream));
168 LoggerOwner LoggerBuilder::build()
170 ILogTarget* loggerTargets[MDLogger::LogLevelCount];
171 for (int i = 0; i < MDLogger::LogLevelCount; ++i)
173 auto& levelTargets = impl_->loggerTargets_[i];
174 loggerTargets[i] = nullptr;
175 if (!levelTargets.empty())
177 if (levelTargets.size() == 1)
179 loggerTargets[i] = levelTargets[0];
181 else
183 std::unique_ptr<LogTargetCollection> collection(new LogTargetCollection);
184 for (auto& target : levelTargets)
186 collection->addTarget(target);
188 loggerTargets[i] = collection.get();
189 impl_->targets_.push_back(std::move(collection));
192 levelTargets.clear();
194 std::unique_ptr<LoggerOwner::Impl> data(new LoggerOwner::Impl(loggerTargets));
195 data->targets_ = std::move(impl_->targets_);
196 data->streams_ = std::move(impl_->streams_);
197 return LoggerOwner(std::move(data));
200 } // namespace gmx