• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
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  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CHRE_LOG_MESSAGE_PARSER_BASE_H_
18 #define CHRE_LOG_MESSAGE_PARSER_BASE_H_
19 
20 #include <cinttypes>
21 #include <cstdint>
22 
23 #include "chre_host/log.h"
24 
25 #include <android-base/logging.h>
26 
27 namespace android {
28 namespace chre {
29 
30 // TODO: Since nanoapp logging won't use tokenized logging, refactor this
31 // to have the base class handle normal log messages, and a subclass
32 // that could extend to handle log messages of other types (eg: tokenized
33 // logging), and can then invoke the parent class methods if they receive
34 // a log of the 'normal' type.
35 class ChreLogMessageParserBase {
36  public:
37   ChreLogMessageParserBase();
38 
~ChreLogMessageParserBase()39   virtual ~ChreLogMessageParserBase() {}
40 
init()41   virtual bool init() {
42     return true;
43   };
44 
deinit()45   virtual bool deinit() {
46     return true;
47   };
48 
49   //! Logs from a log buffer containing one or more log messages (version 1)
50   virtual void log(const uint8_t *logBuffer, size_t logBufferSize);
51 
52   //! Logs from a log buffer containing one or more log messages (version 2)
53   virtual void logV2(const uint8_t *logBuffer, size_t logBufferSize,
54                      uint32_t numLogsDropped);
55 
56   /**
57    * With verbose logging enabled (via enableVerbose()), dump a
58    * binary log buffer to a human-readable string
59    *
60    * @param logBuffer buffer to be output as a string
61    * @param logBufferSize size of the buffer being output
62    */
63   virtual void dump(const uint8_t *logBuffer, size_t logBufferSize);
64 
enableVerbose(bool enable)65   void enableVerbose(bool enable) {
66     mVerboseLoggingEnabled = enable;
67   }
68 
69  protected:
70   static constexpr char kHubLogFormatStr[] = "@ %3" PRIu32 ".%03" PRIu32 ": %s";
71 
72   bool mVerboseLoggingEnabled;
73 
74   static android_LogPriority chreLogLevelToAndroidLogPriority(uint8_t level);
75 
76   void emitLogMessage(uint8_t level, uint32_t timestampMillis,
77                       const char *logMessage);
78 
79  private:
80   enum LogLevel : uint8_t {
81     ERROR = 1,
82     WARNING = 2,
83     INFO = 3,
84     DEBUG = 4,
85     VERBOSE = 5,
86   };
87 
88   //! See host_messages.fbs for the definition of this struct.
89   struct LogMessage {
90     enum LogLevel logLevel;
91     uint64_t timestampNanos;
92     char logMessage[];
93   } __attribute__((packed));
94 
95   //! See host_messages.fbs for the definition of this struct.
96   struct LogMessageV2 {
97     enum LogLevel logLevel;
98     uint32_t timestampMillis;
99     char logMessage[];
100   } __attribute__((packed));
101 
102   //! The number of logs dropped since CHRE start
103   uint32_t mNumLogsDropped = 0;
104 };
105 
106 }  // namespace chre
107 }  // namespace android
108 
109 #endif  // CHRE_LOG_MESSAGE_PARSER_BASE_H_
110