| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Format.cc

Go to the documentation of this file.
00001 #include "Format.hh"
00002 #include "FormString.hh"
00003 #include <sys/time.h>
00004 #include <time.h>
00005 #include <stdlib.h>
00006 // for gettimeofday
00007 
00008 using namespace Logging;
00009 
00010 // Imps so that ROOT can see these.
00011 
00012         /* hxt ClassImp(Format)
00013 ClassImp(FmtLevelString)
00014 ClassImp(FmtFileName)
00015 ClassImp(FmtFilePath)
00016 ClassImp(FmtLineNumber)
00017 ClassImp(FmtLogName)
00018 ClassImp(FmtDate)
00019 ClassImp(FmtTime)
00020 ClassImp(FmtLevelColor)
00021 ClassImp(FmtHtml)
00022 ClassImp(FmtXml)
00023 ClassImp(FmtSpace)
00024 ClassImp(FmtTab)
00025 ClassImp(FmtBar)
00026 ClassImp(FmtPadTo)
00027 */
00028 
00029 using std::string;
00030 
00031 Logging::Format::~Format ()
00032 {
00033 }
00034 
00035 
00036 // Here is a complete set of ANSI/Xterm control sequences
00037 // for font color and style
00038 
00039 const char kColor_Reset[]    = { 0x1B, '[', '0', 'm', 0 };
00040 const char kColor_Bold[]     = { 0x1B, '[', '1', 'm', 0 };
00041 const char kColor_Dim[]      = { 0x1B, '[', '2', 'm', 0 };
00042 const char kColor_Underline[]= { 0x1B, '[', '3', 'm', 0 };
00043 const char kColor_Blink[]    = { 0x1B, '[', '5', 'm', 0 };
00044 const char kColor_Reverse[]  = { 0x1B, '[', '7', 'm', 0 };
00045 
00046 const char kColor_Black[]    = { 0x1B, '[', '3', '0', 'm', 0 };
00047 const char kColor_Red[]      = { 0x1B, '[', '3', '1', 'm', 0 };
00048 const char kColor_Green[]    = { 0x1B, '[', '3', '2', 'm', 0 };
00049 const char kColor_Yellow[]   = { 0x1B, '[', '3', '3', 'm', 0 };
00050 const char kColor_Blue[]     = { 0x1B, '[', '3', '4', 'm', 0 };
00051 const char kColor_Magenta[]  = { 0x1B, '[', '3', '5', 'm', 0 };
00052 const char kColor_Cyan[]     = { 0x1B, '[', '3', '6', 'm', 0 };
00053 const char kColor_White[]    = { 0x1B, '[', '3', '7', 'm', 0 };
00054 
00055 const char kColor_BgWhite[]    = { 0x1B, '[', '4', '0', 'm', 0 };
00056 const char kColor_BgRed[]      = { 0x1B, '[', '4', '1', 'm', 0 };
00057 const char kColor_BgGreen[]    = { 0x1B, '[', '4', '2', 'm', 0 };
00058 const char kColor_BgYellow[]   = { 0x1B, '[', '4', '3', 'm', 0 };
00059 const char kColor_BgBlue[]     = { 0x1B, '[', '4', '4', 'm', 0 };
00060 const char kColor_BgMagenta[]  = { 0x1B, '[', '4', '5', 'm', 0 };
00061 const char kColor_BgCyan[]     = { 0x1B, '[', '4', '6', 'm', 0 };
00062 const char kColor_BgBlack[]    = { 0x1B, '[', '4', '7', 'm', 0 };
00063 
00064 
00065 bool FmtDate::operator()( LogEntry& entry ) 
00066 {
00067   static struct timeval tv;
00068   gettimeofday(&tv,NULL);
00069   struct tm* ts = localtime(&(tv.tv_sec));
00070 
00071   entry.mPrefix += FormString("%04d%02d%02d ",ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday);
00072   
00073   return true;
00074 }
00075 
00076 bool FmtTime::operator()( LogEntry& entry ) 
00077 {
00078   static struct timeval tv;
00079   gettimeofday(&tv,NULL);
00080   struct tm* ts = localtime(&(tv.tv_sec));
00081   
00082   entry.mPrefix += FormString("%02d:%02d:%02d ",
00083                               ts->tm_hour, ts->tm_min, ts->tm_sec);
00084   return true;
00085 }
00086 
00087 bool FmtLevelColor::operator()( LogEntry& entry ) 
00088 {
00089   string s;
00090   if(entry.mLevel>=kFatal)     { s += kColor_Reverse; }
00091   else if(entry.mLevel>=kError) { s += kColor_Bold; s += kColor_Magenta; }
00092   else if(entry.mLevel>=kWarning) { s += kColor_Bold; }
00093   else if(entry.mLevel>=kInfo) { s += kColor_Reset; }
00094   else if(entry.mLevel>=kMonitor) { s += kColor_Blue; }
00095   else if(entry.mLevel>=kDebug1) { s += kColor_Cyan; }
00096   else if(entry.mLevel>=kDebug2) { s += kColor_Blue; }
00097   else if(entry.mLevel>=kDebug3) { s += kColor_Dim; s+= kColor_Blue; }
00098   else  { s +=  kColor_Dim; s+= kColor_Blue; }
00099 
00100   entry.mPrefix += s;
00101   entry.mIndentAdj -= s.length(); // These take up no columns.
00102   entry.mSuffix.insert(0,kColor_Reset);
00103   return true;
00104 }
00105 
00106 
00107 bool FmtPadTo::operator()( LogEntry& entry ) 
00108 {
00110   int len = entry.mPrefix.length() + entry.mIndentAdj;
00111   for(int i=0;i<mTo-len;i++) entry.mPrefix+=' ';
00112   return true;
00113 }
00114 
00116 // HTML
00118 
00119 
00120 string FmtHtml::Header()
00121 {
00122   string out;
00123   out += "<html>\n";
00124   out += "<head>\n";                                
00125   out += "<style type=\"text/css\">\n";
00126   out += ".Fatal  { color:#C31317 }\n";
00127   out += ".Error  { color:#CD6736 }\n";
00128   out += ".Warning{ color:#C5C215 }\n";  
00129   out += ".Info   { color:#000    }\n";  
00130   out += ".Debug  { color:#559914 }\n";  
00131   out += ".Verbose{ color:#C3C3C3 }\n";  
00132   out += "</style>\n";    
00133   out += "</head>\n";                              
00134   out += "<body>\n";
00135   out += "<table bordersize=\"1\">\n";
00136   return out;
00137 }
00138 
00139 string FmtHtml::Footer()
00140 {
00141   string out;
00142   out += "</table>\n";
00143   out += "</body>\n";
00144   out += "</html>\n";                               
00145   return out;
00146 
00147 }
00148 
00149 bool FmtHtml::operator()( LogEntry& entry ) 
00150 {  
00152   const string kurl = "http://minimac1.phy.tufts.edu/~tagg/doc/";
00153   //const string kurl = "file:///Users/tagg/daya/InstallArea/doc/";
00154   
00155   entry.mPrefix.insert(0,"<tr><td>");
00156   entry.mPrefix += "</td><td>";
00157   // Add the filename and link.
00158   if(entry.mFilename) {
00159     string filepath(entry.mFilename);
00160     // Get the name of the file:
00161     string::size_type n =  filepath.length();
00162     string::size_type p1 = filepath.find_last_of('/');
00163     string::size_type p2 = filepath.find_last_of('/',p1-1);
00164     string::size_type p3 = filepath.find_last_of('/',p2-1);
00165     if(p1==string::npos) p1=0;
00166     if(p2==string::npos) p2=0;
00167     if(p3==string::npos) p3=0;
00168     string filename(filepath,p1+1,n-p1);
00169     string pkgname(filepath,p3+1,p2-p3);
00170     string link;
00171     
00172     string filelink = filename;
00173     filelink.replace(filename.find("."),1,"_8");
00174     filelink += "-source.html";
00175     
00176     link += "<a href=\"";
00177     link += kurl;
00178     link += pkgname;
00179     link += "/html/";
00180     link += filelink;
00181     link += "#";
00182     link += FormString("%d",10000 + entry.mLine);
00183     link += "\">";
00184     link += filename;
00185     link += ":";
00186     link += FormString("%d",entry.mLine);
00187     link += "</a></td><td>";
00188     
00189     entry.mPrefix += link;
00190   } else {
00191     entry.mPrefix += "</td><td>";
00192   }
00193   
00194   string classname;
00195   if(entry.mLevel>=kFatal)        { classname = "Fatal"; }
00196   else if(entry.mLevel>=kError)   { classname = "Error"; }
00197   else if(entry.mLevel>=kWarning) { classname = "Warning"; }
00198   else if(entry.mLevel>=kInfo)    { classname = "Info"; }
00199   else if(entry.mLevel>=kMonitor) { classname = "Monitor"; }
00200   else if(entry.mLevel>=kDebug3)  { classname = "Debug"; }
00201   else                            { classname = "Verbose"; }
00202   
00203   // Replace line breaks with <br/>
00204   string::size_type p;
00205   while( (p = entry.mMsg.find('\n')) != string::npos ) {
00206     entry.mMsg.replace(p,1,"<br/>");
00207   }
00208   
00209   entry.mPrefix += "</td><td class=\"" + classname + "\">";
00210   entry.mSuffix += "</td></tr>";
00211   return true;
00212 }
00213 
00214 
00216 // XML
00218 
00219 
00220 string FmtXml::Header()
00221 {
00222   string header="<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
00223   header+= "<log ";
00224   // date="20070101 12:01:01"
00225   static struct timeval tv;
00226   gettimeofday(&tv,NULL);
00227   struct tm* ts = localtime(&(tv.tv_sec));
00228   header += FormString(" date=\"%04d%02d%02d %02d:%02d:%02d\"",
00229                     ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday,
00230                     ts->tm_hour, ts->tm_min, ts->tm_sec);
00231 
00232   // stamp="123129080" (seconds since epoch)
00233   header += FormString(" stamp=\"%d\"",tv.tv_sec);
00234   header += " host=\"";
00235   const char* hostname = getenv("HOSTNAME");
00236   if(hostname) header += hostname;
00237   header += "\"";
00238   header += ">\n";
00239   return header;
00240 }
00241 
00242 string FmtXml::Footer()
00243 {
00244   // Tag it to show the file was closed.
00245   const string kFooter="</log>\n";
00246   return kFooter;
00247 }
00248 
00249 bool FmtXml::operator()( LogEntry& entry ) 
00250 {  
00252   
00253   // Format:
00254   // <logentry log="my_log" lvl="100" file="SomeFile.cc" line="99" date="20070101 12:01:01" stamp="123456789">
00255   // <![CDATA[  =W= Argh! This is an annoying log message! ]]>
00256   // </logentry>
00257   
00258   std::string tag = "<logentry ";
00259   // log="log_name"
00260   tag += " log=\"";
00261   tag += entry.mLog.GetName();
00262   tag += "\"";
00263 
00264   // lvl="100"
00265   tag += FormString(" lvl=\"%d\"",(int)entry.mLevel);
00266   
00267   // file="SomePkg/src/SomeFile.cc"
00268   // unfortunately, impossible to know how far down to dig...
00269   tag += " file=\"";
00270   string filepath;
00271   if(entry.mFilename) {
00272     filepath = entry.mFilename;
00273     string::size_type p = filepath.find_last_of('/');
00274     p = filepath.find_last_of('/',p-1);
00275     p = filepath.find_last_of('/',p-1);
00276     if(p != string::npos) filepath = filepath.substr(p+1);
00277   }
00278   tag += filepath + "\"";
00279 
00280   // line="99"
00281   tag += FormString(" line=\"%d\"",(int)entry.mLine);
00282 
00283   // date="20070101 12:01:01"
00284   static struct timeval tv;
00285   gettimeofday(&tv,NULL);
00286   struct tm* ts = localtime(&(tv.tv_sec));
00287   tag += FormString(" date=\"%04d%02d%02d %02d:%02d:%02d\"",
00288                     ts->tm_year+1900, ts->tm_mon+1, ts->tm_mday,
00289                     ts->tm_hour, ts->tm_min, ts->tm_sec);
00290 
00291   // stamp="123129080" (seconds since epoch)
00292   tag += FormString(" stamp=\"%d\"",tv.tv_sec);
00293   tag += ">";
00294 
00295   tag += "<![CDATA[";
00296 
00297   entry.mPrefix.insert(0,tag);
00298   entry.mIndentAdj -= tag.length(); // These take up no columns.
00299   
00300   // Can't put this in the suffix, because it will be put after every line break.
00301   entry.mMsg += "]]> </logentry>\n";
00302   
00303   return true;
00304 }
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:06:22 2011 for Log by doxygen 1.4.7