blob: 16a04f8f852a175b21817a4966c9b1907f724437 [file] [log] [blame]
Ruchir Rastogi6baeca52019-10-14 16:18:34 -07001/*
2 * Copyright (C) 2019 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 ANDROID_STATS_LOG_STATS_EVENT_H
18#define ANDROID_STATS_LOG_STATS_EVENT_H
19
20#include <stdbool.h>
21#include <stddef.h>
22#include <stdint.h>
23
24/*
25 * Functionality to build and store the buffer sent over the statsd socket.
26 * This code defines and encapsulates the socket protocol.
27 *
28 * Usage:
29 * struct stats_event* event = stats_event_obtain();
30 *
31 * stats_event_set_timestamp_ns(event, timestampNs);
32 * stats_event_set_atom_id(event, atomId);
33 * stats_event_write_int32(event, 24);
34 * stats_event_add_bool_annotation(event, 1, true); // annotations apply to the previous field
35 * stats_event_add_int32_annotation(event, 2, 128);
36 * stats_event_write_float(event, 2.0);
37 *
38 * stats_event_write(event);
39 * stats_event_release(event);
40 *
41 * Notes:
42 * (a) write_<type>() and add_<type>_annotation() should be called in the order that fields
43 * and annotations are defined in the atom.
44 * (b) set_timestamp_ns() and set_atom_id() can be called anytime before stats_event_write().
45 * (c) add_<type>_annotation() calls apply to the previous field.
46 * (d) If errors occur, stats_event_write() will write a bitmask of the errors to the socket.
47 * (e) Strings should be encoded using UTF8 and written using stats_event_write_string8().
48 */
49
50struct stats_event;
51
52/* ERRORS */
53#define ERROR_NO_TIMESTAMP 0x1
54#define ERROR_NO_ATOM_ID 0x2
55#define ERROR_OVERFLOW 0x4
56#define ERROR_ATTRIBUTION_CHAIN_TOO_LONG 0x8
57#define ERROR_ANNOTATION_DOES_NOT_FOLLOW_FIELD 0x10
58#define ERROR_INVALID_ANNOTATION_ID 0x20
59#define ERROR_ANNOTATION_ID_TOO_LARGE 0x40
60#define ERROR_TOO_MANY_ANNOTATIONS 0x80
61#define ERROR_TOO_MANY_FIELDS 0x100
62
63/* System API */
64struct stats_event* stats_event_obtain();
65void stats_event_write(struct stats_event* event);
66void stats_event_release(struct stats_event* event);
67
68void stats_event_set_atom_id(struct stats_event* event, const uint32_t atomId);
69void stats_event_set_timestamp_ns(struct stats_event* event, const uint64_t timestampNs);
70
71void stats_event_write_int32(struct stats_event* event, int32_t value);
72void stats_event_write_int64(struct stats_event* event, int64_t value);
73void stats_event_write_float(struct stats_event* event, float value);
74void stats_event_write_bool(struct stats_event* event, bool value);
75void stats_event_write_byte_array(struct stats_event* event, uint8_t* buf, uint32_t numBytes);
76void stats_event_write_string8(struct stats_event* event, char* buf, uint32_t numBytes);
77void stats_event_write_attribution_chain(struct stats_event* event, uint32_t* uids, char** tags,
78 uint32_t* tagLengths, uint32_t numNodes);
79
80void stats_event_add_bool_annotation(struct stats_event* event, uint32_t annotationId, bool value);
81void stats_event_add_int32_annotation(struct stats_event* event, uint32_t annotationId,
82 int32_t value);
83
84uint32_t stats_event_get_errors(struct stats_event* event);
85
86#endif // ANDROID_STATS_LOG_STATS_EVENT_H