Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* GRAPHITE2 LICENSING |
michael@0 | 2 | |
michael@0 | 3 | Copyright 2010, SIL International |
michael@0 | 4 | All rights reserved. |
michael@0 | 5 | |
michael@0 | 6 | This library is free software; you can redistribute it and/or modify |
michael@0 | 7 | it under the terms of the GNU Lesser General Public License as published |
michael@0 | 8 | by the Free Software Foundation; either version 2.1 of License, or |
michael@0 | 9 | (at your option) any later version. |
michael@0 | 10 | |
michael@0 | 11 | This program is distributed in the hope that it will be useful, |
michael@0 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
michael@0 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
michael@0 | 14 | Lesser General Public License for more details. |
michael@0 | 15 | |
michael@0 | 16 | You should also have received a copy of the GNU Lesser General Public |
michael@0 | 17 | License along with this library in the file named "LICENSE". |
michael@0 | 18 | If not, write to the Free Software Foundation, 51 Franklin Street, |
michael@0 | 19 | Suite 500, Boston, MA 02110-1335, USA or visit their web page on the |
michael@0 | 20 | internet at http://www.fsf.org/licenses/lgpl.html. |
michael@0 | 21 | |
michael@0 | 22 | Alternatively, the contents of this file may be used under the terms of the |
michael@0 | 23 | Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public |
michael@0 | 24 | License, as published by the Free Software Foundation, either version 2 |
michael@0 | 25 | of the License or (at your option) any later version. |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | #include <cstring> |
michael@0 | 29 | #include <cstdarg> |
michael@0 | 30 | #include "Main.h" |
michael@0 | 31 | #include "XmlTraceLog.h" |
michael@0 | 32 | |
michael@0 | 33 | |
michael@0 | 34 | using namespace graphite2; |
michael@0 | 35 | |
michael@0 | 36 | #ifndef DISABLE_TRACING |
michael@0 | 37 | |
michael@0 | 38 | /*static*/ XmlTraceLog XmlTraceLog::sm_NullLog(NULL, NULL, GRLOG_NONE); |
michael@0 | 39 | XmlTraceLog * XmlTraceLog::sLog = &sm_NullLog; |
michael@0 | 40 | |
michael@0 | 41 | XmlTraceLog::XmlTraceLog(FILE * file, const char * ns, GrLogMask logMask) |
michael@0 | 42 | : m_file(file), m_depth(0), m_mask(logMask) |
michael@0 | 43 | { |
michael@0 | 44 | if (!m_file) return; |
michael@0 | 45 | int deep = 0; |
michael@0 | 46 | #ifdef ENABLE_DEEP_TRACING |
michael@0 | 47 | deep = 1; |
michael@0 | 48 | #endif |
michael@0 | 49 | fprintf(m_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<%s xmlns=\"%s\" mask=\"%x\" deep=\"%d\">", |
michael@0 | 50 | xmlTraceLogElements[ElementTopLevel].mName, ns, logMask, deep); |
michael@0 | 51 | m_elementStack[m_depth++] = ElementTopLevel; |
michael@0 | 52 | m_elementEmpty = true; |
michael@0 | 53 | m_inElement = false; |
michael@0 | 54 | m_lastNodeText = false; |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | XmlTraceLog::~XmlTraceLog() |
michael@0 | 58 | { |
michael@0 | 59 | if (m_file && m_file != stdout && m_file != stderr) |
michael@0 | 60 | { |
michael@0 | 61 | assert(m_depth == 1); |
michael@0 | 62 | while (m_depth > 0) |
michael@0 | 63 | { |
michael@0 | 64 | closeElement(m_elementStack[m_depth-1]); |
michael@0 | 65 | } |
michael@0 | 66 | fclose(m_file); |
michael@0 | 67 | m_file = NULL; |
michael@0 | 68 | } |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | void XmlTraceLog::addSingleElement(XmlTraceLogElement eId, const int value) |
michael@0 | 72 | { |
michael@0 | 73 | if (!m_file) return; |
michael@0 | 74 | if (m_inElement) |
michael@0 | 75 | { |
michael@0 | 76 | if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) |
michael@0 | 77 | fprintf(m_file, ">"); |
michael@0 | 78 | } |
michael@0 | 79 | if (xmlTraceLogElements[eId].mFlags & m_mask) |
michael@0 | 80 | { |
michael@0 | 81 | if (!m_lastNodeText) |
michael@0 | 82 | { |
michael@0 | 83 | fprintf(m_file, "\n"); |
michael@0 | 84 | for (size_t i = 0; i < m_depth; i++) |
michael@0 | 85 | { |
michael@0 | 86 | fprintf(m_file, " "); |
michael@0 | 87 | } |
michael@0 | 88 | } |
michael@0 | 89 | fprintf(m_file, "<%s val=\"%d\"/>", xmlTraceLogElements[eId].mName, value); |
michael@0 | 90 | } |
michael@0 | 91 | m_inElement = false; |
michael@0 | 92 | m_lastNodeText = false; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | void XmlTraceLog::writeElementArray(XmlTraceLogElement eId, XmlTraceLogAttribute aId, int16 values [], size_t length) |
michael@0 | 96 | { |
michael@0 | 97 | if (!m_file) return; |
michael@0 | 98 | if (xmlTraceLogElements[eId].mFlags & m_mask) |
michael@0 | 99 | { |
michael@0 | 100 | if(m_inElement && xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) |
michael@0 | 101 | { |
michael@0 | 102 | fprintf(m_file, ">"); |
michael@0 | 103 | m_inElement = false; |
michael@0 | 104 | } |
michael@0 | 105 | // line break after 5 columns |
michael@0 | 106 | for (size_t i = 0; i < length; i++) |
michael@0 | 107 | { |
michael@0 | 108 | if (i % 5 == 0) |
michael@0 | 109 | { |
michael@0 | 110 | fprintf(m_file, "\n"); |
michael@0 | 111 | for (size_t j = 0; j < m_depth; j++) |
michael@0 | 112 | { |
michael@0 | 113 | fprintf(m_file, " "); |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | fprintf(m_file, "<%s index=\"%d\" %s=\"%d\"/>", xmlTraceLogElements[eId].mName, int(i), |
michael@0 | 117 | xmlTraceLogAttributes[aId], (int)values[i]); |
michael@0 | 118 | } |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | void XmlTraceLog::writeText(const char * utf8) |
michael@0 | 123 | { |
michael@0 | 124 | if (!m_file) return; |
michael@0 | 125 | if (m_inElement) |
michael@0 | 126 | { |
michael@0 | 127 | if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) |
michael@0 | 128 | { |
michael@0 | 129 | fprintf(m_file, ">"); |
michael@0 | 130 | } |
michael@0 | 131 | m_inElement = false; |
michael@0 | 132 | } |
michael@0 | 133 | if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) |
michael@0 | 134 | { |
michael@0 | 135 | escapeIfNeeded(utf8); |
michael@0 | 136 | } |
michael@0 | 137 | m_lastNodeText = true; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | void XmlTraceLog::writeUnicode(const uint32 code) |
michael@0 | 141 | { |
michael@0 | 142 | if (!m_file) return; |
michael@0 | 143 | if (m_inElement) |
michael@0 | 144 | { |
michael@0 | 145 | if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) |
michael@0 | 146 | { |
michael@0 | 147 | fprintf(m_file, ">"); |
michael@0 | 148 | } |
michael@0 | 149 | m_inElement = false; |
michael@0 | 150 | } |
michael@0 | 151 | if (xmlTraceLogElements[m_elementStack[m_depth-1]].mFlags & m_mask) |
michael@0 | 152 | { |
michael@0 | 153 | fprintf(m_file, "&#x%02x;", code); |
michael@0 | 154 | } |
michael@0 | 155 | m_lastNodeText = true; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | void XmlTraceLog::escapeIfNeeded(const char * data) |
michael@0 | 159 | { |
michael@0 | 160 | size_t length = strlen(data); |
michael@0 | 161 | for (size_t i = 0; i < length; i++) |
michael@0 | 162 | { |
michael@0 | 163 | switch (data[i]) |
michael@0 | 164 | { |
michael@0 | 165 | case '<': |
michael@0 | 166 | fprintf(m_file, "<"); |
michael@0 | 167 | break; |
michael@0 | 168 | case '>': |
michael@0 | 169 | fprintf(m_file, ">"); |
michael@0 | 170 | break; |
michael@0 | 171 | case '&': |
michael@0 | 172 | fprintf(m_file, "&"); |
michael@0 | 173 | break; |
michael@0 | 174 | case '"': |
michael@0 | 175 | fprintf(m_file, """); |
michael@0 | 176 | break; |
michael@0 | 177 | default: |
michael@0 | 178 | fprintf(m_file, "%c", data[i]); |
michael@0 | 179 | } |
michael@0 | 180 | } |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | static const int MAX_MSG_LEN = 1024; |
michael@0 | 184 | |
michael@0 | 185 | void XmlTraceLog::error(const char * msg, ...) |
michael@0 | 186 | { |
michael@0 | 187 | if (!m_file) return; |
michael@0 | 188 | openElement(ElementError); |
michael@0 | 189 | va_list args; |
michael@0 | 190 | va_start(args, msg); |
michael@0 | 191 | char buffer[MAX_MSG_LEN]; |
michael@0 | 192 | #ifndef NDEBUG |
michael@0 | 193 | int len = |
michael@0 | 194 | #endif |
michael@0 | 195 | vsnprintf(buffer, MAX_MSG_LEN, msg, args); |
michael@0 | 196 | assert(len + 1 < MAX_MSG_LEN); |
michael@0 | 197 | writeText(buffer); |
michael@0 | 198 | va_end(args); |
michael@0 | 199 | closeElement(ElementError); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | void XmlTraceLog::warning(const char * msg, ...) |
michael@0 | 203 | { |
michael@0 | 204 | if (!m_file) return; |
michael@0 | 205 | openElement(ElementWarning); |
michael@0 | 206 | va_list args; |
michael@0 | 207 | va_start(args, msg); |
michael@0 | 208 | char buffer[MAX_MSG_LEN]; |
michael@0 | 209 | #ifndef NDEBUG |
michael@0 | 210 | int len = |
michael@0 | 211 | #endif |
michael@0 | 212 | vsnprintf(buffer, MAX_MSG_LEN, msg, args); |
michael@0 | 213 | assert(len + 1 < MAX_MSG_LEN); |
michael@0 | 214 | writeText(buffer); |
michael@0 | 215 | va_end(args); |
michael@0 | 216 | closeElement(ElementWarning); |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | #endif //!DISABLE_TRACING |