summaryrefslogtreecommitdiffstats
path: root/gfx/graphite2/src/XmlTraceLog.cpp
blob: f34d15018f3f418e00d22f39030c14ff229eb871 (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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*  GRAPHITE2 LICENSING

    Copyright 2010, SIL International
    All rights reserved.

    This library is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published
    by the Free Software Foundation; either version 2.1 of License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should also have received a copy of the GNU Lesser General Public
    License along with this library in the file named "LICENSE".
    If not, write to the Free Software Foundation, 51 Franklin Street, 
    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the 
    internet at http://www.fsf.org/licenses/lgpl.html.

Alternatively, the contents of this file may be used under the terms of the
Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
License, as published by the Free Software Foundation, either version 2
of the License or (at your option) any later version.
*/

#include <cstring>
#include <cstdarg>
#include "Main.h"
#include "XmlTraceLog.h"


using namespace graphite2;

#ifndef DISABLE_TRACING

/*static*/ XmlTraceLog XmlTraceLog::sm_NullLog(NULL, NULL, GRLOG_NONE);
XmlTraceLog * XmlTraceLog::sLog = &sm_NullLog;

XmlTraceLog::XmlTraceLog(FILE * file, const char * ns, GrLogMask logMask)
                         : m_file(file), m_depth(0), m_mask(logMask)
{
    if (!m_file) return;
    int deep = 0;
#ifdef ENABLE_DEEP_TRACING
    deep = 1;
#endif
    fprintf(m_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%s xmlns=\"%s\" mask=\"%x\" deep=\"%d\">",
            xmlTraceLogElements[ElementTopLevel].mName, ns, logMask, deep);
    m_elementStack[m_depth++] = ElementTopLevel;
    m_elementEmpty = true;
    m_inElement = false;
    m_lastNodeText = false;
}

XmlTraceLog::~XmlTraceLog()
{
    if (m_file && m_file != stdout && m_file != stderr)
    {
        assert(m_depth == 1);
        while (m_depth > 0)
        {
            closeElement(m_elementStack[m_depth-1]);
        }
        fclose(m_file);
        m_file = NULL;
    }
}

void XmlTraceLog::addSingleElement(XmlTraceLogElement eId, const int value)
{
    if (!m_file) return;
    if (m_inElement)
    {
        if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask)
            fprintf(m_file, ">");
    }
    if (xmlTraceLogElements[eId].mFlags & m_mask)
    {
        if (!m_lastNodeText)
        {
            fprintf(m_file, "\n");
            for (size_t i = 0; i < m_depth; i++)
            {
                fprintf(m_file, " ");
            }
        }
        fprintf(m_file, "<%s val=\"%d\"/>", xmlTraceLogElements[eId].mName, value);
    }
    m_inElement = false;
    m_lastNodeText = false;
}
    
void XmlTraceLog::writeElementArray(XmlTraceLogElement eId, XmlTraceLogAttribute aId, int16 values [], size_t length)
{
    if (!m_file) return;
    if (xmlTraceLogElements[eId].mFlags & m_mask)
    {
        if(m_inElement && xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask)
        {
            fprintf(m_file, ">");
            m_inElement = false;
        }
        // line break after 5 columns
        for (size_t i = 0; i < length; i++)
        {
            if (i % 5 == 0)
            {
                fprintf(m_file, "\n");
                for (size_t j = 0; j < m_depth; j++)
                {
                    fprintf(m_file, " ");
                }
            }
            fprintf(m_file, "<%s index=\"%d\" %s=\"%d\"/>", xmlTraceLogElements[eId].mName, int(i),
                xmlTraceLogAttributes[aId], (int)values[i]);
        }
    }
}

void XmlTraceLog::writeText(const char * utf8)
{
    if (!m_file) return;
    if (m_inElement)
    {
        if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask)
        {
            fprintf(m_file, ">");
        }
        m_inElement = false;
    }
    if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask)
    {
        escapeIfNeeded(utf8);
    }
    m_lastNodeText = true;
}

void XmlTraceLog::writeUnicode(const uint32 code)
{
    if (!m_file) return;
    if (m_inElement)
    {
        if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask)
        {
            fprintf(m_file, ">");
        }
        m_inElement = false;
    }
    if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask)
    {
        fprintf(m_file, "&#x%02x;", code);
    }
    m_lastNodeText = true;
}

void XmlTraceLog::escapeIfNeeded(const char * data)
{
    size_t length = strlen(data);
    for (size_t i = 0; i < length; i++)
    {
        switch (data[i])
        {
            case '<':
                fprintf(m_file, "&lt;");
                break;
            case '>':
                fprintf(m_file, "&gt;");
                break;
            case '&':
                fprintf(m_file, "&amp;");
                break;
            case '"':
                fprintf(m_file, "&#34;");
                break;
            default:
                fprintf(m_file, "%c", data[i]);
        }
    }
}

static const int MAX_MSG_LEN = 1024;

void XmlTraceLog::error(const char * msg, ...)
{
    if (!m_file) return;
    openElement(ElementError);
    va_list args;
    va_start(args, msg);
    char buffer[MAX_MSG_LEN];
#ifndef NDEBUG
    int len =
#endif
        vsnprintf(buffer, MAX_MSG_LEN, msg, args);
    assert(len + 1 < MAX_MSG_LEN);
    writeText(buffer);
    va_end(args);
    closeElement(ElementError);
}

void XmlTraceLog::warning(const char * msg, ...)
{
    if (!m_file) return;
    openElement(ElementWarning);
    va_list args;
    va_start(args, msg);
    char buffer[MAX_MSG_LEN];
#ifndef NDEBUG
    int len =
#endif
        vsnprintf(buffer, MAX_MSG_LEN, msg, args);
    assert(len + 1 < MAX_MSG_LEN);
    writeText(buffer);
    va_end(args);
    closeElement(ElementWarning);
}

#endif		//!DISABLE_TRACING