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/assert.h"
18 #include "chre/platform/log.h"
19 #ifdef CHRE_USE_BUFFERED_LOGGING
20 #include "chre/platform/shared/log_buffer_manager.h"
21 #endif
22 #include "chre/util/macros.h"
23 #include "chre_api/chre/re.h"
24 
25 #ifdef CHRE_USE_FARF_LOGGING
logToFarf(enum chreLogLevel level,const char * logStr)26 inline void logToFarf(enum chreLogLevel level, const char *logStr) {
27   switch (level) {
28     case CHRE_LOG_ERROR:
29       LOGE(logStr);
30       break;
31     case CHRE_LOG_WARN:
32       LOGW(logStr);
33       break;
34     case CHRE_LOG_INFO:
35       LOGI(logStr);
36       break;
37     case CHRE_LOG_DEBUG:
38     default:
39       LOGD(logStr);
40   }
41 }
42 
43 #elif !defined(CHRE_USE_BUFFERED_LOGGING)
chreLogLevelToAshLogLevel(enum chreLogLevel level)44 inline ashLogLevel chreLogLevelToAshLogLevel(enum chreLogLevel level) {
45   enum ashLogLevel ashLevel;
46   switch (level) {
47     case CHRE_LOG_ERROR:
48       ashLevel = ASH_LOG_ERROR;
49       break;
50     case CHRE_LOG_WARN:
51       ashLevel = ASH_LOG_WARN;
52       break;
53     case CHRE_LOG_INFO:
54       ashLevel = ASH_LOG_INFO;
55       break;
56     case CHRE_LOG_DEBUG:
57     default:
58       ashLevel = ASH_LOG_DEBUG;
59   }
60   return ashLevel;
61 }
62 #endif  // !defined(CHRE_USE_BUFFERED_LOGGING)
63 
chreLog(enum chreLogLevel level,const char * formatStr,...)64 DLL_EXPORT void chreLog(enum chreLogLevel level, const char *formatStr, ...) {
65   va_list args;
66 
67   va_start(args, formatStr);
68 #ifdef CHRE_USE_BUFFERED_LOGGING
69   CHRE_ASSERT(chre::LogBufferManagerSingleton::isInitialized());
70   chre::LogBufferManagerSingleton::get()->logVa(level, formatStr, args);
71 #elif defined(CHRE_USE_FARF_LOGGING)
72   // The same size is used in the implmentation of ashVaLog().
73   constexpr size_t kDebugMaxLogEntrySize = 128;
74   // FARF doesn't provide a method that takes va_list as an input so write the
75   // string to a buffer before logging so it can be passed to the LOGX methods.
76   char logBuf[kDebugMaxLogEntrySize];
77   vsnprintf(logBuf, sizeof(logBuf), formatStr, args);
78   logToFarf(level, logBuf);
79 #else   // CHRE_USE_FARF_LOGGING
80   ashVaLog(ASH_SOURCE_CHRE, chreLogLevelToAshLogLevel(level), formatStr, args);
81 #endif  // CHRE_USE_FARF_LOGGING
82   va_end(args);
83 }
84