From 6bd71aef37dc037ca82d036507408d03226ec57f Mon Sep 17 00:00:00 2001 From: Yao Chen Date: Thu, 6 Dec 2018 13:03:12 -0800 Subject: [PATCH] Add a function to pass byte array to the log context. The existing APIs will truncate the byte array in case of '\0' in between. Test: test_drive with atom 103 Change-Id: I104d35b330c6b52e2fdce1f5d4305dca08228f1b --- libstats/include/stats_event_list.h | 10 ++++++- libstats/stats_event_list.c | 44 +++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/libstats/include/stats_event_list.h b/libstats/include/stats_event_list.h index a9832db29..41ca79bcd 100644 --- a/libstats/include/stats_event_list.h +++ b/libstats/include/stats_event_list.h @@ -26,7 +26,7 @@ void reset_log_context(android_log_context ctx); int write_to_logger(android_log_context context, log_id_t id); void note_log_drop(); void stats_log_close(); - +int android_log_write_char_array(android_log_context ctx, const char* value, size_t len); #ifdef __cplusplus } #endif @@ -244,6 +244,14 @@ class stats_event_list { return ret >= 0; } + bool AppendCharArray(const char* value, size_t len) { + int retval = android_log_write_char_array(ctx, value, len); + if (retval < 0) { + ret = retval; + } + return ret >= 0; + } + android_log_list_element read() { return android_log_read_next(ctx); } android_log_list_element peek() { return android_log_peek_next(ctx); } }; diff --git a/libstats/stats_event_list.c b/libstats/stats_event_list.c index 72770d48a..f4a7e9405 100644 --- a/libstats/stats_event_list.c +++ b/libstats/stats_event_list.c @@ -193,3 +193,47 @@ static int __write_to_statsd_init(struct iovec* vec, size_t nr) { errno = save_errno; return ret; } + +static inline void copy4LE(uint8_t* buf, uint32_t val) { + buf[0] = val & 0xFF; + buf[1] = (val >> 8) & 0xFF; + buf[2] = (val >> 16) & 0xFF; + buf[3] = (val >> 24) & 0xFF; +} + +// Note: this function differs from android_log_write_string8_len in that the length passed in +// should be treated as actual length and not max length. +int android_log_write_char_array(android_log_context ctx, const char* value, size_t actual_len) { + size_t needed; + ssize_t len = actual_len; + android_log_context_internal* context; + + context = (android_log_context_internal*)ctx; + if (!context || (kAndroidLoggerWrite != context->read_write_flag)) { + return -EBADF; + } + if (context->overflow) { + return -EIO; + } + if (!value) { + value = ""; + len = 0; + } + needed = sizeof(uint8_t) + sizeof(int32_t) + len; + if ((context->pos + needed) > MAX_EVENT_PAYLOAD) { + /* Truncate string for delivery */ + len = MAX_EVENT_PAYLOAD - context->pos - 1 - sizeof(int32_t); + if (len <= 0) { + context->overflow = true; + return -EIO; + } + } + context->count[context->list_nest_depth]++; + context->storage[context->pos + 0] = EVENT_TYPE_STRING; + copy4LE(&context->storage[context->pos + 1], len); + if (len) { + memcpy(&context->storage[context->pos + 5], value, len); + } + context->pos += needed; + return len; +}