blob: c0f62d39ca992e596ec2e843a66e67496b447cf3 [file] [log] [blame]
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -07001/*
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
Tom Cherrye170d1a2020-05-01 16:45:25 -070017#pragma once
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070018
Mark Salyzynd048f112016-02-08 10:28:12 -080019#include <sys/cdefs.h>
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070020#include <sys/types.h>
21
Mark Salyzynaeaaf812016-09-30 13:30:33 -070022#include <private/android_logger.h>
Mark Salyzyn501c3732017-03-10 14:31:54 -080023#include <utils/FastStrcmp.h>
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070024
25// Hijack this header as a common include file used by most all sources
26// to report some utilities defined here and there.
27
Elliott Hughes0fe3b672020-08-20 15:40:57 -070028#define LOGD_SNDTIMEO 32
29
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070030namespace android {
31
32// Furnished in main.cpp. Caller must own and free returned value
Mark Salyzyn501c3732017-03-10 14:31:54 -080033char* uidToName(uid_t uid);
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070034
Mark Salyzyn32962912016-09-12 10:29:17 -070035// Caller must own and free returned value
Mark Salyzyn501c3732017-03-10 14:31:54 -080036char* pidToName(pid_t pid);
37char* tidToName(pid_t tid);
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070038
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070039// Furnished in LogTags.cpp. Thread safe.
Mark Salyzyn501c3732017-03-10 14:31:54 -080040const char* tagToName(uint32_t tag);
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070041
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070042// Furnished by LogKlog.cpp
43char* log_strntok_r(char* s, ssize_t& len, char*& saveptr, ssize_t& sublen);
44
45// needle should reference a string longer than 1 character
46static 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}
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070061}
62
Tom Cherry3dd3ec32020-06-02 15:39:21 -070063// Returns true if the log buffer is meant for binary logs.
64static 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.
69static 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
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070077static inline bool worstUidEnabledForLogid(log_id_t id) {
Mark Salyzyn6a066942016-07-14 15:34:30 -070078 return (id == LOG_ID_MAIN) || (id == LOG_ID_SYSTEM) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -080079 (id == LOG_ID_RADIO) || (id == LOG_ID_EVENTS);
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070080}