1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2005-2017 The Android Open Source Project
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
5bf215546Sopenharmony_ci * you may not use this file except in compliance with the License.
6bf215546Sopenharmony_ci * You may obtain a copy of the License at
7bf215546Sopenharmony_ci *
8bf215546Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
9bf215546Sopenharmony_ci *
10bf215546Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
11bf215546Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
12bf215546Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bf215546Sopenharmony_ci * See the License for the specific language governing permissions and
14bf215546Sopenharmony_ci * limitations under the License.
15bf215546Sopenharmony_ci */
16bf215546Sopenharmony_ci
17bf215546Sopenharmony_ci#pragma once
18bf215546Sopenharmony_ci
19bf215546Sopenharmony_ci#include <stdint.h>
20bf215546Sopenharmony_ci#include <sys/types.h>
21bf215546Sopenharmony_ci
22bf215546Sopenharmony_ci#include <android/log.h>
23bf215546Sopenharmony_ci#include <log/log_time.h>
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#ifdef __cplusplus
26bf215546Sopenharmony_ciextern "C" {
27bf215546Sopenharmony_ci#endif
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#define ANDROID_LOG_WRAP_DEFAULT_TIMEOUT 7200 /* 2 hour default */
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci/*
32bf215546Sopenharmony_ci * Native log reading interface section. See logcat for sample code.
33bf215546Sopenharmony_ci *
34bf215546Sopenharmony_ci * The preferred API is an exec of logcat. Likely uses of this interface
35bf215546Sopenharmony_ci * are if native code suffers from exec or filtration being too costly,
36bf215546Sopenharmony_ci * access to raw information, or parsing is an issue.
37bf215546Sopenharmony_ci */
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_cistruct logger_entry {
40bf215546Sopenharmony_ci  uint16_t len;      /* length of the payload */
41bf215546Sopenharmony_ci  uint16_t hdr_size; /* sizeof(struct logger_entry) */
42bf215546Sopenharmony_ci  int32_t pid;       /* generating process's pid */
43bf215546Sopenharmony_ci  uint32_t tid;      /* generating process's tid */
44bf215546Sopenharmony_ci  uint32_t sec;      /* seconds since Epoch */
45bf215546Sopenharmony_ci  uint32_t nsec;     /* nanoseconds */
46bf215546Sopenharmony_ci  uint32_t lid;      /* log id of the payload, bottom 4 bits currently */
47bf215546Sopenharmony_ci  uint32_t uid;      /* generating process's uid */
48bf215546Sopenharmony_ci};
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci/*
51bf215546Sopenharmony_ci * The maximum size of a log entry which can be read.
52bf215546Sopenharmony_ci * An attempt to read less than this amount may result
53bf215546Sopenharmony_ci * in read() returning EINVAL.
54bf215546Sopenharmony_ci */
55bf215546Sopenharmony_ci#define LOGGER_ENTRY_MAX_LEN (5 * 1024)
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_cistruct log_msg {
58bf215546Sopenharmony_ci  union {
59bf215546Sopenharmony_ci    unsigned char buf[LOGGER_ENTRY_MAX_LEN + 1];
60bf215546Sopenharmony_ci    struct logger_entry entry;
61bf215546Sopenharmony_ci  } __attribute__((aligned(4)));
62bf215546Sopenharmony_ci#ifdef __cplusplus
63bf215546Sopenharmony_ci  uint64_t nsec() const {
64bf215546Sopenharmony_ci    return static_cast<uint64_t>(entry.sec) * NS_PER_SEC + entry.nsec;
65bf215546Sopenharmony_ci  }
66bf215546Sopenharmony_ci  log_id_t id() {
67bf215546Sopenharmony_ci    return static_cast<log_id_t>(entry.lid);
68bf215546Sopenharmony_ci  }
69bf215546Sopenharmony_ci  char* msg() {
70bf215546Sopenharmony_ci    unsigned short hdr_size = entry.hdr_size;
71bf215546Sopenharmony_ci    if (hdr_size >= sizeof(struct log_msg) - sizeof(entry)) {
72bf215546Sopenharmony_ci      return nullptr;
73bf215546Sopenharmony_ci    }
74bf215546Sopenharmony_ci    return reinterpret_cast<char*>(buf) + hdr_size;
75bf215546Sopenharmony_ci  }
76bf215546Sopenharmony_ci  unsigned int len() { return entry.hdr_size + entry.len; }
77bf215546Sopenharmony_ci#endif
78bf215546Sopenharmony_ci};
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_cistruct logger;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_cilog_id_t android_logger_get_id(struct logger* logger);
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci/* Clears the given log buffer. */
85bf215546Sopenharmony_ciint android_logger_clear(struct logger* logger);
86bf215546Sopenharmony_ci/* Return the allotted size for the given log buffer. */
87bf215546Sopenharmony_cilong android_logger_get_log_size(struct logger* logger);
88bf215546Sopenharmony_ci/* Set the allotted size for the given log buffer. */
89bf215546Sopenharmony_ciint android_logger_set_log_size(struct logger* logger, unsigned long size);
90bf215546Sopenharmony_ci/* Return the actual, uncompressed size that can be read from the given log buffer. */
91bf215546Sopenharmony_cilong android_logger_get_log_readable_size(struct logger* logger);
92bf215546Sopenharmony_ci/* Return the actual, compressed size that the given log buffer is consuming. */
93bf215546Sopenharmony_cilong android_logger_get_log_consumed_size(struct logger* logger);
94bf215546Sopenharmony_ci/* Deprecated.  Always returns '4' regardless of input. */
95bf215546Sopenharmony_ciint android_logger_get_log_version(struct logger* logger);
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_cistruct logger_list;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_cissize_t android_logger_get_statistics(struct logger_list* logger_list,
100bf215546Sopenharmony_ci                                      char* buf, size_t len);
101bf215546Sopenharmony_cissize_t android_logger_get_prune_list(struct logger_list* logger_list,
102bf215546Sopenharmony_ci                                      char* buf, size_t len);
103bf215546Sopenharmony_ciint android_logger_set_prune_list(struct logger_list* logger_list, const char* buf, size_t len);
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci/* The below values are used for the `mode` argument of the below functions. */
106bf215546Sopenharmony_ci/* Note that 0x00000003 were previously used and should be considered reserved. */
107bf215546Sopenharmony_ci#define ANDROID_LOG_NONBLOCK 0x00000800
108bf215546Sopenharmony_ci#define ANDROID_LOG_WRAP 0x40000000 /* Block until buffer about to wrap */
109bf215546Sopenharmony_ci#define ANDROID_LOG_PSTORE 0x80000000
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_cistruct logger_list* android_logger_list_alloc(int mode, unsigned int tail,
112bf215546Sopenharmony_ci                                              pid_t pid);
113bf215546Sopenharmony_cistruct logger_list* android_logger_list_alloc_time(int mode, log_time start,
114bf215546Sopenharmony_ci                                                   pid_t pid);
115bf215546Sopenharmony_civoid android_logger_list_free(struct logger_list* logger_list);
116bf215546Sopenharmony_ci/* In the purest sense, the following two are orthogonal interfaces */
117bf215546Sopenharmony_ciint android_logger_list_read(struct logger_list* logger_list,
118bf215546Sopenharmony_ci                             struct log_msg* log_msg);
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci/* Multiple log_id_t opens */
121bf215546Sopenharmony_cistruct logger* android_logger_open(struct logger_list* logger_list, log_id_t id);
122bf215546Sopenharmony_ci/* Single log_id_t open */
123bf215546Sopenharmony_cistruct logger_list* android_logger_list_open(log_id_t id, int mode,
124bf215546Sopenharmony_ci                                             unsigned int tail, pid_t pid);
125bf215546Sopenharmony_ci#define android_logger_list_close android_logger_list_free
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci#ifdef __cplusplus
128bf215546Sopenharmony_ci}
129bf215546Sopenharmony_ci#endif
130