simpleformatter.cpp

Go to the documentation of this file.
00001 
00008 /**************************************************************************
00009 
00010    begin                : Thu 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 <log4sendpp/tcpip_appender.h>
00036 #include <log4sendpp/simpleformatter.h>
00037 #include <log4sendpp/locationinfo.h>
00038 #include <log4sendpp/arguments.h>
00039 #include <log4sendpp/logger.h>
00040 
00041 #include <ctime>
00042 
00043 LOG4SENDPP_NS_START
00044 
00045 
00046 LOG4SENDPP_API_IMPL0 SimpleFormatter::SimpleFormatter(const LOG4SENDPP_STD_NS::string &in_appname,
00047                                                       const LOG4SENDPP_STD_NS::string &in_hostname)
00048   : appname(in_appname)
00049   , hostname(in_hostname)
00050 {
00051 }
00052 
00053 
00054 LOG4SENDPP_API_IMPL0 SimpleFormatter::SimpleFormatter(const LOG4SENDPP_STD_NS::string &in_appname)
00055   : appname(in_appname)
00056 {
00057   hostname = TcpIpAppender::gethostname();
00058 }
00059 
00060 
00061 LOG4SENDPP_API_IMPL(LOG4SENDPP_STD_NS::vector<LOG4SENDPP_STD_NS::string>)
00062   SimpleFormatter::format (Logger::Level level,
00063                            const LOG4SENDPP_STD_NS::string &in_msg,
00064                            LOG4SENDPP_STD_NS::string category,
00065                            LOG4SENDPP_INT64 stamp,
00066                            const LocationInformation */*locationinfo*/,
00067                            const Logger::DiagnosticInformation */*diaginfo*/)
00068 {
00069   static const char dectab [] = "0123456789??????????????????????????????????????????";
00070 
00071   LOG4SENDPP_STD_NS::string s;
00072   LOG4SENDPP_STD_NS::vector<LOG4SENDPP_STD_NS::string> sl;
00073 
00074   s = "hh:mm:ss,SSS";
00075 
00076   LOG4SENDPP_STD_NS::time_t time = stamp / 1000;
00077   struct LOG4SENDPP_STD_NS::tm *tblock;
00078   tblock = LOG4SENDPP_STD_NS::localtime(&time);
00079 
00080   s[0] = dectab[tblock->tm_hour / 10];
00081   s[1] = dectab[tblock->tm_hour % 10];
00082 
00083   s[3] = dectab[tblock->tm_min / 10];
00084   s[4] = dectab[tblock->tm_min % 10];
00085 
00086   s[6] = dectab[tblock->tm_sec / 10];
00087   s[7] = dectab[tblock->tm_sec % 10];
00088 
00089   unsigned ms = stamp % 1000;
00090   s[9] = dectab[ms / 100];
00091   ms %= 100;
00092   s[10] = dectab[ms / 100];
00093   s[11] = dectab[ms % 10];
00094 
00095   switch(level)
00096   {
00097     case Logger::Trace:
00098        s += " TRACE ";
00099     break;
00100 
00101     case Logger::Debug:
00102        s += " DEBUG ";
00103     break;
00104 
00105     case Logger::Info:
00106        s += " INFO  ";
00107     break;
00108 
00109     case Logger::Warning:
00110        s += " WARN  ";
00111     break;
00112 
00113     case Logger::Error:
00114        s += " ERROR ";
00115     break;
00116 
00117     case Logger::Fatal:
00118        s += " FATAL ";
00119     break;
00120 
00121     default:
00122        s += " ????? ";
00123   };
00124 
00125   LOG4SENDPP_STD_NS::string msg = in_msg;
00126   LOG4SENDPP_STD_NS::vector<LOG4SENDPP_STD_NS::string> msgs;
00127   unsigned pos;
00128 
00129   while ((pos = msg.find('\n')) !=  LOG4SENDPP_STD_NS::string::npos)
00130   {
00131     msgs.push_back(msg.substr(0, pos));
00132     msg.erase(0, pos+1);
00133   }
00134 
00135   if (msg.length() != 0)
00136     msgs.push_back(msg);
00137 
00138   if (msgs.size() == 1)
00139     sl.push_back(LOG4SENDPP_STD_NS::string("[ ") + s + "] " + msgs[0]);
00140 
00141   else
00142   {
00143     sl.push_back(LOG4SENDPP_STD_NS::string("[ ") + s + ". " + msgs[0]);
00144 
00145     for (unsigned i = 1; i < msgs.size()-1; ++i)
00146       sl.push_back(LOG4SENDPP_STD_NS::string(". ") + s + ". " + msgs[i]);
00147 
00148     sl.push_back(LOG4SENDPP_STD_NS::string(". ") + s + "] " + msgs[msgs.size()-1]);
00149   }
00150 
00151   return sl;
00152 }
00153 
00154 
00155 LOG4SENDPP_NS_END
00156 

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