00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00032 #define LOG4SENDPP_NEED_EXPORTS
00033 #include <log4sendpp/log4sendpp.h>
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
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=\"\"";
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
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
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