1 /*
2 * Copyright (c) 2015, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30 #include <ParameterMgrFullConnector.h>
31 #include <vector>
32 #include <utility>
33 #include <string>
34 #include <algorithm>
35 #include <sstream>
36 #include <iterator>
37
38 namespace parameterFramework
39 {
40
41 /** Logger that stores all fed log in order retrieve them asynchronously.
42 * Compatible with the ParameterFramework::Ilogger api.
43 * Usually used in tests to inspect what was logged by a PF instances
44 * (eg: test if a warring occurred).
45 */
46 class StoreLogger : public CParameterMgrFullConnector::ILogger
47 {
48 public:
49 struct Log
50 {
51 enum class Level
52 {
53 info,
54 warning
55 };
56 Level level;
57 std::string msg;
operator ==parameterFramework::StoreLogger::Log58 bool operator==(const Log &other) const
59 {
60 return level == other.level and msg == other.msg;
61 }
62 };
63 using Logs = std::vector<Log>;
64
warning(const std::string & strLog)65 void warning(const std::string &strLog) override
66 {
67 logs.push_back({Log::Level::warning, strLog});
68 }
info(const std::string & strLog)69 void info(const std::string &strLog) override { logs.push_back({Log::Level::info, strLog}); }
70
getLogs() const71 const Logs &getLogs() const { return logs; }
72
filter(Log::Level level) const73 Logs filter(Log::Level level) const
74 {
75 return filter([&level](const Log &log) { return log.level == level; });
76 };
77
match(const std::string & pattern) const78 Logs match(const std::string &pattern) const
79 {
80 return filter(
81 [&pattern](const Log &log) { return log.msg.find(pattern) == std::string::npos; });
82 }
83
84 private:
85 template <class Predicate>
filter(Predicate predicate) const86 Logs filter(Predicate predicate) const
87 {
88 Logs filtered;
89 std::copy_if(logs.begin(), logs.end(), std::back_inserter(filtered), predicate);
90 return filtered;
91 }
92
93 Logs logs;
94 };
95
96 /** Overload input stream operator to pretty print a StoreLogger::Log::Level. */
operator <<(std::ostream & os,const StoreLogger::Log::Level & level)97 std::ostream &operator<<(std::ostream &os, const StoreLogger::Log::Level &level)
98 {
99 auto levelStr = "UNREACHABLE";
100 using L = StoreLogger::Log::Level;
101 switch (level) {
102 case L::info:
103 levelStr = "Info";
104 break;
105 case L::warning:
106 levelStr = "Warn";
107 break;
108 }
109 return os << levelStr << ": ";
110 }
111
112 /** Overload input stream operator to pretty print a StoreLogger::Log. */
operator <<(std::ostream & os,const StoreLogger::Log & log)113 std::ostream &operator<<(std::ostream &os, const StoreLogger::Log &log)
114 {
115 return os << log.level << log.msg << std::endl;
116 }
117
118 } // namespace parameterFramework
119