1 /*
2 * Copyright (C) 2012-2015 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 #pragma once
18
19 #include <sys/cdefs.h>
20 #include <sys/types.h>
21
22 #include <private/android_logger.h>
23 #include <utils/FastStrcmp.h>
24
25 // Hijack this header as a common include file used by most all sources
26 // to report some utilities defined here and there.
27
28 #define LOGD_SNDTIMEO 32
29
30 namespace android {
31
32 // Furnished in main.cpp. Caller must own and free returned value
33 char* uidToName(uid_t uid);
34
35 // Caller must own and free returned value
36 char* pidToName(pid_t pid);
37 char* tidToName(pid_t tid);
38
39 // Furnished in LogTags.cpp. Thread safe.
40 const char* tagToName(uint32_t tag);
41
42 // Furnished by LogKlog.cpp
43 char* log_strntok_r(char* s, ssize_t& len, char*& saveptr, ssize_t& sublen);
44
45 // needle should reference a string longer than 1 character
strnstr(const char * s,ssize_t len,const char * needle)46 static inline const char* strnstr(const char* s, ssize_t len,
47 const char* needle) {
48 if (len <= 0) return nullptr;
49
50 const char c = *needle++;
51 const size_t needleLen = strlen(needle);
52 do {
53 do {
54 if (len <= (ssize_t)needleLen) return nullptr;
55 --len;
56 } while (*s++ != c);
57 } while (fastcmp<memcmp>(s, needle, needleLen));
58 s--;
59 return s;
60 }
61 }
62
63 // Returns true if the log buffer is meant for binary logs.
IsBinary(log_id_t log_id)64 static inline bool IsBinary(log_id_t log_id) {
65 return log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS || log_id == LOG_ID_SECURITY;
66 }
67
68 // Returns the numeric log tag for binary log messages.
MsgToTag(const char * msg,uint16_t msg_len)69 static inline uint32_t MsgToTag(const char* msg, uint16_t msg_len) {
70 if (msg_len < sizeof(android_event_header_t)) {
71 return 0;
72 }
73
74 return reinterpret_cast<const android_event_header_t*>(msg)->tag;
75 }
76
worstUidEnabledForLogid(log_id_t id)77 static inline bool worstUidEnabledForLogid(log_id_t id) {
78 return (id == LOG_ID_MAIN) || (id == LOG_ID_SYSTEM) ||
79 (id == LOG_ID_RADIO) || (id == LOG_ID_EVENTS);
80 }
81