• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "chre/platform/linux/platform_log.h"
18 
19 #include <cstdarg>
20 #include <cstdio>
21 #include <iostream>
22 
23 #include "chre/platform/fatal_error.h"
24 #include "chre/platform/shared/bt_snoop_log.h"
25 
chrePlatformBtSnoopLog(BtSnoopDirection,const uint8_t *,size_t)26 void chrePlatformBtSnoopLog(BtSnoopDirection /*direction*/,
27                             const uint8_t * /*buffer*/, size_t /*size*/) {
28   // Unimplemented in this platform
29 }
30 
31 namespace chre {
32 
logLooper()33 void PlatformLog::logLooper() {
34   while (1) {
35     char *logMessage = nullptr;
36 
37     {
38       std::unique_lock<std::mutex> lock(mMutex);
39       mConditionVariable.wait(
40           lock, [this] { return (!mLogQueue.empty() || mStopLogger); });
41 
42       if (!mLogQueue.empty()) {
43         // Move the log message to avoid holding a lock for longer than
44         // required.
45         logMessage = mLogQueue.front();
46         mLogQueue.pop();
47       } else if (mStopLogger) {
48         // The stop logger is checked in an else-if to allow the main log queue
49         // to drain when the logger is stopping.
50         break;
51       }
52     }
53 
54     // If we get here, there must be a log message to output. This is outside of
55     // the context of the lock which means that the logging thread will only be
56     // blocked for the minimum amount of time.
57     std::cerr << logMessage << std::endl;
58     free(logMessage);
59   }
60 }
61 
PlatformLog()62 PlatformLog::PlatformLog() {
63   mLoggerThread = std::thread(&PlatformLog::logLooper, this);
64 }
65 
~PlatformLog()66 PlatformLog::~PlatformLog() {
67   {
68     std::unique_lock<std::mutex> lock(mMutex);
69     mStopLogger = true;
70     mConditionVariable.notify_one();
71   }
72 
73   mLoggerThread.join();
74 }
75 
logVa(chreLogLevel,const char * formatStr,va_list args)76 void PlatformLog::logVa(chreLogLevel /*logLevel*/, const char *formatStr,
77                         va_list args) {
78   char *formattedStr;
79   int result = vasprintf(&formattedStr, formatStr, args);
80 
81   if (result >= 0) {
82     std::unique_lock<std::mutex> lock(mMutex);
83     mLogQueue.push(formattedStr);
84     mConditionVariable.notify_one();
85   } else {
86     FATAL_ERROR("Failed to allocate log message");
87   }
88 }
89 
90 }  // namespace chre
91