blob: af83228fd7d409bc6ccee6e5c8e5217225d59f92 [file] [log] [blame]
Mark Salyzyn018a96d2016-03-01 13:45:42 -08001/*
2 * Copyright (C) 2016 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#ifndef _LIBLOG_LOGGER_H__
18#define _LIBLOG_LOGGER_H__
19
Mark Salyzyndb8a2662016-10-10 07:27:42 -070020#include <stdatomic.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080021#include <stdbool.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080022
23#include <cutils/list.h>
Mark Salyzynaeaaf812016-09-30 13:30:33 -070024#include <log/log.h>
Mark Salyzyncfd5b082016-10-17 14:28:00 -070025#include <log/uio.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -080026
27#include "log_portability.h"
28
29__BEGIN_DECLS
30
31/* Union, sock or fd of zero is not allowed unless static initialized */
32union android_log_context {
Mark Salyzyn04bbc8e2017-03-08 15:03:20 -080033 void* priv;
Mark Salyzyndb8a2662016-10-10 07:27:42 -070034 atomic_int sock;
35 atomic_int fd;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080036 struct listnode* node;
Mark Salyzyndb8a2662016-10-10 07:27:42 -070037 atomic_uintptr_t atomic_pointer;
Mark Salyzyn018a96d2016-03-01 13:45:42 -080038};
39
40struct android_log_transport_write {
41 struct listnode node;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080042 const char* name; /* human name to describe the transport */
43 unsigned logMask; /* mask cache of available() success */
Mark Salyzyn018a96d2016-03-01 13:45:42 -080044 union android_log_context context; /* Initialized by static allocation */
45
Mark Salyzyn7048f0b2017-02-06 10:21:56 -080046 int (*available)(log_id_t logId); /* Does not cause resources to be taken */
47 int (*open)(); /* can be called multiple times, reusing current resources */
48 void (*close)(); /* free up resources */
49 /* write log to transport, returns number of bytes propagated, or -errno */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080050 int (*write)(log_id_t logId, struct timespec* ts, struct iovec* vec,
51 size_t nr);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080052};
53
54struct android_log_logger_list;
55struct android_log_transport_context;
56struct android_log_logger;
57
58struct android_log_transport_read {
59 struct listnode node;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080060 const char* name; /* human name to describe the transport */
Mark Salyzyn018a96d2016-03-01 13:45:42 -080061
Mark Salyzyn7048f0b2017-02-06 10:21:56 -080062 /* Does not cause resources to be taken */
Mark Salyzyn018a96d2016-03-01 13:45:42 -080063 int (*available)(log_id_t logId);
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080064 int (*version)(struct android_log_logger* logger,
65 struct android_log_transport_context* transp);
Mark Salyzyn7048f0b2017-02-06 10:21:56 -080066 /* Release resources taken by the following interfaces */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080067 void (*close)(struct android_log_logger_list* logger_list,
68 struct android_log_transport_context* transp);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080069 /*
Mark Salyzyn7048f0b2017-02-06 10:21:56 -080070 * Expect all to instantiate open automagically on any call,
71 * so we do not have an explicit open call.
Mark Salyzyn018a96d2016-03-01 13:45:42 -080072 */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080073 int (*read)(struct android_log_logger_list* logger_list,
74 struct android_log_transport_context* transp,
75 struct log_msg* log_msg);
Mark Salyzyn7048f0b2017-02-06 10:21:56 -080076 /* Must only be called if not ANDROID_LOG_NONBLOCK (blocking) */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080077 int (*poll)(struct android_log_logger_list* logger_list,
78 struct android_log_transport_context* transp);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080079
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080080 int (*clear)(struct android_log_logger* logger,
81 struct android_log_transport_context* transp);
82 ssize_t (*setSize)(struct android_log_logger* logger,
83 struct android_log_transport_context* transp, size_t size);
84 ssize_t (*getSize)(struct android_log_logger* logger,
85 struct android_log_transport_context* transp);
86 ssize_t (*getReadableSize)(struct android_log_logger* logger,
87 struct android_log_transport_context* transp);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080088
Mark Salyzyn2ed51d72017-03-09 08:09:43 -080089 ssize_t (*getPrune)(struct android_log_logger_list* logger_list,
90 struct android_log_transport_context* transp, char* buf,
91 size_t len);
92 ssize_t (*setPrune)(struct android_log_logger_list* logger_list,
93 struct android_log_transport_context* transp, char* buf,
94 size_t len);
95 ssize_t (*getStats)(struct android_log_logger_list* logger_list,
96 struct android_log_transport_context* transp, char* buf,
97 size_t len);
Mark Salyzyn018a96d2016-03-01 13:45:42 -080098};
99
100struct android_log_logger_list {
101 struct listnode logger;
102 struct listnode transport;
103 int mode;
104 unsigned int tail;
105 log_time start;
106 pid_t pid;
107};
108
109struct android_log_logger {
110 struct listnode node;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800111 struct android_log_logger_list* parent;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800112
113 log_id_t logId;
114};
115
116struct android_log_transport_context {
117 struct listnode node;
118 union android_log_context context; /* zero init per-transport context */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800119 struct android_log_logger_list* parent;
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800120
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800121 struct android_log_transport_read* transport;
Mark Salyzyn7048f0b2017-02-06 10:21:56 -0800122 unsigned logMask; /* mask of requested log buffers */
123 int ret; /* return value associated with following data */
124 struct log_msg logMsg; /* peek at upcoming data, valid if logMsg.len != 0 */
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800125};
126
127/* assumes caller has structures read-locked, single threaded, or fenced */
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800128#define transport_context_for_each(transp, logger_list) \
129 for ((transp) = node_to_item((logger_list)->transport.next, \
130 struct android_log_transport_context, node); \
131 ((transp) != node_to_item(&(logger_list)->transport, \
132 struct android_log_transport_context, node)) && \
133 ((transp)->parent == (logger_list)); \
134 (transp) = node_to_item((transp)->node.next, \
135 struct android_log_transport_context, node))
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800136
137#define logger_for_each(logp, logger_list) \
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800138 for ((logp) = node_to_item((logger_list)->logger.next, \
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800139 struct android_log_logger, node); \
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800140 ((logp) != node_to_item(&(logger_list)->logger, \
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800141 struct android_log_logger, node)) && \
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800142 ((logp)->parent == (logger_list)); \
143 (logp) = \
144 node_to_item((logp)->node.next, struct android_log_logger, node))
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800145
146/* OS specific dribs and drabs */
147
148#if defined(_WIN32)
Mark Salyzynec4f5c72016-07-13 07:38:39 -0700149#include <private/android_filesystem_config.h>
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800150typedef uint32_t uid_t;
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800151static inline uid_t __android_log_uid() {
152 return AID_SYSTEM;
153}
Mark Salyzynec4f5c72016-07-13 07:38:39 -0700154#else
Mark Salyzyn2ed51d72017-03-09 08:09:43 -0800155static inline uid_t __android_log_uid() {
156 return getuid();
157}
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800158#endif
159
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800160LIBLOG_HIDDEN void __android_log_lock();
161LIBLOG_HIDDEN int __android_log_trylock();
162LIBLOG_HIDDEN void __android_log_unlock();
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800163
Mark Salyzyn81321a72017-03-09 07:28:29 -0800164LIBLOG_HIDDEN int __android_log_transport;
Mark Salyzyn71002882016-03-08 16:18:26 -0800165
Mark Salyzyn018a96d2016-03-01 13:45:42 -0800166__END_DECLS
167
168#endif /* _LIBLOG_LOGGER_H__ */