~linaro-graphics-wg/libmatrix/misc-changes-glmark2

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// Copyright (c) 2010-2012 Linaro Limited
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the MIT License which accompanies
// this distribution, and is available at
// http://www.opensource.org/licenses/mit-license.php
//
// Contributors:
//     Alexandros Frantzis <alexandros.frantzis@linaro.org>
//     Jesse Barker <jesse.barker@linaro.org>
//
#include <cstdio>
#include <cstdarg>
#include <string>
#include <sstream>
#include <iostream>
#include "log.h"

#ifdef ANDROID
#include <android/log.h>
#endif

using std::string;

const string Log::continuation_prefix("\x10");
string Log::appname_;
bool Log::do_debug_(false);

#ifndef ANDROID

static const string terminal_color_normal("\033[0m");
static const string terminal_color_red("\033[1;31m");
static const string terminal_color_cyan("\033[36m");
static const string terminal_color_yellow("\033[33m");
static const string empty;

static void
print_prefixed_message(std::ostream& stream, const string& color, const string& prefix,
                       const string& fmt, va_list ap)
{
    va_list aq;

    /* Estimate message size */
    va_copy(aq, ap);
    int msg_size = vsnprintf(NULL, 0, fmt.c_str(), aq);
    va_end(aq);

    /* Create the buffer to hold the message */
    char *buf = new char[msg_size + 1];

    /* Store the message in the buffer */
    va_copy(aq, ap);
    vsnprintf(buf, msg_size + 1, fmt.c_str(), aq);
    va_end(aq);

    /*
     * Print the message lines prefixed with the supplied prefix.
     * If the target stream is a terminal make the prefix colored.
     */
    string linePrefix;
    if (!prefix.empty())
    {
        static const string colon(": ");
        string start_color;
        string end_color;
        if (!color.empty())
        {
            start_color = color;
            end_color = terminal_color_normal;
        }
        linePrefix = start_color + prefix + end_color + colon;
    }

    std::string line;
    std::stringstream ss(buf);

    while(std::getline(ss, line)) {
        /*
         * If this line is a continuation of a previous log message
         * just print the line plainly.
         */
        if (line[0] == Log::continuation_prefix[0]) {
            stream << line.c_str() + 1;
        }
        else {
            /* Normal line, emit the prefix. */
            stream << linePrefix << line;
        }

        /* Only emit a newline if the original message has it. */
        if (!(ss.rdstate() & std::stringstream::eofbit))
            stream << std::endl;
    }

    delete[] buf;
}

void
Log::info(const char *fmt, ...)
{
    static const string infoprefix("Info");
    static const string& infocolor(isatty(fileno(stdout)) ? terminal_color_cyan : empty);
    va_list ap;
    va_start(ap, fmt);
    if (do_debug_)
        print_prefixed_message(std::cout, infocolor, infoprefix, fmt, ap);
    else
        print_prefixed_message(std::cout, empty, empty, fmt, ap);
    va_end(ap);
}

void
Log::debug(const char *fmt, ...)
{
    static const string dbgprefix("Debug");
    static const string& dbgcolor(isatty(fileno(stdout)) ? terminal_color_yellow : empty);
    if (!do_debug_)
        return;
    va_list ap;
    va_start(ap, fmt);
    print_prefixed_message(std::cout, dbgcolor, dbgprefix, fmt, ap);
    va_end(ap);
}

void
Log::error(const char *fmt, ...)
{
    static const string errprefix("Error");
    static const string& errcolor(isatty(fileno(stderr)) ? terminal_color_red : empty);
    va_list ap;
    va_start(ap, fmt);
    print_prefixed_message(std::cerr, errcolor, errprefix, fmt, ap);
    va_end(ap);
}

void
Log::flush()
{
    std::cout.flush();
    std::cerr.flush();
}
#else
void
Log::info(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    __android_log_vprint(ANDROID_LOG_INFO, appname_.c_str(), fmt, ap);
    va_end(ap);
}

void
Log::debug(const char *fmt, ...)
{
    if (!do_debug_)
        return;
    va_list ap;
    va_start(ap, fmt);
    __android_log_vprint(ANDROID_LOG_DEBUG, appname_.c_str(), fmt, ap);
    va_end(ap);
}

void
Log::error(const char *fmt, ...)
{
    va_list ap;
    va_start(ap, fmt);
    __android_log_vprint(ANDROID_LOG_ERROR, appname_.c_str(), fmt, ap);
    va_end(ap);
}

void
Log::flush()
{
}

#endif