xmlformatter.cpp

Go to the documentation of this file.
00001 
00008 /**************************************************************************
00009 
00010    begin                : Mon Sep 11 2007
00011    copyright            : (C) 2007 by Ewald Arnold
00012    email                : log4sendpp at ewald-arnold dot de
00013 
00014    This program is free software; you can redistribute it and/or modify
00015    it under the terms of the GNU Lesser General Public License as
00016    published by the Free Software Foundation; either version 2 of the License,
00017    or (at your option) any later version.
00018 
00019    This program is distributed in the hope that it will be useful,
00020    but WITHOUT ANY WARRANTY; without even the implied warranty of
00021    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022    GNU General Public License for more details.
00023 
00024    You should have received a copy of the GNU Lesser General Public License
00025    along with this program; if not, write to the Free Software
00026    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00027 
00028  **/
00029 
00030 
00032 #define LOG4SENDPP_NEED_EXPORTS
00033 #include <log4sendpp/log4sendpp.h>  // always first header
00034 
00035 #include <cctype>
00036 #include <sstream>
00037 
00038 #ifdef __unix__
00039 #include <unistd.h>
00040 #endif
00041 
00042 #include <log4sendpp/xmlformatter.h>
00043 #include <log4sendpp/locationinfo.h>
00044 #include <log4sendpp/arguments.h>
00045 #include <log4sendpp/logger.h>
00046 #include <log4sendpp/tcpip_appender.h>
00047 
00048 
00049 LOG4SENDPP_NS_START
00050 
00051 
00052 LOG4SENDPP_API_IMPL0 XmlFormatter::XmlFormatter(const LOG4SENDPP_STD_NS::string &in_appname,
00053                                                 const LOG4SENDPP_STD_NS::string &in_hostname)
00054   : first(true)
00055   , seqnum(0)
00056   , appname(in_appname)
00057   , hostname(in_hostname)
00058 {
00059 }
00060 
00061 
00062 LOG4SENDPP_API_IMPL0 XmlFormatter::XmlFormatter(const LOG4SENDPP_STD_NS::string &in_appname)
00063   : first(true)
00064   , seqnum(0)
00065   , appname(in_appname)
00066 {
00067   hostname = TcpIpAppender::gethostname();
00068 }
00069 
00070 
00071 LOG4SENDPP_API_IMPL(LOG4SENDPP_STD_NS::string) XmlFormatter::xmlEscape(const LOG4SENDPP_STD_NS::string &msg)
00072 {
00073    return msg;
00074 }
00075 
00076 
00077 LOG4SENDPP_API_IMPL(LOG4SENDPP_STD_NS::vector<LOG4SENDPP_STD_NS::string>)
00078   XmlFormatter::format (Logger::Level level,
00079                         const LOG4SENDPP_STD_NS::string &msg,
00080                         LOG4SENDPP_STD_NS::string category,
00081                         LOG4SENDPP_INT64 stamp,
00082                         const LocationInformation *locationinfo,
00083                         const Logger::DiagnosticInformation *diaginfo)
00084 {
00085   ++seqnum;
00086 
00087   LOG4SENDPP_STD_NS::string s;
00088   LOG4SENDPP_STD_NS::vector<LOG4SENDPP_STD_NS::string> sl;
00089 
00090   if (first)
00091   {
00092     first = false;
00093 //    s += "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
00094   }
00095 
00096   s += "<log4j:event logger=\"";
00097   s += category;
00098 
00099   s += "\" timestamp=\"";
00100   LOG4SENDPP_STD_NS::stringstream ts;
00101   ts << stamp;
00102   s += ts.str();
00103 
00104   s += "\" sequenceNumber=\"";
00105   s += number(seqnum);
00106 
00107   s += "\" level=\"";
00108   switch(level)
00109   {
00110     case Logger::Trace:
00111        s += "TRACE";
00112     break;
00113 
00114     case Logger::Debug:
00115        s += "DEBUG";
00116     break;
00117 
00118     case Logger::Info:
00119        s += "INFO";
00120     break;
00121 
00122     case Logger::Warning:
00123        s += "WARN";
00124     break;
00125 
00126     case Logger::Error:
00127        s += "ERROR";
00128     break;
00129 
00130     case Logger::Fatal:
00131        s += "FATAL";
00132     break;
00133 
00134     default:
00135        s += "UNKNOWN";
00136   };
00137 
00138   s += "\" ";
00139 
00140   if (locationinfo != 0)
00141   {
00142     s += "thread=\"";
00143     s += locationinfo->getThreadname();
00144     s += "\"";
00145   }
00146   else
00147     s += "thread=\"\""; // field is mandatory
00148 
00149   s += ">";
00150   sl.push_back(s);
00151   s.clear();
00152 
00153   s += "  <log4j:message>";
00154   s += xmlEscape(msg);
00155   s += "</log4j:message>";
00156   sl.push_back(s);
00157   s.clear();
00158 
00159 
00160   if (locationinfo != 0 && locationinfo->getThrowablename().length() != 0)
00161   {
00162     s += "  <log4j:throwable>";
00163     s += locationinfo->getThrowablename();
00164     s += "</log4j:throwable>";
00165     sl.push_back(s);
00166     s.clear();
00167   }
00168 
00169   if(diaginfo != 0 && diaginfo->ndc != 0 && diaginfo->ndc->numNDCs() != 0)
00170   {
00171     s += "  <log4j:NDC>";
00172     s += xmlEscape(diaginfo->ndc->get());
00173     s += "</log4j:NDC>";
00174     sl.push_back(s);
00175     s.clear();
00176   }
00177 
00178   if(diaginfo != 0 && diaginfo->mdc != 0 && diaginfo->mdc->numMDCs() != 0)
00179   {
00180     sl.push_back("  <log4j:MDC>");
00181     LOG4SENDPP_STD_NS::vector<MDC::Pair> pairs = diaginfo->mdc->get();
00182 
00183     for (unsigned i = 0; i < pairs.size(); ++i)
00184     {
00185       s = "    <log4j:data name=\"";
00186       s += pairs[i].first;
00187       s += "\" value=\"";
00188       s += pairs[i].second;
00189       s += "\" />";
00190       sl.push_back(s);
00191       s.clear();
00192     }
00193     sl.push_back("  </log4j:MDC>");
00194   }
00195 
00196   if (locationinfo != 0)
00197   {
00198     s += "  <log4j:locationInfo";
00199     s += " file=\"";
00200     s += locationinfo->getFilename();
00201     s += "\" line=\"";
00202     s += number((long)locationinfo->getLinenumber());
00203     s += "\" class=\"";
00204 //    s += locationinfo->getClassname();
00205     s += "\" method=\"";
00206     s += locationinfo->getMethodname();
00207     s += "\" />";
00208     sl.push_back(s);
00209     s.clear();
00210   }
00211 
00212   s += "  <log4j:properties>";
00213   sl.push_back(s);
00214   s.clear();
00215 
00216   s +="    <log4j:data name=\"application\" value=\"";
00217   s += appname;
00218   s += "\" />";
00219   sl.push_back(s);
00220   s.clear();
00221 
00222   s += "    <log4j:data name=\"hostname\" value=\"";
00223   s += hostname;
00224   s += "\" />";
00225   sl.push_back(s);
00226   s.clear();
00227 
00228   if(diaginfo != 0 && diaginfo->property != 0 && diaginfo->property->numProperties() != 0)
00229   {
00230     LOG4SENDPP_STD_NS::vector<Property::Pair> pairs = diaginfo->property->get();
00231 
00232     for (unsigned i = 0; i < pairs.size(); ++i)
00233     {
00234       s = "    <log4j:data name=\"";
00235       s += pairs[i].first;
00236       s += "\" value=\"";
00237       s += pairs[i].second;
00238       s += "\" />";
00239       sl.push_back(s);
00240       s.clear();
00241     }
00242   }
00243 
00244 //  s += "    <log4j:data name=\"log4jid\" value=\"27\" />";
00245   s += "  </log4j:properties>";
00246   sl.push_back(s);
00247   s.clear();
00248 
00249   s += "</log4j:event>";
00250   sl.push_back(s);
00251   s.clear();
00252 
00253   return sl;
00254 }
00255 
00256 
00257 LOG4SENDPP_NS_END
00258 

Generated on Sat Nov 24 14:41:22 2007 for log4sendpp by  doxygen 1.5.3