StatsEventCompat
StatsEventCompat is a wrapper library that toggles between the old
logging scheme and the new logging scheme. It was designed particularly
for the DNS Resolver module and should not be used by others.
We will merge libstatspush_compat and libstatssocket_q in a future CL.
Test: m libstatspush_compat
Test: bit libstatspush_compat_test:* (passes on Q and R)
Bug: 145534143
Change-Id: Ib355031f3573101ea90bd8694861fbfc33b0c788
Merged-In: Idf35ccb6669798166475f08b2fbab40534b5db19
diff --git a/libstats/socket/Android.bp b/libstats/socket/Android.bp
index beb009c..bd3d9ae 100644
--- a/libstats/socket/Android.bp
+++ b/libstats/socket/Android.bp
@@ -41,3 +41,9 @@
"liblog",
],
}
+
+cc_library_headers {
+ name: "libstatssocket_headers",
+ export_include_dirs: ["include"],
+ host_supported: true,
+}
diff --git a/libstats/socket/include/stats_event.h b/libstats/socket/include/stats_event.h
index 6a33d54..e7117d2 100644
--- a/libstats/socket/include/stats_event.h
+++ b/libstats/socket/include/stats_event.h
@@ -85,7 +85,7 @@
// The build function can be called multiple times without error. If the event
// has been built before, this function is a no-op.
void stats_event_build(struct stats_event* event);
-void stats_event_write(struct stats_event* event);
+int stats_event_write(struct stats_event* event);
void stats_event_release(struct stats_event* event);
void stats_event_set_atom_id(struct stats_event* event, uint32_t atomId);
@@ -98,7 +98,7 @@
void stats_event_write_byte_array(struct stats_event* event, const uint8_t* buf, size_t numBytes);
// Buf must be null-terminated.
-void stats_event_write_string8(struct stats_event* event, const char* buf);
+void stats_event_write_string8(struct stats_event* event, const char* value);
// Tags must be null-terminated.
void stats_event_write_attribution_chain(struct stats_event* event, const uint32_t* uids,
@@ -127,9 +127,33 @@
int32_t value);
uint32_t stats_event_get_atom_id(struct stats_event* event);
+// Size is an output parameter.
uint8_t* stats_event_get_buffer(struct stats_event* event, size_t* size);
uint32_t stats_event_get_errors(struct stats_event* event);
+// This table is used by StatsEventCompat to access the stats_event API.
+struct stats_event_api_table {
+ struct stats_event* (*obtain)(void);
+ void (*build)(struct stats_event*);
+ int (*write)(struct stats_event*);
+ void (*release)(struct stats_event*);
+ void (*set_atom_id)(struct stats_event*, uint32_t);
+ void (*write_int32)(struct stats_event*, int32_t);
+ void (*write_int64)(struct stats_event*, int64_t);
+ void (*write_float)(struct stats_event*, float);
+ void (*write_bool)(struct stats_event*, bool);
+ void (*write_byte_array)(struct stats_event*, const uint8_t*, size_t);
+ void (*write_string8)(struct stats_event*, const char*);
+ void (*write_attribution_chain)(struct stats_event*, const uint32_t*, const char* const*,
+ uint8_t);
+ void (*write_key_value_pairs)(struct stats_event*, struct key_value_pair*, uint8_t);
+ void (*add_bool_annotation)(struct stats_event*, uint8_t, bool);
+ void (*add_int32_annotation)(struct stats_event*, uint8_t, int32_t);
+ uint32_t (*get_atom_id)(struct stats_event*);
+ uint8_t* (*get_buffer)(struct stats_event*, size_t*);
+ uint32_t (*get_errors)(struct stats_event*);
+};
+
#ifdef __cplusplus
}
#endif // __CPLUSPLUS
diff --git a/libstats/socket/stats_event.c b/libstats/socket/stats_event.c
index ef887e3..4098434 100644
--- a/libstats/socket/stats_event.c
+++ b/libstats/socket/stats_event.c
@@ -193,12 +193,12 @@
append_byte_array(event, buf, numBytes);
}
-// Buf is assumed to be encoded using UTF8
-void stats_event_write_string8(struct stats_event* event, const char* buf) {
+// Value is assumed to be encoded using UTF8
+void stats_event_write_string8(struct stats_event* event, const char* value) {
if (event->errors) return;
start_field(event, STRING_TYPE);
- append_string(event, buf);
+ append_string(event, value);
}
// Tags are assumed to be encoded using UTF8
@@ -320,8 +320,28 @@
event->built = true;
}
-void stats_event_write(struct stats_event* event) {
+int stats_event_write(struct stats_event* event) {
stats_event_build(event);
-
- write_buffer_to_statsd(&event->buf, event->size, event->atomId);
+ return write_buffer_to_statsd(&event->buf, event->size, event->atomId);
}
+
+struct stats_event_api_table table = {
+ stats_event_obtain,
+ stats_event_build,
+ stats_event_write,
+ stats_event_release,
+ stats_event_set_atom_id,
+ stats_event_write_int32,
+ stats_event_write_int64,
+ stats_event_write_float,
+ stats_event_write_bool,
+ stats_event_write_byte_array,
+ stats_event_write_string8,
+ stats_event_write_attribution_chain,
+ stats_event_write_key_value_pairs,
+ stats_event_add_bool_annotation,
+ stats_event_add_int32_annotation,
+ stats_event_get_atom_id,
+ stats_event_get_buffer,
+ stats_event_get_errors,
+};