Path: blob/21.2-virgl/include/android_stub/log/log_event_list.h
4547 views
/*1* Copyright (C) 2005-2016 The Android Open Source Project2*3* Licensed under the Apache License, Version 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6*7* http://www.apache.org/licenses/LICENSE-2.08*9* Unless required by applicable law or agreed to in writing, software10* distributed under the License is distributed on an "AS IS" BASIS,11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12* See the License for the specific language governing permissions and13* limitations under the License.14*/1516#pragma once1718#include <errno.h>19#include <stdint.h>2021#ifdef __cplusplus22#include <string>23#endif2425#include <log/log.h>2627#ifdef __cplusplus28extern "C" {29#endif3031/* For manipulating lists of events. */3233#define ANDROID_MAX_LIST_NEST_DEPTH 83435/*36* The opaque context used to manipulate lists of events.37*/38typedef struct android_log_context_internal* android_log_context;3940/*41* Elements returned when reading a list of events.42*/43typedef struct {44AndroidEventLogType type;45uint16_t complete;46uint16_t len;47union {48int32_t int32;49int64_t int64;50char* string;51float float32;52} data;53} android_log_list_element;5455/*56* Creates a context associated with an event tag to write elements to57* the list of events.58*/59android_log_context create_android_logger(uint32_t tag);6061/* All lists must be braced by a begin and end call */62/*63* NB: If the first level braces are missing when specifying multiple64* elements, we will manufacturer a list to embrace it for your API65* convenience. For a single element, it will remain solitary.66*/67int android_log_write_list_begin(android_log_context ctx);68int android_log_write_list_end(android_log_context ctx);6970int android_log_write_int32(android_log_context ctx, int32_t value);71int android_log_write_int64(android_log_context ctx, int64_t value);72int android_log_write_string8(android_log_context ctx, const char* value);73int android_log_write_string8_len(android_log_context ctx, const char* value,74size_t maxlen);75int android_log_write_float32(android_log_context ctx, float value);7677/* Submit the composed list context to the specified logger id */78/* NB: LOG_ID_EVENTS and LOG_ID_SECURITY only valid binary buffers */79int android_log_write_list(android_log_context ctx, log_id_t id);8081/*82* Creates a context from a raw buffer representing a list of events to be read.83*/84android_log_context create_android_log_parser(const char* msg, size_t len);8586android_log_list_element android_log_read_next(android_log_context ctx);87android_log_list_element android_log_peek_next(android_log_context ctx);8889/* Reset writer context */90int android_log_reset(android_log_context ctx);9192/* Reset reader context */93int android_log_parser_reset(android_log_context ctx,94const char* msg, size_t len);9596/* Finished with reader or writer context */97int android_log_destroy(android_log_context* ctx);9899#ifdef __cplusplus100/* android_log_list C++ helpers */101extern "C++" {102class android_log_event_list {103private:104android_log_context ctx;105int ret;106107android_log_event_list(const android_log_event_list&) = delete;108void operator=(const android_log_event_list&) = delete;109110public:111explicit android_log_event_list(int tag) : ret(0) {112ctx = create_android_logger(static_cast<uint32_t>(tag));113}114~android_log_event_list() {115android_log_destroy(&ctx);116}117118int close() {119int retval = android_log_destroy(&ctx);120if (retval < 0) ret = retval;121return retval;122}123124/* To allow above C calls to use this class as parameter */125operator android_log_context() const {126return ctx;127}128129/* return errors or transmit status */130int status() const {131return ret;132}133134int begin() {135int retval = android_log_write_list_begin(ctx);136if (retval < 0) ret = retval;137return ret;138}139int end() {140int retval = android_log_write_list_end(ctx);141if (retval < 0) ret = retval;142return ret;143}144145android_log_event_list& operator<<(int32_t value) {146int retval = android_log_write_int32(ctx, value);147if (retval < 0) ret = retval;148return *this;149}150151android_log_event_list& operator<<(uint32_t value) {152int retval = android_log_write_int32(ctx, static_cast<int32_t>(value));153if (retval < 0) ret = retval;154return *this;155}156157android_log_event_list& operator<<(bool value) {158int retval = android_log_write_int32(ctx, value ? 1 : 0);159if (retval < 0) ret = retval;160return *this;161}162163android_log_event_list& operator<<(int64_t value) {164int retval = android_log_write_int64(ctx, value);165if (retval < 0) ret = retval;166return *this;167}168169android_log_event_list& operator<<(uint64_t value) {170int retval = android_log_write_int64(ctx, static_cast<int64_t>(value));171if (retval < 0) ret = retval;172return *this;173}174175android_log_event_list& operator<<(const char* value) {176int retval = android_log_write_string8(ctx, value);177if (retval < 0) ret = retval;178return *this;179}180181android_log_event_list& operator<<(const std::string& value) {182int retval =183android_log_write_string8_len(ctx, value.data(), value.length());184if (retval < 0) ret = retval;185return *this;186}187188android_log_event_list& operator<<(float value) {189int retval = android_log_write_float32(ctx, value);190if (retval < 0) ret = retval;191return *this;192}193194int write(log_id_t id = LOG_ID_EVENTS) {195/* facilitate -EBUSY retry */196if ((ret == -EBUSY) || (ret > 0)) ret = 0;197int retval = android_log_write_list(ctx, id);198/* existing errors trump transmission errors */199if (!ret) ret = retval;200return ret;201}202203int operator<<(log_id_t id) {204write(id);205android_log_destroy(&ctx);206return ret;207}208209/*210* Append<Type> methods removes any integer promotion211* confusion, and adds access to string with length.212* Append methods are also added for all types for213* convenience.214*/215216bool AppendInt(int32_t value) {217int retval = android_log_write_int32(ctx, value);218if (retval < 0) ret = retval;219return ret >= 0;220}221222bool AppendLong(int64_t value) {223int retval = android_log_write_int64(ctx, value);224if (retval < 0) ret = retval;225return ret >= 0;226}227228bool AppendString(const char* value) {229int retval = android_log_write_string8(ctx, value);230if (retval < 0) ret = retval;231return ret >= 0;232}233234bool AppendString(const char* value, size_t len) {235int retval = android_log_write_string8_len(ctx, value, len);236if (retval < 0) ret = retval;237return ret >= 0;238}239240bool AppendString(const std::string& value) {241int retval =242android_log_write_string8_len(ctx, value.data(), value.length());243if (retval < 0) ret = retval;244return ret;245}246247bool Append(const std::string& value) {248int retval =249android_log_write_string8_len(ctx, value.data(), value.length());250if (retval < 0) ret = retval;251return ret;252}253254bool AppendFloat(float value) {255int retval = android_log_write_float32(ctx, value);256if (retval < 0) ret = retval;257return ret >= 0;258}259260template <typename Tvalue>261bool Append(Tvalue value) {262*this << value;263return ret >= 0;264}265266bool Append(const char* value, size_t len) {267int retval = android_log_write_string8_len(ctx, value, len);268if (retval < 0) ret = retval;269return ret >= 0;270}271};272}273#endif274275#ifdef __cplusplus276}277#endif278279280