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