3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
9 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
10 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
11 * Copyright (c) 2001-2009, The GROMACS development team,
12 * check out http://www.gromacs.org for more information.
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version 2
17 * of the License, or (at your option) any later version.
19 * If you want to redistribute modifications, please consider that
20 * scientific software is very special. Version control is crucial -
21 * bugs must be traceable. We will be happy to consider code for
22 * inclusion in the official distribution, but derived work must not
23 * be called official GROMACS. Details are found in the README & COPYING
24 * files - if they are missing, get the official version at www.gromacs.org.
26 * To help us fund GROMACS development, we humbly ask that you cite
27 * the papers on the package - you can find them in the top README file.
29 * For more info, check our website at http://www.gromacs.org
33 * Implements gmx::MessageStringCollector.
35 * \author Teemu Murtola <teemu.murtola@cbr.su.se>
36 * \ingroup module_utility
38 #include "gromacs/utility/messagestringcollector.h"
42 #include "gromacs/utility/gmxassert.h"
47 class MessageStringCollector::Impl
50 Impl() : prevContext_(0) {}
52 std::vector<std::string> contexts_;
57 MessageStringCollector::MessageStringCollector()
62 MessageStringCollector::~MessageStringCollector()
66 void MessageStringCollector::startContext(const char *name)
68 impl_->contexts_.push_back(name);
71 void MessageStringCollector::append(const std::string &message)
73 int indent = static_cast<int>(impl_->prevContext_ * 2);
74 if (!impl_->contexts_.empty())
76 std::vector<std::string>::const_iterator ci;
77 for (ci = impl_->contexts_.begin() + impl_->prevContext_;
78 ci != impl_->contexts_.end(); ++ci)
80 impl_->text_.append(indent, ' ');
81 impl_->text_.append(*ci);
82 impl_->text_.append("\n");
86 impl_->prevContext_ = impl_->contexts_.size();
88 // TODO: Put this into a more generic helper, could be useful elsewhere
90 while (pos < message.size())
92 size_t nextpos = message.find_first_of('\n', pos);
93 impl_->text_.append(indent, ' ');
94 impl_->text_.append(message.substr(pos, nextpos - pos));
95 impl_->text_.append("\n");
96 if (nextpos == std::string::npos)
104 void MessageStringCollector::finishContext()
106 GMX_RELEASE_ASSERT(!impl_->contexts_.empty(),
107 "finishContext() called without context");
108 impl_->contexts_.pop_back();
109 if (impl_->prevContext_ > impl_->contexts_.size())
111 impl_->prevContext_ = impl_->contexts_.size();
115 void MessageStringCollector::clear()
117 impl_->contexts_.clear();
118 impl_->text_.clear();
119 impl_->prevContext_ = 0;
122 bool MessageStringCollector::isEmpty() const
124 return impl_->text_.empty();
127 std::string MessageStringCollector::toString() const