blob: e72290ed124c26751ed7377801681f8606049917 [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
Mark Salyzynfacf94c2016-03-01 13:45:42 -080022/* In the future, we would like to make this list extensible */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080023static const char* LOG_NAME[LOG_ID_MAX] = {
Tom Cherry71ba1642019-01-10 10:37:36 -080024 /* clang-format off */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080025 [LOG_ID_MAIN] = "main",
26 [LOG_ID_RADIO] = "radio",
27 [LOG_ID_EVENTS] = "events",
28 [LOG_ID_SYSTEM] = "system",
29 [LOG_ID_CRASH] = "crash",
Stefan Lafon701a0652017-08-24 20:14:06 -070030 [LOG_ID_STATS] = "stats",
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080031 [LOG_ID_SECURITY] = "security",
32 [LOG_ID_KERNEL] = "kernel",
Tom Cherry71ba1642019-01-10 10:37:36 -080033 /* clang-format on */
Mark Salyzynfacf94c2016-03-01 13:45:42 -080034};
35
Tom Cherry2d9779e2019-02-08 11:46:19 -080036const char* android_log_id_to_name(log_id_t log_id) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080037 if (log_id >= LOG_ID_MAX) {
38 log_id = LOG_ID_MAIN;
39 }
40 return LOG_NAME[log_id];
Mark Salyzynfacf94c2016-03-01 13:45:42 -080041}
42
Tom Cherry71ba1642019-01-10 10:37:36 -080043static_assert(std::is_same<std::underlying_type<log_id_t>::type, uint32_t>::value,
Tom Cherryf1a975b2020-03-12 11:07:07 -070044 "log_id_t must be an uint32_t");
45
46static_assert(std::is_same<std::underlying_type<android_LogPriority>::type, uint32_t>::value,
47 "log_id_t must be an uint32_t");
Tom Cherry71ba1642019-01-10 10:37:36 -080048
Tom Cherry2d9779e2019-02-08 11:46:19 -080049log_id_t android_name_to_log_id(const char* logName) {
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080050 const char* b;
Tom Cherry71ba1642019-01-10 10:37:36 -080051 unsigned int ret;
Mark Salyzynfacf94c2016-03-01 13:45:42 -080052
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080053 if (!logName) {
Josh Gaoc6ad69d2019-03-25 14:55:39 -070054 return static_cast<log_id_t>(LOG_ID_MAX);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080055 }
Josh Gaoc6ad69d2019-03-25 14:55:39 -070056
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080057 b = strrchr(logName, '/');
58 if (!b) {
59 b = logName;
60 } else {
61 ++b;
62 }
Mark Salyzynfacf94c2016-03-01 13:45:42 -080063
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080064 for (ret = LOG_ID_MIN; ret < LOG_ID_MAX; ++ret) {
65 const char* l = LOG_NAME[ret];
66 if (l && !strcmp(b, l)) {
Tom Cherry71ba1642019-01-10 10:37:36 -080067 return static_cast<log_id_t>(ret);
Mark Salyzynfacf94c2016-03-01 13:45:42 -080068 }
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080069 }
Josh Gaoc6ad69d2019-03-25 14:55:39 -070070
71 return static_cast<log_id_t>(LOG_ID_MAX);
Mark Salyzynfacf94c2016-03-01 13:45:42 -080072}