| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BLIMP_COMMON_LOGGING_H_ | 5 #ifndef BLIMP_COMMON_LOGGING_H_ |
| 6 #define BLIMP_COMMON_LOGGING_H_ | 6 #define BLIMP_COMMON_LOGGING_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <ostream> |
| 9 #include <memory> | |
| 10 #include <string> | |
| 11 #include <utility> | |
| 12 #include <vector> | |
| 13 | 9 |
| 14 #include "base/macros.h" | 10 #include "base/macros.h" |
| 15 #include "blimp/common/blimp_common_export.h" | 11 #include "blimp/common/blimp_common_export.h" |
| 16 #include "blimp/common/proto/blimp_message.pb.h" | |
| 17 | 12 |
| 18 namespace blimp { | 13 namespace blimp { |
| 19 | 14 |
| 20 class BlimpMessage; | 15 class BlimpMessage; |
| 21 class LogExtractor; | |
| 22 | |
| 23 typedef std::vector<std::pair<std::string, std::string>> LogFields; | |
| 24 | |
| 25 // Defines an interface for classes that extract a set of loggable field | |
| 26 // values from a message. | |
| 27 class BLIMP_COMMON_EXPORT LogExtractor { | |
| 28 public: | |
| 29 virtual ~LogExtractor() {} | |
| 30 | |
| 31 // |message|: The message which is used for field extraction. | |
| 32 // |output|: A vector of KV pairs which will receive the extracted fields. | |
| 33 virtual void ExtractFields(const BlimpMessage& message, | |
| 34 LogFields* output) const = 0; | |
| 35 }; | |
| 36 | |
| 37 // Registry of log extractors. | |
| 38 class BLIMP_COMMON_EXPORT BlimpMessageLogger { | |
| 39 public: | |
| 40 BlimpMessageLogger(); | |
| 41 ~BlimpMessageLogger(); | |
| 42 | |
| 43 // Formats the loggable representation of |message| and sends it to |out|. | |
| 44 void LogMessageToStream(const BlimpMessage& message, std::ostream* out) const; | |
| 45 | |
| 46 private: | |
| 47 // Adds |extractor| to the registry for parsing messages of type | |
| 48 // |feature_case|. | |
| 49 // |feature_name|: The human readable name of |feature_case|. | |
| 50 void AddHandler(const std::string& feature_name, | |
| 51 BlimpMessage::FeatureCase feature_case, | |
| 52 std::unique_ptr<LogExtractor> extractor); | |
| 53 | |
| 54 // Registry of log extractors. Map structure is: | |
| 55 // {message type => (human readable message type, LogExtractor*)} | |
| 56 std::map<BlimpMessage::FeatureCase, | |
| 57 std::pair<std::string, std::unique_ptr<LogExtractor>>> | |
| 58 extractors_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(BlimpMessageLogger); | |
| 61 }; | |
| 62 | 16 |
| 63 // Serializes a BlimpMessage in a human-readable format for logging. | 17 // Serializes a BlimpMessage in a human-readable format for logging. |
| 64 // An example message would look like: | 18 // An example message would look like: |
| 65 // "<type=TAB_CONTROL subtype=SIZE size=640x480:1.000000 size=19>" | 19 // "<type=TAB_CONTROL subtype=SIZE size=640x480:1.000000 size=19>" |
| 66 BLIMP_COMMON_EXPORT std::ostream& operator<<(std::ostream& out, | 20 BLIMP_COMMON_EXPORT std::ostream& operator<<(std::ostream& out, |
| 67 const BlimpMessage& message); | 21 const BlimpMessage& message); |
| 68 | 22 |
| 69 } // namespace blimp | 23 } // namespace blimp |
| 70 | 24 |
| 71 #endif // BLIMP_COMMON_LOGGING_H_ | 25 #endif // BLIMP_COMMON_LOGGING_H_ |
| OLD | NEW |