AlexaClientSDK  1.25.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
LogEntry.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LOGGER_LOGENTRY_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LOGGER_LOGENTRY_H_
18 
19 #include <algorithm>
20 #include <functional>
21 #include <sstream>
22 #include <string>
23 
25 
26 namespace alexaClientSDK {
27 namespace avsCommon {
28 namespace utils {
29 namespace logger {
30 
32 class LogEntry {
33 public:
40  LogEntry(const std::string& source, const char* event);
41 
48  LogEntry(const std::string& source, const std::string& event);
49 
57  LogEntry& d(const std::string& key, const char* value);
58 
66  LogEntry& d(const char* key, char* value);
67 
74  LogEntry& d(const char* key, const char* value);
75 
83  LogEntry& d(const std::string& key, const std::string& value);
84 
92  LogEntry& d(const char* key, const std::string& value);
93 
101  LogEntry& d(const std::string& key, bool value);
102 
110  LogEntry& d(const char* key, bool value);
111 
119  template <typename ValueType>
120  inline LogEntry& d(const char* key, const ValueType& value);
121 
130  template <typename ValueType>
131  inline LogEntry& sensitive(const char* key, const ValueType& value);
132 
143  inline LogEntry& obfuscatePrivateData(const char* key, const std::string& value);
144 
152  LogEntry& m(const char* message);
153 
161  LogEntry& m(const std::string& message);
162 
171  template <typename PtrType>
172  LogEntry& p(const char* key, const std::shared_ptr<PtrType>& ptr);
173 
182  LogEntry& p(const char* key, void* ptr);
183 
190  const char* c_str() const;
191 
192 private:
194  void prefixKeyValuePair();
195 
197  void prefixMessage();
198 
200  static std::vector<std::string> getPrivateLabelDenyList() {
201  static std::vector<std::string> privateLabelDenyList = {"ssid"};
202  return privateLabelDenyList;
203  }
204 
213  void appendEscapedString(const char* in);
214 
216  static const char KEY_VALUE_SEPARATOR = '=';
217 
219  bool m_hasMetadata;
220 
222  LogEntryStream m_stream;
223 };
224 
225 template <typename ValueType>
226 LogEntry& LogEntry::d(const char* key, const ValueType& value) {
227  prefixKeyValuePair();
228  m_stream << key << KEY_VALUE_SEPARATOR << value;
229  return *this;
230 }
231 
232 template <typename PtrType>
233 LogEntry& LogEntry::p(const char* key, const std::shared_ptr<PtrType>& ptr) {
234  return d(key, ptr.get());
235 }
236 
237 // Define ACSDK_EMIT_SENSITIVE_LOGS if you want to include sensitive data in log output.
238 #ifdef ACSDK_EMIT_SENSITIVE_LOGS
239 
240 template <typename ValueType>
241 LogEntry& LogEntry::sensitive(const char* key, const ValueType& value) {
242  return d(key, value);
243 }
244 #else
245 template <typename ValueType>
246 LogEntry& LogEntry::sensitive(const char*, const ValueType&) {
247  return *this;
248 }
249 #endif
250 
251 LogEntry& LogEntry::obfuscatePrivateData(const char* key, const std::string& value) {
252  // if value contains any private label, obfuscate the section after the label
253  // since it can (but shouldn't) contain multiple, obfuscate from the earliest one found onward
254  auto firstPosition = value.length();
255 
256  for (auto privateLabel : getPrivateLabelDenyList()) {
257  auto it = std::search(
258  value.begin(),
259  value.end(),
260  privateLabel.begin(),
261  privateLabel.end(),
262  [](char valueChar, char denyListChar) { return std::tolower(valueChar) == std::tolower(denyListChar); });
263  if (it != value.end()) {
264  // capture the least value
265  auto thisPosition = std::distance(value.begin(), it) + privateLabel.length();
266  if (thisPosition < firstPosition) {
267  firstPosition = thisPosition;
268  }
269  }
270  }
271 
272  if (firstPosition <= value.length()) {
273  // hash everything after the label itself
274  auto labelPart = value.substr(0, firstPosition);
275  auto obfuscatedPart = std::to_string(std::hash<std::string>{}(value.substr(firstPosition)));
276  return d(key, labelPart + obfuscatedPart);
277  }
278  return d(key, value);
279 }
280 
281 } // namespace logger
282 } // namespace utils
283 } // namespace avsCommon
284 } // namespace alexaClientSDK
285 
286 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LOGGER_LOGENTRY_H_
LogEntry & p(const char *key, const std::shared_ptr< PtrType > &ptr)
Definition: LogEntry.h:233
const char * c_str() const
Definition: LogEntry.cpp:119
LogEntry & obfuscatePrivateData(const char *key, const std::string &value)
Definition: LogEntry.h:251
LogEntry & sensitive(const char *key, const ValueType &value)
Definition: LogEntry.h:246
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
LogEntry & d(const std::string &key, const char *value)
Definition: LogEntry.cpp:67
Definition: LogEntryStream.h:31
LogEntry & m(const char *message)
Definition: LogEntry.cpp:101
LogEntry(const std::string &source, const char *event)
Definition: LogEntry.cpp:56
static const std::string key
The database key to be used by the protocol given the METADATA object.
Definition: SharedAVSSettingProtocolTest.cpp:58
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:32

AlexaClientSDK 1.25.0 - Copyright 2016-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0