LibreOffice Module writerfilter (master) 1
TagLogger.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#include <string.h>
21#include "TagLogger.hxx"
22#ifdef DBG_UTIL
24#endif
25
26using namespace css;
27
28namespace writerfilter
29{
31 : m_pWriter( nullptr ), m_pName( "DOMAINMAPPER" )
32 {
33 }
34
36 {
37 m_pWriter = nullptr;
38 m_pName = nullptr;
39 }
40
41#ifdef DBG_UTIL
42 void TagLogger::setFileName( const std::string & filename )
43 {
44 if ( m_pWriter )
46
47 std::string fileName;
48 char * temp = getenv("TAGLOGGERTMP");
49
50 if (temp != nullptr)
51 fileName += temp;
52 else
53 fileName += SvtPathOptions().GetTempPath().toUtf8().getStr();
54
55 std::string sPrefix = filename;
56 size_t nLastSlash = sPrefix.find_last_of('/');
57 size_t nLastBackslash = sPrefix.find_last_of('\\');
58 size_t nCutPos = nLastSlash;
59 if (nLastBackslash < nCutPos)
60 nCutPos = nLastBackslash;
61 if (nCutPos < sPrefix.size())
62 sPrefix = sPrefix.substr(nCutPos + 1);
63
64 fileName += "/";
65 fileName += sPrefix;
66 fileName += ".";
67 fileName += m_pName;
68 fileName += ".xml";
69
70 m_pWriter = xmlNewTextWriterFilename( fileName.c_str(), 0 );
71 xmlTextWriterSetIndent(m_pWriter,1);
72 (void)xmlTextWriterSetIndentString(m_pWriter, BAD_CAST(" "));
73 xmlTextWriterSetIndent( m_pWriter, 4 );
74 }
75
77 {
78 if (!m_pWriter)
79 return;
80 (void)xmlTextWriterStartDocument( m_pWriter, nullptr, nullptr, nullptr );
81 (void)xmlTextWriterStartElement( m_pWriter, BAD_CAST( "root" ) );
82 }
83
85 {
86 if (!m_pWriter)
87 return;
88 (void)xmlTextWriterEndDocument( m_pWriter );
89 xmlFreeTextWriter( m_pWriter );
90 m_pWriter = nullptr;
91 }
92
93#endif
94
96 {
97 static TagLogger theTagLogger;
98 return theTagLogger;
99 }
100
101#ifdef DBG_UTIL
102 void TagLogger::element(const std::string & name)
103 {
105 endElement();
106 }
107
109 {
110 uno::Reference<beans::XPropertySetInfo> xPropSetInfo(rPropSet->getPropertySetInfo());
111 const uno::Sequence<beans::Property> aProps(xPropSetInfo->getProperties());
112
113 startElement( "unoPropertySet" );
114
115 for (beans::Property const & prop : aProps)
116 {
117 startElement( "property" );
118 OUString sName(prop.Name);
119
120 attribute( "name", sName );
121 try
122 {
123 attribute( "value", rPropSet->getPropertyValue( sName ) );
124 }
125 catch (const uno::Exception &)
126 {
127 startElement( "exception" );
128
129 chars(std::string("getPropertyValue(\""));
130 chars(sName);
131 chars(std::string("\")"));
132
133 endElement( );
134 }
135 endElement( );
136 }
137 endElement( );
138 }
139
140 void TagLogger::startElement(const std::string & name)
141 {
142 if (!m_pWriter)
143 return;
144 xmlChar* xmlName = xmlCharStrdup( name.c_str() );
145 (void)xmlTextWriterStartElement( m_pWriter, xmlName );
146 xmlFree( xmlName );
147 }
148#endif
149
150 void TagLogger::attribute(const std::string & name, const std::string & value)
151 {
152 if (!m_pWriter)
153 return;
154 xmlChar* xmlName = xmlCharStrdup( name.c_str() );
155 xmlChar* xmlValue = xmlCharStrdup( value.c_str() );
156 (void)xmlTextWriterWriteAttribute( m_pWriter, xmlName, xmlValue );
157
158 xmlFree( xmlValue );
159 xmlFree( xmlName );
160 }
161
162#ifdef DBG_UTIL
163 void TagLogger::attribute(const std::string & name, std::u16string_view value)
164 {
165 attribute( name, std::string(OUStringToOString( value, RTL_TEXTENCODING_ASCII_US )) );
166 }
167
168 void TagLogger::attribute(const std::string & name, sal_uInt32 value)
169 {
170 if (!m_pWriter)
171 return;
172 xmlChar* xmlName = xmlCharStrdup( name.c_str() );
173 (void)xmlTextWriterWriteFormatAttribute( m_pWriter, xmlName,
174 "%" SAL_PRIuUINT32, value );
175 xmlFree( xmlName );
176 }
177
178 void TagLogger::attribute(const std::string & name, float value)
179 {
180 if (!m_pWriter)
181 return;
182 xmlChar* xmlName = xmlCharStrdup( name.c_str() );
183 (void)xmlTextWriterWriteFormatAttribute( m_pWriter, xmlName,
184 "%f", value );
185 xmlFree( xmlName );
186 }
187
188 void TagLogger::attribute(const std::string & name, const uno::Any& aAny)
189 {
190 if (!m_pWriter)
191 return;
192
193 sal_Int32 nInt = 0;
194 float nFloat = 0.0;
195 OUString aStr;
196
197 xmlChar* xmlName = xmlCharStrdup( name.c_str() );
198 if ( aAny >>= nInt )
199 {
200 (void)xmlTextWriterWriteFormatAttribute( m_pWriter, xmlName,
201 "%" SAL_PRIdINT32, nInt );
202 }
203 else if ( aAny >>= nFloat )
204 {
205 (void)xmlTextWriterWriteFormatAttribute( m_pWriter, xmlName,
206 "%f", nFloat );
207 }
208 else if ( aAny >>= aStr )
209 {
210 attribute( name, aStr );
211 }
212 xmlFree( xmlName );
213 }
214
215 void TagLogger::chars(const std::string & rChars)
216 {
217 if (!m_pWriter)
218 return;
219 xmlChar* xmlChars = xmlCharStrdup( rChars.c_str() );
220 (void)xmlTextWriterWriteString( m_pWriter, xmlChars );
221 xmlFree( xmlChars );
222 }
223
224 void TagLogger::chars(std::u16string_view rChars)
225 {
226 chars(std::string(OUStringToOString(rChars, RTL_TEXTENCODING_ASCII_US)));
227 }
228
230 {
231 if (!m_pWriter)
232 return;
233 (void)xmlTextWriterEndElement( m_pWriter );
234 }
235
236#endif
237
238}
239
240/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString & GetTempPath() const
const char * m_pName
Definition: TagLogger.hxx:37
static TagLogger & getInstance()
Definition: TagLogger.cxx:95
void startElement(const std::string &name)
Definition: TagLogger.cxx:140
void chars(const std::string &chars)
Definition: TagLogger.cxx:215
xmlTextWriterPtr m_pWriter
Definition: TagLogger.hxx:36
void attribute(const std::string &name, const std::string &value)
Definition: TagLogger.cxx:150
void element(const std::string &name)
Definition: TagLogger.cxx:102
void unoPropertySet(const css::uno::Reference< css::beans::XPropertySet > &rPropSet)
Definition: TagLogger.cxx:108
void setFileName(const std::string &filename)
Definition: TagLogger.cxx:42
Any value
OUString sName
OUString sPrefix
const char * name
aStr
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)