Skip to content
This repository was archived by the owner on Aug 5, 2022. It is now read-only.

Commit 85679b3

Browse files
committed
Fix ILogger::operator<< when called on list<string>
Calling info() << someList should be identical to calling info() << element on each element of someList. This was not the case because when used on a list, the string was concatenated with \n and the logging callback called once. Instead, the logging callback is now called for each line. Fixes #125. Signed-off-by: David Wagner <david.wagner@intel.com>
1 parent b81f4a5 commit 85679b3

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

parameter/log/include/log/LogWrapper.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,53 +71,56 @@ class LogWrapper
7171
/** Class destructor */
7272
~LogWrapper()
7373
{
74-
if (!mLog.str().empty()) {
75-
if(isWarning) {
76-
mLogger.warning(mProlog + mLog.str());
77-
} else {
78-
mLogger.info(mProlog + mLog.str());
79-
}
74+
auto logfunc = isWarning ? &ILogger::warning : &ILogger::info;
75+
76+
for (auto& log : mLogs) {
77+
(mLogger.*logfunc)(mProlog + log.str());
8078
}
8179
}
8280

8381
/**
8482
* Simulate stream behaviour
8583
*
84+
* Both operator<<() versions must not be used in conjunction: either use
85+
* the 'scalar' version (this one) or the 'list' version but not both on
86+
* the same object.
87+
*
8688
* @tparam T the type of the information to log
8789
* @param[in] log the information to log
8890
*/
8991
template <class T>
9092
LogWrapper& operator<<(const T &log)
9193
{
92-
mLog << log;
94+
if (mLogs.empty()) {
95+
mLogs.emplace_back();
96+
}
97+
98+
mLogs.back() << log;
9399
return *this;
94100
}
95101

96102
/**
97103
* Simulate stream behaviour for string list
98104
*
105+
* Both operator<<() versions must not be used in conjunction: either use
106+
* the 'scalar' version or the 'list' version (this one) but not both on
107+
* the same object.
108+
*
99109
* @param[in] logs list of information to log
100110
*/
101111
LogWrapper& operator<<(const std::list<std::string>& logs)
102112
{
103-
std::string formatedLogs;
104-
std::string separator = "\n" + mProlog;
105-
CUtility::asString(logs, formatedLogs, separator);
106-
107-
// Check if there is something in the log to know if we have to add a prefix
108-
if (!mLog.str().empty() && mLog.str()[mLog.str().length()-1] == separator[0]) {
109-
*this << mProlog;
113+
for (auto& log : logs) {
114+
mLogs.emplace_back(log);
110115
}
111-
112-
*this << formatedLogs;
113116
return *this;
114117
}
115118

116119
private:
117120
LogWrapper& operator=(const LogWrapper&);
118121

119122
/** Log stream holder */
120-
std::ostringstream mLog;
123+
std::list<std::ostringstream> mLogs;
121124

122125
/** Wrapped logger */
123126
ILogger& mLogger;

0 commit comments

Comments
 (0)