/* * Copyright 2003,2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include using namespace log4cxx; using namespace log4cxx::helpers; using namespace log4cxx::spi; using namespace log4cxx::xml; IMPLEMENT_LOG4CXX_OBJECT(XMLLayout) String XMLLayout::LOCATION_INFO_OPTION = _T("LocationInfo"); XMLLayout::XMLLayout() : locationInfo(false) { } void XMLLayout::setOption(const String& option, const String& value) { if (StringHelper::equalsIgnoreCase(option, LOCATION_INFO_OPTION)) { setLocationInfo(OptionConverter::toBoolean(value, false)); } } void XMLLayout::format(ostream& output, const spi::LoggingEventPtr& event) const { output << _T("getLoggerName(); output << _T("\" timestamp=\""); output << event->getTimeStamp(); output << _T("\" level=\""); output << event->getLevel()->toString(); output << _T("\" thread=\""); output << event->getThreadId(); output << _T("\">") << std::endl; output << _T("getRenderedMessage()); output << _T("]]>") << std::endl; const String& ndc = event->getNDC(); if(ndc.length() != 0) { output << _T("") << std::endl; } std::set mdcKeySet = event->getMDCKeySet(); if(!mdcKeySet.empty()) { /** * Normally a sort isn't required, but for Test Case purposes * we need to guarantee a particular order. * * Besides which, from a human readable point of view, the sorting * of the keys is kinda nice.. */ output << _T("") << std::endl; for (std::set::iterator i = mdcKeySet.begin(); i != mdcKeySet.end(); i++) { String propName = *i; String propValue = event->getMDC(propName); output << _T(" ") << std::endl; } output << _T("") << std::endl; } if(locationInfo) { USES_CONVERSION; output << _T("getFile()); output << _T("\" line=\""); output << event->getLine(); output << _T("\"/>") << std::endl; } std::set propertySet = event->getPropertyKeySet(); if (!propertySet.empty()) { output << _T("\n"); for (std::set::iterator i = propertySet.begin(); i != propertySet.end(); i++) { String propName = *i; output << _T("getProperty(propName); output << _T("\" value=\"") << propValue; output << _T("\"/>") << std::endl; } output << _T("") << std::endl; } output << _T("") << std::endl; }