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

In This Package:

LogStream.cc

Go to the documentation of this file.
00001 #include "LogStream.hh"
00002 #include "Format.hh"
00003 #include "Manager.hh"
00004 #include <vector>
00005 #include <fstream>
00006 #include <iostream>
00007 #include <cassert>
00008 
00009 using namespace Logging;
00010 
00011 ClassImp(LogStream)
00012 
00013 Logging::LogStream::LogStream( std::string name )
00014 : mName(name)
00015 , mLevel(kEverything)
00016 , mOStream(&std::cout)
00017 {
00018 }
00019 
00020 Logging::LogStream::~LogStream()
00021 {
00022   // We own the formats:
00023   for(unsigned int i=0;i<mFormats.size();i++) 
00024     delete mFormats[i];
00025   mFormats.clear();
00026 }
00027 
00028 void Logging::LogStream::ResetFormat()
00029 {
00030   for(unsigned int i=0;i<mFormats.size();i++) 
00031     delete mFormats[i];
00032   mFormats.clear();
00033 }
00034 
00035 Logging::LogStream& 
00036 Logging::LogStream::AddFormat( Format* fmt )
00037 {
00038   mFormats.push_back(fmt);
00039   return (*this);
00040 }
00041 
00042 
00043 
00044 LogStream& Logging::LogStream::SetLevel( Level lvl )
00045 {
00046         mLevel = lvl;  
00047   return (*this);
00048 }
00049 
00050 
00051 
00052 LogStream& Logging::LogStream::SetOutput( std::string filename )
00053 {
00054   const char* cstr = filename.c_str();
00055         if( (0==strcasecmp(cstr,"cout")) 
00056       || (0==strcasecmp(cstr,"std::cout")) 
00057       || (0==strcasecmp(cstr,"stdout"))  ) {
00058     mOStream = &(std::cout);
00059     return (*this);
00060   } 
00061   if( (0==strcasecmp(cstr,"cerr")) 
00062       || (0==strcasecmp(cstr,"std::cerr")) 
00063       || (0==strcasecmp(cstr,"stderr"))  ) {
00064     mOStream = &(std::cerr);
00065     return (*this);
00066   } 
00067 
00068   // Open or get a handle to a log file:
00069   mOStream = Manager::Instance().GetOStream( filename );
00070   assert(mOStream);
00071   return (*this);
00072 }
00073 
00074 
00075 
00076 void Logging::LogStream::WriteEntry( LogEntry entry )
00077 {
00081   
00082   // Note the argument pulls a COPY of the LogEntry, so streams don't mess each other up.
00083   
00084   // First, is this message even printable by us?
00085   // If not, just abort. Let some other stream handle it.
00086         if(entry.mLevel < mLevel) return;
00087   
00088   // First, run all the formatters.
00089         bool good = true;
00090   for(unsigned int i=0;i<mFormats.size();i++) {
00091                 good = (*mFormats[i])(entry);
00092           if(!good) return; // Abort if one of the processing units aborts.
00093   }
00094   // Next, carve up the entry into lines.
00095   
00096   std::vector<std::string> lines;
00097   {
00098     std::string::size_type p1=0;
00099     std::string::size_type p2=entry.mMsg.find_first_of('\n');
00100     while(p2 != std::string::npos) {
00101       std::string l(entry.mMsg, p1, p2-p1);
00102       lines.push_back(l);
00103       p1=p2+1;
00104       p2 = entry.mMsg.find_first_of('\n',p1);
00105     }
00106     p2 = entry.mMsg.length();
00107           if(p2>p1) {
00108       std::string l(entry.mMsg,p1,p2-p1);
00109       lines.push_back(l);
00110     }
00111   }
00112   
00113   // Finally, write out each line, applying the prefix to the first line,
00114   //  indenting the rest of the lines, and putting the suffix on every line.
00115   // FIXME this should be configuable.
00116   assert(mOStream);
00117         for(unsigned int i=0;i<lines.size(); i++) {
00118     if(i==0) (*mOStream) << entry.mPrefix;
00119     else   {
00120       int spaces = entry.mPrefix.length() + entry.mIndentAdj;
00121       for(int is=0;is<spaces;is++) (*mOStream) << " ";
00122     }
00123     
00124     (*mOStream) << lines[i];
00125     (*mOStream) << std::endl;
00126   }
00127   (*mOStream) << entry.mSuffix;
00128   (*mOStream).flush();
00129 }
00130 
| 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