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