1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chromeos/services/assistant/assistant_interaction_logger.h"
6 
7 #include <utility>
8 
9 #include "chromeos/services/assistant/public/cpp/features.h"
10 
11 namespace chromeos {
12 namespace assistant {
13 
14 namespace {
15 
ResolutionToString(AssistantInteractionResolution resolution)16 std::string ResolutionToString(AssistantInteractionResolution resolution) {
17   std::stringstream result;
18   result << static_cast<int>(resolution);
19   return result.str();
20 }
21 
IsPIILoggingAllowed()22 bool IsPIILoggingAllowed() {
23   return features::IsAssistantDebuggingEnabled();
24 }
25 
HidePiiMaybe(const std::string & value)26 std::string HidePiiMaybe(const std::string& value) {
27   if (IsPIILoggingAllowed())
28     return "[PII](" + value + ")";
29   else
30     return "[Redacted PII]";
31 }
32 
33 #define LOG_INTERACTION() \
34   LOG_INTERACTION_AT_LEVEL(AssistantInteractionLogger::kVLogLevel)
35 
36 #define LOG_INTERACTION_AT_LEVEL(_level) \
37   VLOG(_level) << "Assistant: " << __func__ << ": "
38 
39 }  // namespace
40 
IsLoggingEnabled()41 bool AssistantInteractionLogger::IsLoggingEnabled() {
42   return VLOG_IS_ON(kVLogLevel);
43 }
44 
45 AssistantInteractionLogger::AssistantInteractionLogger() = default;
46 
47 AssistantInteractionLogger::~AssistantInteractionLogger() = default;
48 
OnInteractionStarted(const AssistantInteractionMetadata & metadata)49 void AssistantInteractionLogger::OnInteractionStarted(
50     const AssistantInteractionMetadata& metadata) {
51   switch (metadata.type) {
52     case AssistantInteractionType::kText:
53       LOG_INTERACTION() << "Text interaction with query "
54                         << HidePiiMaybe(metadata.query);
55       break;
56     case AssistantInteractionType::kVoice:
57       LOG_INTERACTION() << "Voice interaction";
58       break;
59   }
60 }
61 
OnInteractionFinished(AssistantInteractionResolution resolution)62 void AssistantInteractionLogger::OnInteractionFinished(
63     AssistantInteractionResolution resolution) {
64   LOG_INTERACTION() << "with resolution " << ResolutionToString(resolution);
65 }
66 
OnHtmlResponse(const std::string & response,const std::string & fallback)67 void AssistantInteractionLogger::OnHtmlResponse(const std::string& response,
68                                                 const std::string& fallback) {
69   // Displaying fallback instead of the response as the response is filled with
70   // HTML tags and rather large.
71   LOG_INTERACTION() << "with fallback '" << fallback << "'";
72   // Display HTML at highest verbosity.
73   LOG_INTERACTION_AT_LEVEL(3) << "with HTML: " << HidePiiMaybe(response);
74 }
75 
OnSuggestionsResponse(const std::vector<chromeos::assistant::AssistantSuggestion> & response)76 void AssistantInteractionLogger::OnSuggestionsResponse(
77     const std::vector<chromeos::assistant::AssistantSuggestion>& response) {
78   std::stringstream suggestions;
79   for (const auto& suggestion : response)
80     suggestions << "'" << suggestion.text << "', ";
81   LOG_INTERACTION() << "{ " << suggestions.str() << " }";
82 }
83 
OnTextResponse(const std::string & response)84 void AssistantInteractionLogger::OnTextResponse(const std::string& response) {
85   LOG_INTERACTION() << HidePiiMaybe(response);
86 }
87 
OnOpenUrlResponse(const GURL & url,bool in_background)88 void AssistantInteractionLogger::OnOpenUrlResponse(const GURL& url,
89                                                    bool in_background) {
90   LOG_INTERACTION() << "with url '" << url.possibly_invalid_spec() << "'";
91 }
92 
OnOpenAppResponse(const AndroidAppInfo & app_info)93 bool AssistantInteractionLogger::OnOpenAppResponse(
94     const AndroidAppInfo& app_info) {
95   LOG_INTERACTION() << "with app '" << app_info.package_name << "'";
96   return false;
97 }
98 
OnSpeechRecognitionStarted()99 void AssistantInteractionLogger::OnSpeechRecognitionStarted() {
100   LOG_INTERACTION();
101 }
102 
OnSpeechRecognitionIntermediateResult(const std::string & high_confidence_text,const std::string & low_confidence_text)103 void AssistantInteractionLogger::OnSpeechRecognitionIntermediateResult(
104     const std::string& high_confidence_text,
105     const std::string& low_confidence_text) {
106   // Not logged until we have a use for this (and this might spam the log).
107 }
108 
OnSpeechRecognitionEndOfUtterance()109 void AssistantInteractionLogger::OnSpeechRecognitionEndOfUtterance() {
110   LOG_INTERACTION();
111 }
112 
OnSpeechRecognitionFinalResult(const std::string & final_result)113 void AssistantInteractionLogger::OnSpeechRecognitionFinalResult(
114     const std::string& final_result) {
115   LOG_INTERACTION() << "with final result '" << final_result << "'";
116 }
117 
OnSpeechLevelUpdated(float speech_level)118 void AssistantInteractionLogger::OnSpeechLevelUpdated(float speech_level) {
119   // Not logged until we have a use for this and this might spam the log.
120 }
121 
OnTtsStarted(bool due_to_error)122 void AssistantInteractionLogger::OnTtsStarted(bool due_to_error) {
123   LOG_INTERACTION() << (due_to_error ? "not" : "") << "due to error";
124 }
125 
OnWaitStarted()126 void AssistantInteractionLogger::OnWaitStarted() {
127   LOG_INTERACTION();
128 }
129 
130 }  // namespace assistant
131 }  // namespace chromeos
132