Skip to content

Commit 070fac0

Browse files
committedJan 6, 2016
Add LogDump methods to lldb_private::StringList.
This patch eases the printing of iterable string containers. Author: Luke Drummond <luke.drummond@codeplay.com> Differential Revision: http://reviews.llvm.org/D15773 llvm-svn: 256927
1 parent b38c32b commit 070fac0

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
 

‎lldb/include/lldb/Core/StringList.h

+24
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,16 @@ class StringList
132132
StringList&
133133
operator << (const char* str);
134134

135+
StringList&
136+
operator << (const std::string &s);
137+
135138
StringList&
136139
operator << (StringList strings);
137140

141+
// Copy assignment for a vector of strings
142+
StringList&
143+
operator = (const std::vector<std::string> &rhs);
144+
138145
// This string list contains a list of valid auto completion
139146
// strings, and the "s" is passed in. "matches" is filled in
140147
// with zero or more string values that start with "s", and
@@ -147,6 +154,23 @@ class StringList
147154
StringList &matches,
148155
size_t &exact_matches_idx) const;
149156

157+
// Dump the StringList to the given lldb_private::Log, `log`, one item per line.
158+
// If given, `name` will be used to identify the start and end of the list in the output.
159+
virtual void LogDump(Log *log, const char *name = nullptr);
160+
161+
// Static helper to convert an iterable of strings to a StringList, and then
162+
// dump it with the semantics of the `LogDump` method.
163+
template<typename T> static void LogDump(Log *log, T s_iterable, const char *name = nullptr)
164+
{
165+
if (!log)
166+
return;
167+
// Make a copy of the iterable as a StringList
168+
StringList l{};
169+
for (const auto &s : s_iterable)
170+
l << s;
171+
172+
l.LogDump(log, name);
173+
}
150174
private:
151175
STLStringArray m_strings;
152176
};

‎lldb/source/Core/StringList.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "lldb/Core/StreamString.h"
1313
#include "lldb/Host/FileSpec.h"
14+
#include "lldb/Core/Log.h"
15+
#include "lldb/Core/StreamString.h"
1416

1517
#include <string>
1618

@@ -304,13 +306,30 @@ StringList::operator << (const char* str)
304306
return *this;
305307
}
306308

309+
StringList&
310+
StringList::operator << (const std::string& str)
311+
{
312+
AppendString(str);
313+
return *this;
314+
}
315+
307316
StringList&
308317
StringList::operator << (StringList strings)
309318
{
310319
AppendList(strings);
311320
return *this;
312321
}
313322

323+
StringList&
324+
StringList::operator = (const std::vector<std::string> &rhs)
325+
{
326+
Clear();
327+
for (const auto &s : rhs)
328+
m_strings.push_back(s);
329+
330+
return *this;
331+
}
332+
314333
size_t
315334
StringList::AutoComplete (const char *s, StringList &matches, size_t &exact_idx) const
316335
{
@@ -339,3 +358,21 @@ StringList::AutoComplete (const char *s, StringList &matches, size_t &exact_idx)
339358
return matches.GetSize();
340359
}
341360

361+
void
362+
StringList::LogDump(Log *log, const char *name)
363+
{
364+
if (!log)
365+
return;
366+
367+
StreamString strm;
368+
if (name)
369+
strm.Printf("Begin %s:\n", name);
370+
for (const auto &s : m_strings) {
371+
strm.Indent();
372+
strm.Printf("%s\n", s.c_str());
373+
}
374+
if (name)
375+
strm.Printf("End %s.\n", name);
376+
377+
log->Debug("%s", strm.GetData());
378+
}

0 commit comments

Comments
 (0)
Please sign in to comment.