blob: df78a508acdc2b37f4595b59644b3869443e5dee [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 Salyzyn083b0372015-12-04 10:59:45 -080023#include <sysutils/SocketClient.h>
Mark Salyzyn501c3732017-03-10 14:31:54 -080024#include <utils/FastStrcmp.h>
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070025
26// Hijack this header as a common include file used by most all sources
27// to report some utilities defined here and there.
28
29namespace android {
30
31// Furnished in main.cpp. Caller must own and free returned value
Mark Salyzyn501c3732017-03-10 14:31:54 -080032char* uidToName(uid_t uid);
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070033
Mark Salyzyn32962912016-09-12 10:29:17 -070034// Caller must own and free returned value
Mark Salyzyn501c3732017-03-10 14:31:54 -080035char* pidToName(pid_t pid);
36char* tidToName(pid_t tid);
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070037
Mark Salyzyn61e9ce62016-09-12 14:51:54 -070038// Furnished in LogTags.cpp. Thread safe.
Mark Salyzyn501c3732017-03-10 14:31:54 -080039const char* tagToName(uint32_t tag);
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070040
Mark Salyzyn0484b3b2016-08-11 08:02:06 -070041// Furnished by LogKlog.cpp
42char* log_strntok_r(char* s, ssize_t& len, char*& saveptr, ssize_t& sublen);
43
44// needle should reference a string longer than 1 character
45static inline const char* strnstr(const char* s, ssize_t len,
46 const char* needle) {
47 if (len <= 0) return nullptr;
48
49 const char c = *needle++;
50 const size_t needleLen = strlen(needle);
51 do {
52 do {
53 if (len <= (ssize_t)needleLen) return nullptr;
54 --len;
55 } while (*s++ != c);
56 } while (fastcmp<memcmp>(s, needle, needleLen));
57 s--;
58 return s;
59}
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070060}
61
Tom Cherry3dd3ec32020-06-02 15:39:21 -070062// Returns true if the log buffer is meant for binary logs.
63static inline bool IsBinary(log_id_t log_id) {
64 return log_id == LOG_ID_EVENTS || log_id == LOG_ID_STATS || log_id == LOG_ID_SECURITY;
65}
66
67// Returns the numeric log tag for binary log messages.
68static inline uint32_t MsgToTag(const char* msg, uint16_t msg_len) {
69 if (msg_len < sizeof(android_event_header_t)) {
70 return 0;
71 }
72
73 return reinterpret_cast<const android_event_header_t*>(msg)->tag;
74}
75
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070076static inline bool worstUidEnabledForLogid(log_id_t id) {
Mark Salyzyn6a066942016-07-14 15:34:30 -070077 return (id == LOG_ID_MAIN) || (id == LOG_ID_SYSTEM) ||
Mark Salyzyn501c3732017-03-10 14:31:54 -080078 (id == LOG_ID_RADIO) || (id == LOG_ID_EVENTS);
Mark Salyzyn5ac5c6b2015-08-28 08:02:59 -070079}