xref: /aosp_15_r20/external/pigweed/pw_chre/chre_api_re.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "chre/platform/log.h"
16 #include "chre/util/macros.h"
17 #include "chre_api/chre/re.h"
18 #include "pw_log/log.h"
19 #include "pw_string/format.h"
20 
21 namespace {
ToPigweedLogLevel(enum chreLogLevel level)22 int ToPigweedLogLevel(enum chreLogLevel level) {
23   switch (level) {
24     case CHRE_LOG_ERROR:
25       return PW_LOG_LEVEL_ERROR;
26     case CHRE_LOG_WARN:
27       return PW_LOG_LEVEL_WARN;
28     case CHRE_LOG_INFO:
29       return PW_LOG_LEVEL_INFO;
30     case CHRE_LOG_DEBUG:
31       return PW_LOG_LEVEL_DEBUG;
32   }
33 }
34 }  // namespace
35 
chreLog(enum chreLogLevel level,const char * format_string,...)36 DLL_EXPORT void chreLog(enum chreLogLevel level,
37                         const char* format_string,
38                         ...) {
39   char log[512];
40 
41   va_list args;
42   va_start(args, format_string);
43   pw::StatusWithSize status = pw::string::Format(log, format_string, args);
44   PW_ASSERT(status.ok());
45   va_end(args);
46 
47   PW_LOG(
48       ToPigweedLogLevel(level), PW_LOG_LEVEL, "CHRE", PW_LOG_FLAGS, "%s", log);
49 }
50