summaryrefslogtreecommitdiff
path: root/platform/android/log_android.cpp
blob: 5e40ce33bd68a6c7707e8b4f7ff4fe92fc1f7c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <mbgl/platform/android/log_android.hpp>

#include <iostream>
#include <cstdarg>
#define __STDC_FORMAT_MACROS // NDK bug workaround: https://code.google.com/p/android/issues/detail?id=72349
#include <cinttypes>

#include <android/log.h>

namespace mbgl {

int AndroidLogBackend::severityToPriority(EventSeverity severity) {
    switch(severity) {
    case EventSeverity::Debug:
        return ANDROID_LOG_DEBUG;

    case EventSeverity::Info:
        return ANDROID_LOG_INFO;

    case EventSeverity::Warning:
        return ANDROID_LOG_WARN;

    case EventSeverity::Error:
        return ANDROID_LOG_ERROR;

    default:
        return ANDROID_LOG_VERBOSE;
    }
}

void AndroidLogBackend::record(EventSeverity severity, Event event, const std::string &msg) {
    __android_log_print(severityToPriority(severity), EventClass(event).c_str(), "%s", msg.c_str());
}

void AndroidLogBackend::record(EventSeverity severity, Event event, const char* format, ...) {
    va_list args;
    va_start(args, format);

    const int len = vsnprintf(nullptr, 0, format, args) + 1;
    char* buf = new char[len];
    vsnprintf(buf, len, format, args);

    va_end(args);

    __android_log_print(severityToPriority(severity), EventClass(event).c_str(), "%s", buf);

    delete buf;
    buf  = nullptr;
}

void AndroidLogBackend::record(EventSeverity severity, Event event, int64_t code) {
    __android_log_print(severityToPriority(severity), EventClass(event).c_str(), "(%" PRId64 ")", code);
}

void AndroidLogBackend::record(EventSeverity severity, Event event, int64_t code, const std::string &msg) {
    __android_log_print(severityToPriority(severity), EventClass(event).c_str(), "(%" PRId64 ") %s", code, msg.c_str());
}

}