blob: ece05505b58559beedec6119f20f70540408baf0 [file] [log] [blame]
Mark Salyzynfacf94c2016-03-01 13:45:42 -08001/*
2** Copyright 2013-2014, 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 <string.h>
Tom Cherry71ba1642019-01-10 10:37:36 -080018#include <type_traits>
Mark Salyzynfacf94c2016-03-01 13:45:42 -080019
Mark Salyzynaeaaf812016-09-30 13:30:33 -070020#include <log/log.h>
Mark Salyzynfacf94c2016-03-01 13:45:42 -080021
22#include "log_portability.h"
23
24/* In the future, we would like to make this list extensible */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080025static const char* LOG_NAME[LOG_ID_MAX] = {
Tom Cherry71ba1642019-01-10 10:37:36 -080026 /* clang-format off */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080027 [LOG_ID_MAIN] = "main",
28 [LOG_ID_RADIO] = "radio",
29 [LOG_ID_EVENTS] = "events",
30 [LOG_ID_SYSTEM] = "system",
31 [LOG_ID_CRASH] = "crash",
Stefan Lafon701a0652017-08-24 20:14:06 -070032 [LOG_ID_STATS] = "stats",
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080033 [LOG_ID_SECURITY] = "security",
34 [LOG_ID_KERNEL] = "kernel",
Tom Cherry71ba1642019-01-10 10:37:36 -080035 /* clang-format on */
Mark Salyzynfacf94c2016-03-01 13:45:42 -080036};
37
Tom Cherry2d9779e2019-02-08 11:46:19 -080038const char* android_log_id_to_name(log_id_t log_id) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080039 if (log_id >= LOG_ID_MAX) {
40 log_id = LOG_ID_MAIN;
41 }
42 return LOG_NAME[log_id];
Mark Salyzynfacf94c2016-03-01 13:45:42 -080043}
44
Tom Cherry71ba1642019-01-10 10:37:36 -080045static_assert(std::is_same<std::underlying_type<log_id_t>::type, uint32_t>::value,
46 "log_id_t must be an unsigned int");
47
Tom Cherry2d9779e2019-02-08 11:46:19 -080048log_id_t android_name_to_log_id(const char* logName) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080049 const char* b;
Tom Cherry71ba1642019-01-10 10:37:36 -080050 unsigned int ret;
Mark Salyzynfacf94c2016-03-01 13:45:42 -080051
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080052 if (!logName) {
Josh Gaoc6ad69d2019-03-25 14:55:39 -070053 return static_cast<log_id_t>(LOG_ID_MAX);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080054 }
Josh Gaoc6ad69d2019-03-25 14:55:39 -070055
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080056 b = strrchr(logName, '/');
57 if (!b) {
58 b = logName;
59 } else {
60 ++b;
61 }
Mark Salyzynfacf94c2016-03-01 13:45:42 -080062
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080063 for (ret = LOG_ID_MIN; ret < LOG_ID_MAX; ++ret) {
64 const char* l = LOG_NAME[ret];
65 if (l && !strcmp(b, l)) {
Tom Cherry71ba1642019-01-10 10:37:36 -080066 return static_cast<log_id_t>(ret);
Mark Salyzynfacf94c2016-03-01 13:45:42 -080067 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080068 }
Josh Gaoc6ad69d2019-03-25 14:55:39 -070069
70 return static_cast<log_id_t>(LOG_ID_MAX);
Mark Salyzynfacf94c2016-03-01 13:45:42 -080071}