Home | History | Annotate | Download | only in target_platform
      1 /*
      2  * Copyright (C) 2017 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_PLATFORM_PLATFORM_LOG_BASE_H_
     18 #define CHRE_PLATFORM_PLATFORM_LOG_BASE_H_
     19 
     20 #include <cstddef>
     21 #include <cstdint>
     22 
     23 #include "chre/platform/mutex.h"
     24 
     25 namespace chre {
     26 
     27 /**
     28  * Storage for the SLPI implementation of PlatformLog.
     29  */
     30 class PlatformLogBase {
     31  public:
     32   typedef void (FlushLogBufferCallback)(const char *buffer, size_t bufferSize,
     33                                         void *context);
     34 
     35   /**
     36    * Flushes the log buffer by invoking a provided callback, passing in the log
     37    * buffer and size, then resetting the log buffer. The callback is invoked
     38    * within the context of the logging lock which guarantees exclusive access to
     39    * the buffer. The callback should not attempt to use the PlatformLog::log()
     40    * method or else a deadlock will occur.
     41    *
     42    * @param callback the callback to invoke within the context of a lock on the
     43    *        log buffer.
     44    * @param context a context pointer to provide to the callback.
     45    */
     46   void flushLogBuffer(FlushLogBufferCallback callback, void *context);
     47 
     48  protected:
     49   //! The maximum size of a formatted log message.
     50   static constexpr size_t kMaxLogMessageSize = 160;
     51 
     52   //! The size of the logging buffer, in bytes. Assuming an average log message
     53   //! has a length of 60, this allows for ~70 log messages. Some messages may
     54   //! be longer, some may be shorter. YMMV but this should be a good balance
     55   //! between AP wakeups and memory usage.
     56   static constexpr size_t kLogBufferSize = (1 * 1024);
     57 
     58   //! The watermark point at which log buffers should be sent to the host. This
     59   //! cannot be zero or else an infinite loop may be caused. It must be large
     60   //! enough to hold a few messages.
     61   static constexpr size_t kLogBufferWatermarkSize = (kLogBufferSize * 20) / 100;
     62 
     63   //! A mutex for acquiring exclusive control over the log buffer and other
     64   //! internal state.
     65   Mutex mMutex;
     66 
     67   //! The buffer to log messages into.
     68   char mLogBuffer[kLogBufferSize];
     69 
     70   //! The current size of messages logged in the buffer.
     71   size_t mLogBufferSize = 0;
     72 
     73   //! Set to true when a log flush is pending. Do not request another log flush
     74   //! if this is set to true.
     75   bool mLogFlushPending = false;
     76 };
     77 
     78 }  // namespace chre
     79 
     80 #endif  // CHRE_PLATFORM_PLATFORM_LOG_BASE_H_
     81