blob: 025926650b18dc08c154fef96792cd83f729f6af [file] [log] [blame]
Mark Salyzyn0175b072014-02-26 09:50:16 -08001/*
Tom Cherryd5b38382020-05-12 13:16:41 -07002 * Copyright (C) 2020 The Android Open Source Project
Mark Salyzyn0175b072014-02-26 09:50:16 -08003 *
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 Cherry1a12ae32020-05-01 16:13:18 -070017#pragma once
Mark Salyzyn0175b072014-02-26 09:50:16 -080018
19#include <sys/types.h>
20
Tom Cherryd5b38382020-05-12 13:16:41 -070021#include <functional>
Tom Cherry28b9eb52020-10-05 11:35:59 -070022#include <memory>
Mark Salyzyn94a89c42015-08-19 13:41:51 -070023
Tom Cherryd5b38382020-05-12 13:16:41 -070024#include <log/log.h>
Tom Cherry70fadea2020-05-27 14:43:19 -070025#include <log/log_read.h>
Mark Salyzyn1a240b42014-06-12 11:16:16 -070026
Tom Cherry70fadea2020-05-27 14:43:19 -070027#include "LogWriter.h"
Tom Cherry283c9a12020-05-14 19:25:05 -070028
Tom Cherry855c7c82020-05-28 12:38:21 -070029// A mask to represent which log buffers a reader is watching, values are (1 << LOG_ID_MAIN), etc.
30using LogMask = uint32_t;
31constexpr uint32_t kLogMaskAll = 0xFFFFFFFF;
32
33// State that a LogBuffer may want to persist across calls to FlushTo().
34class FlushToState {
35 public:
36 FlushToState(uint64_t start, LogMask log_mask) : start_(start), log_mask_(log_mask) {}
37 virtual ~FlushToState() {}
38
39 uint64_t start() const { return start_; }
40 void set_start(uint64_t start) { start_ = start; }
41
42 LogMask log_mask() const { return log_mask_; }
43
44 private:
45 uint64_t start_;
46 LogMask log_mask_;
47};
48
49// Enum for the return values of the `filter` function passed to FlushTo().
Tom Cherry3e61a132020-05-27 10:46:37 -070050enum class FilterResult {
Tom Cherry68630a02020-05-11 16:29:29 -070051 kSkip,
52 kStop,
53 kWrite,
54};
55
Tom Cherry30968182019-06-28 13:37:10 -070056class LogBuffer {
Tom Cherry68630a02020-05-11 16:29:29 -070057 public:
Tom Cherryd5b38382020-05-12 13:16:41 -070058 virtual ~LogBuffer() {}
Mark Salyzyn0175b072014-02-26 09:50:16 -080059
Tom Cherryd5b38382020-05-12 13:16:41 -070060 virtual void Init() = 0;
61
62 virtual int Log(log_id_t log_id, log_time realtime, uid_t uid, pid_t pid, pid_t tid,
63 const char* msg, uint16_t len) = 0;
Tom Cherry855c7c82020-05-28 12:38:21 -070064
65 virtual std::unique_ptr<FlushToState> CreateFlushToState(uint64_t start, LogMask log_mask) = 0;
Tom Cherry28b9eb52020-10-05 11:35:59 -070066 virtual void DeleteFlushToState(std::unique_ptr<FlushToState>) {}
Tom Cherryb3e16332020-05-28 20:02:42 -070067 virtual bool FlushTo(
68 LogWriter* writer, FlushToState& state,
69 const std::function<FilterResult(log_id_t log_id, pid_t pid, uint64_t sequence,
70 log_time realtime)>& filter) = 0;
Mark Salyzyn0175b072014-02-26 09:50:16 -080071
Tom Cherryd5b38382020-05-12 13:16:41 -070072 virtual bool Clear(log_id_t id, uid_t uid) = 0;
Tom Cherry39dc2212020-08-05 12:14:45 -070073 virtual size_t GetSize(log_id_t id) = 0;
74 virtual bool SetSize(log_id_t id, size_t size) = 0;
Tom Cherrya3c5ff52020-05-21 13:56:33 -070075
76 virtual uint64_t sequence() const = 0;
Tom Cherry855c7c82020-05-28 12:38:21 -070077};