Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8eaaac01 authored by Mark Salyzyn's avatar Mark Salyzyn
Browse files

liblog: Add C++ wrapper for C event log handler

Add android_log_event_context class.

Test: in context of 'logd: add getEventTag command and service'
Bug: 31456426
Bug: 26552300
Bug: 31289077
Bug: 19235719
Change-Id: Icf918b443f01e04040c8d3efe0cdb7a8e70be10c
parent e5976bea
Loading
Loading
Loading
Loading
+94 −2
Original line number Diff line number Diff line
@@ -15,6 +15,10 @@
#include <time.h> /* clockid_t definition */
#endif

#ifdef __cplusplus
#include <string>
#endif

#include <log/log.h>
#include <log/log_read.h>

@@ -219,6 +223,94 @@ clockid_t android_log_clockid();
log_id_t android_name_to_log_id(const char *logName);
const char *android_log_id_to_name(log_id_t log_id);

#ifdef __cplusplus
// android_log_context C++ helpers
class android_log_event_context {
    android_log_context ctx;
    int ret;

public:
    explicit android_log_event_context(int tag) : ret(0) {
        ctx = create_android_logger(tag);
    }
    explicit android_log_event_context(log_msg& log_msg) : ret(0) {
        ctx = create_android_log_parser(log_msg.msg() + sizeof(uint32_t),
                                        log_msg.entry.len - sizeof(uint32_t));
    }
    ~android_log_event_context() { android_log_destroy(&ctx); }

    int close() {
        int retval = android_log_destroy(&ctx);
        if (retval < 0) ret = retval;
        return retval;
    }

    // To allow above C calls to use this class as parameter
    operator android_log_context() const { return ctx; };

    int error() const { return ret; }

    int begin() {
        int retval = android_log_write_list_begin(ctx);
        if (retval < 0) ret = retval;
        return ret;
    }
    int end() {
        int retval = android_log_write_list_end(ctx);
        if (retval < 0) ret = retval;
        return ret;
    }

    android_log_event_context& operator <<(int32_t value) {
        int retval = android_log_write_int32(ctx, value);
        if (retval < 0) ret = retval;
        return *this;
    }
    android_log_event_context& operator <<(uint32_t value) {
        int retval = android_log_write_int32(ctx, value);
        if (retval < 0) ret = retval;
        return *this;
    }
    android_log_event_context& operator <<(int64_t value) {
        int retval = android_log_write_int64(ctx, value);
        if (retval < 0) ret = retval;
        return *this;
    }
    android_log_event_context& operator <<(uint64_t value) {
        int retval = android_log_write_int64(ctx, value);
        if (retval < 0) ret = retval;
        return *this;
    }
    android_log_event_context& operator <<(const char* value) {
        int retval = android_log_write_string8(ctx, value);
        if (retval < 0) ret = retval;
        return *this;
    }
    android_log_event_context& operator <<(std::string& value) {
        int retval = android_log_write_string8_len(ctx,
                                                   value.data(),
                                                   value.length());
        if (retval < 0) ret = retval;
        return *this;
    }
    android_log_event_context& operator <<(float value) {
        int retval = android_log_write_float32(ctx, value);
        if (retval < 0) ret = retval;
        return *this;
    }

    int write(log_id_t id) {
        int retval = android_log_write_list(ctx, id);
        if (retval < 0) ret = retval;
        return ret;
    }

    android_log_list_element read() { return android_log_read_next(ctx); }
    android_log_list_element peak() { return android_log_peek_next(ctx); }

};
#endif

#ifdef __cplusplus
}
#endif