1/*
2 * Copyright (C) 2005-2017 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 <stdint.h>
20#include <sys/types.h>
21
22#include <android/log.h>
23#include <log/log_time.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29#define ANDROID_LOG_WRAP_DEFAULT_TIMEOUT 7200 /* 2 hour default */
30
31/*
32 * Native log reading interface section. See logcat for sample code.
33 *
34 * The preferred API is an exec of logcat. Likely uses of this interface
35 * are if native code suffers from exec or filtration being too costly,
36 * access to raw information, or parsing is an issue.
37 */
38
39struct logger_entry {
40  uint16_t len;      /* length of the payload */
41  uint16_t hdr_size; /* sizeof(struct logger_entry) */
42  int32_t pid;       /* generating process's pid */
43  uint32_t tid;      /* generating process's tid */
44  uint32_t sec;      /* seconds since Epoch */
45  uint32_t nsec;     /* nanoseconds */
46  uint32_t lid;      /* log id of the payload, bottom 4 bits currently */
47  uint32_t uid;      /* generating process's uid */
48};
49
50/*
51 * The maximum size of a log entry which can be read.
52 * An attempt to read less than this amount may result
53 * in read() returning EINVAL.
54 */
55#define LOGGER_ENTRY_MAX_LEN (5 * 1024)
56
57struct log_msg {
58  union {
59    unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
60    struct logger_entry entry;
61  } __attribute__((aligned(4)));
62#ifdef __cplusplus
63  uint64_t nsec() const {
64    return static_cast<uint64_t>(entry.sec) * NS_PER_SEC + entry.nsec;
65  }
66  log_id_t id() {
67    return static_cast<log_id_t>(entry.lid);
68  }
69  char* msg() {
70    unsigned short hdr_size = entry.hdr_size;
71    if (hdr_size >= sizeof(struct log_msg) - sizeof(entry)) {
72      return nullptr;
73    }
74    return reinterpret_cast<char*>(buf) + hdr_size;
75  }
76  unsigned int len() { return entry.hdr_size + entry.len; }
77#endif
78};
79
80struct logger;
81
82log_id_t android_logger_get_id(struct logger* logger);
83
84/* Clears the given log buffer. */
85int android_logger_clear(struct logger* logger);
86/* Return the allotted size for the given log buffer. */
87long android_logger_get_log_size(struct logger* logger);
88/* Set the allotted size for the given log buffer. */
89int android_logger_set_log_size(struct logger* logger, unsigned long size);
90/* Return the actual, uncompressed size that can be read from the given log buffer. */
91long android_logger_get_log_readable_size(struct logger* logger);
92/* Return the actual, compressed size that the given log buffer is consuming. */
93long android_logger_get_log_consumed_size(struct logger* logger);
94/* Deprecated.  Always returns '4' regardless of input. */
95int android_logger_get_log_version(struct logger* logger);
96
97struct logger_list;
98
99ssize_t android_logger_get_statistics(struct logger_list* logger_list,
100                                      char* buf, size_t len);
101ssize_t android_logger_get_prune_list(struct logger_list* logger_list,
102                                      char* buf, size_t len);
103int android_logger_set_prune_list(struct logger_list* logger_list, const char* buf, size_t len);
104
105/* The below values are used for the `mode` argument of the below functions. */
106/* Note that 0x00000003 were previously used and should be considered reserved. */
107#define ANDROID_LOG_NONBLOCK 0x00000800
108#define ANDROID_LOG_WRAP 0x40000000 /* Block until buffer about to wrap */
109#define ANDROID_LOG_PSTORE 0x80000000
110
111struct logger_list* android_logger_list_alloc(int mode, unsigned int tail,
112                                              pid_t pid);
113struct logger_list* android_logger_list_alloc_time(int mode, log_time start,
114                                                   pid_t pid);
115void android_logger_list_free(struct logger_list* logger_list);
116/* In the purest sense, the following two are orthogonal interfaces */
117int android_logger_list_read(struct logger_list* logger_list,
118                             struct log_msg* log_msg);
119
120/* Multiple log_id_t opens */
121struct logger* android_logger_open(struct logger_list* logger_list, log_id_t id);
122/* Single log_id_t open */
123struct logger_list* android_logger_list_open(log_id_t id, int mode,
124                                             unsigned int tail, pid_t pid);
125#define android_logger_list_close android_logger_list_free
126
127#ifdef __cplusplus
128}
129#endif
130