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