1 /*
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>
18 #include <type_traits>
19 
20 #include <log/log.h>
21 
22 #include "log_portability.h"
23 
24 /* In the future, we would like to make this list extensible */
25 static const char* LOG_NAME[LOG_ID_MAX] = {
26     /* clang-format off */
27   [LOG_ID_MAIN] = "main",
28   [LOG_ID_RADIO] = "radio",
29   [LOG_ID_EVENTS] = "events",
30   [LOG_ID_SYSTEM] = "system",
31   [LOG_ID_CRASH] = "crash",
32   [LOG_ID_STATS] = "stats",
33   [LOG_ID_SECURITY] = "security",
34   [LOG_ID_KERNEL] = "kernel",
35     /* clang-format on */
36 };
37 
android_log_id_to_name(log_id_t log_id)38 const char* android_log_id_to_name(log_id_t log_id) {
39   if (log_id >= LOG_ID_MAX) {
40     log_id = LOG_ID_MAIN;
41   }
42   return LOG_NAME[log_id];
43 }
44 
45 static_assert(std::is_same<std::underlying_type<log_id_t>::type, uint32_t>::value,
46               "log_id_t must be an unsigned int");
47 
android_name_to_log_id(const char * logName)48 log_id_t android_name_to_log_id(const char* logName) {
49   const char* b;
50   unsigned int ret;
51 
52   if (!logName) {
53     return static_cast<log_id_t>(LOG_ID_MAX);
54   }
55 
56   b = strrchr(logName, '/');
57   if (!b) {
58     b = logName;
59   } else {
60     ++b;
61   }
62 
63   for (ret = LOG_ID_MIN; ret < LOG_ID_MAX; ++ret) {
64     const char* l = LOG_NAME[ret];
65     if (l && !strcmp(b, l)) {
66       return static_cast<log_id_t>(ret);
67     }
68   }
69 
70   return static_cast<log_id_t>(LOG_ID_MAX);
71 }
72