ulxr_log4j.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                ulxr_log4j.h  -  logging based on log4j
00003                              -------------------
00004     begin                : Fri Jul 13 2007
00005     copyright            : (C) 2002-2007 by Ewald Arnold
00006     email                : ulxmlrpcpp@ewald-arnold.de
00007 
00008     $Id: ulxr_Log4JSender.cpp 940 2006-12-30 18:22:05Z ewald-arnold $
00009 
00010  ***************************************************************************/
00011 
00012 /**************************************************************************
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 #define ULXR_NEED_EXPORTS
00031 #include <ulxmlrpcpp/ulxmlrpcpp.h>  // always first header
00032 
00033 #ifndef ULXR_OMIT_TCP_STUFF
00034 
00035 #include <ulxmlrpcpp/ulxr_tcpip_connection.h>
00036 #include <ulxmlrpcpp/ulxr_log4j.h>
00037 #include <ulxmlrpcpp/ulxr_except.h>
00038 #include <ulxmlrpcpp/ulxr_htmlform_handler.h>
00039 
00040 #ifndef ULXR_OMIT_REENTRANT_PROTECTOR
00041 #include <ulxmlrpcpp/ulxr_mutex.h>
00042 #endif
00043 
00044 #include <sys/timeb.h>
00045 
00046 #ifdef __BORLANDC__
00047 #include <strstream>
00048 #else
00049 #include <sstream>
00050 #endif
00051 
00052 #include <ctime>
00053 
00054 #ifdef __unix__
00055 #include <sys/time.h>
00056 #endif
00057 
00058 
00059 namespace ulxr {
00060 
00061 
00062 ULXR_API_IMPL0 Log4JSender::Log4JSender(const CppString &in_appname,
00063                                         TcpIpConnection &in_conn)
00064   : appname(in_appname)
00065   , conn(in_conn)
00066   , network_error(false)
00067   , threadname(ULXR_PCHAR("ulxr"))
00068   , seqnum(0)
00069   , disable_send(false)
00070   , first_log4j(false)
00071 
00072 {
00073   char buffer [1000];
00074   ::gethostname(buffer, sizeof(buffer)-1);
00075   hostname = buffer;
00076   open();
00077 }
00078 
00079 ULXR_API_IMPL0 Log4JSender::~Log4JSender() throw()
00080 {
00081 }
00082 
00083 
00084 void ULXR_API_IMPL0 Log4JSender::close()
00085 {
00086   disable_send = true;
00087   try
00088   {
00089     if (conn.isOpen())
00090       conn.close();
00091 
00092     network_error = false;
00093   }
00094   catch(...)
00095   {
00096     network_error = true;
00097   }
00098 }
00099 void ULXR_API_IMPL0 Log4JSender::open()
00100 {
00101   try
00102   {
00103     if (!conn.isOpen())
00104       conn.open();
00105 
00106     first_log4j = false;
00107     network_error = false;
00108     disable_send = false;
00109  }
00110   catch(...)
00111   {
00112     network_error = true;
00113   }
00114 }
00115 
00116 
00117 ULXR_INT64 ULXR_API_IMPL0 Log4JSender::currentTimeMillis()
00118 {
00119 #if defined(HAVE_GETTIMEOFDAY)
00120     timeval tp;
00121     ::gettimeofday(&tp, 0);
00122 
00123     return ((ULXR_INT64)tp.tv_sec * 1000) + (ULXR_INT64)(tp.tv_usec / 1000);
00124 #elif defined(HAVE_FTIME) || defined(HAVE_STD_FTIME)
00125     struct timeb tp;
00126     ::ftime(&tp);
00127 
00128     return ((ULXR_INT64)tp.time * 1000) + (ULXR_INT64)tp.millitm;
00129 #else
00130     return (ULXR_INT64)std::time(0) * 1000;
00131 #endif
00132 }
00133 
00134 
00135 void ULXR_API_IMPL0 Log4JSender::send(const CppString &level,
00136                                       const CppString &message,
00137                                       const CppString &in_filename,
00138                                       unsigned line)
00139 {
00140   if (network_error || disable_send)
00141     return;
00142 
00143   ++seqnum;
00144 
00145   Cpp8BitString s;
00146 
00147   if (!first_log4j)
00148   {
00149     first_log4j = true;
00150 //    s += "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
00151   }
00152 
00153   s += "<log4j:event logger=\"";
00154 #ifdef ULXR_UNICODE
00155   s += unicodeToUtf8(appname);
00156 #else
00157   s += asciiToUtf8(appname);
00158 #endif
00159 
00160   s += "\" timestamp=\"";
00161 #ifndef __BORLANDC__
00162   std::stringstream ts;
00163 #else
00164   std::strstream ts;
00165 #endif
00166   ts << currentTimeMillis();
00167   s += ts.str();
00168 
00169   s += "\" sequenceNumber=\"";
00170   s += getLatin1(HtmlFormHandler::makeNumber(seqnum));
00171 
00172   s += "\" level=\"";
00173 #ifdef ULXR_UNICODE
00174   s += unicodeToUtf8(level);
00175 #else
00176   s += asciiToUtf8(level);
00177 #endif
00178   s += "\" ";
00179 
00180   s += "thread=\"";
00181 #ifdef ULXR_UNICODE
00182   s += unicodeToUtf8(threadname);
00183 #else
00184   s += asciiToUtf8(threadname);
00185 #endif
00186   s += "\"";
00187 
00188   s += ">\n";
00189 
00190   s += "  <log4j:message>";
00191 #ifdef ULXR_UNICODE
00192   s += unicodeToUtf8(xmlEscape(message, true));
00193 #else
00194   s += asciiToUtf8(xmlEscape(message, true));
00195 #endif
00196   s += "</log4j:message>\n";
00197 
00198   classname = ULXR_PCHAR("");
00199   methodname = ULXR_PCHAR("");
00200 
00201   CppString filename = in_filename;
00202   // strip path from both unix and win
00203   if (filename.length() != 0)
00204   {
00205     unsigned pos;
00206     while ((pos = filename.find(ULXR_PCHAR("/"))) != CppString::npos)
00207       filename.erase(0, pos+1);
00208 
00209     while ((pos = filename.find(ULXR_PCHAR("\\"))) != CppString::npos)
00210       filename.erase(0, pos+1);
00211   }
00212 
00213   s += "  <log4j:locationInfo";
00214   s += " file=\"";
00215 #ifdef ULXR_UNICODE
00216   s += unicodeToUtf8(filename);
00217 #else
00218   s += asciiToUtf8(filename);
00219 #endif
00220   s += "\" line=\"";
00221   if (line != 0)
00222     s += getLatin1(HtmlFormHandler::makeNumber(line));
00223   s += "\" class=\"";
00224   s += getLatin1(classname);
00225   s += "\" method=\"";
00226   s += getLatin1(methodname);
00227   s += "\" />\n";
00228 
00229   s += "  <log4j:properties>\n";
00230   s +="    <log4j:data name=\"application\" value=\"";
00231 #ifdef ULXR_UNICODE
00232   s += unicodeToUtf8(appname);
00233 #else
00234   s += asciiToUtf8(appname);
00235 #endif
00236   s += "\" />\n";
00237 
00238   s += "    <log4j:data name=\"hostname\" value=\"";
00239   s += hostname;
00240   s += "\" />\n";
00241 
00242 //  s += "    <log4j:data name=\"log4jid\" value=\"27\" />\n";
00243   s += "  </log4j:properties>\n";
00244 
00245   s += "</log4j:event>\n";
00246 
00247   try
00248   {
00249     disable_send = true;
00250     conn.write(s.data(), s.length());
00251     disable_send = false;
00252   }
00253   catch(...)
00254   {
00255     network_error = true;
00256   }
00257 }
00258 
00259 #endif // ULXR_OMIT_TCP_STUFF
00260 
00261 }  // namespace ulxr
00262 

Generated on Sun Aug 19 20:08:57 2007 for ulxmlrpcpp by  doxygen 1.5.1