Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

JobCMsgModule.cxx

Go to the documentation of this file.
00001 
00002 // $Id: JobCMsgModule.cxx,v 1.10 2005/03/03 19:34:39 tagg Exp $
00003 //
00004 // A job module to configure the message service
00005 //
00006 // messier@huhepl.harvard.edu
00008 #include "JobControl/JobCMsgModule.h"
00009 #include <list>
00010 #include <string>
00011 #include <cstring> // strcmp, strlen
00012 #include "MessageService/MsgService.h"
00013 #include "JobControl/JobCommand.h"
00014 #include "JobControl/JobCModuleRegistry.h" // JOBCMODULE macro
00015 
00016 CVSID("$Id: JobCMsgModule.cxx,v 1.10 2005/03/03 19:34:39 tagg Exp $");
00017 JOBMODULE(JobCMsgModule,"Msg","Configure the message service");
00018 
00019 //......................................................................
00020 
00021 static void sorry() 
00022 {
00023   MSG("JobC", Msg::kWarning) << "Sorry, command not implemented\n";
00024 }
00025 
00026 //......................................................................
00027 
00028 JobCMsgModule::JobCMsgModule() {}
00029 
00030 //......................................................................
00031 
00032 JobCMsgModule::~JobCMsgModule() {}
00033 
00034 //......................................................................
00035 
00036 void JobCMsgModule::Attach(const char* msgstream, const char* os)
00037 {
00038 //======================================================================
00039 // Purpose: Attach an output stream to a given message stream. Expected
00040 // order of options: <msg_stream_name>::<log_level> <output_stream>
00041 //  
00042 // Inputs: cmd - the job command
00043 //======================================================================
00044   if (msgstream == 0 || os == 0 ) {
00045     MSG("JobC", Msg::kWarning) <<
00046       "usage: /" << this->GetName() << "/Attach <stream> <level> <ostream>\n";
00047     return;
00048   }
00049   else {
00050     string stream; // Which stream?
00051     string level;  // Which priority?
00052     MsgStream *m;
00053     
00054     JobCommand::SplitLine(msgstream, ':', stream, level);
00055     Msg::LogLevel_t lvl = Msg::GetLevelCode(level.c_str());
00056     
00057     m = MsgService::Instance()->GetStream(stream.c_str());
00058     m->AttachOStream(lvl,os);
00059   }
00060 }
00061 
00062 //......................................................................
00063 
00064 void JobCMsgModule::Concat()
00065 { 
00066   sorry(); 
00067 }
00068 
00069 //......................................................................
00070 
00071 void JobCMsgModule::Format(const char* msgstream,
00072                            const char* fmtflags,
00073                            bool raiseFlag)
00074 {
00075 //======================================================================
00076 // Purpose: Set format flags for a given message stream.
00077 // 
00078 // Inputs: 
00079 //   msgstream : The stream to affect. Format is <msgname>::<loglevel>
00080 //   fmtflags  : List of format flags to affect (space, comma, sepatated)
00081 //   raiseFlag : true=raise the format flags, false=lower the flags
00082 //======================================================================
00083   string stream;
00084   string level;
00085   MsgStream *ms;
00086   Msg::LogLevel_t lvl;
00087 
00088   JobCommand::SplitLine(msgstream, ':', stream, level);
00089   ms = MsgService::Instance()->GetStream(stream.c_str());
00090   
00091   lvl = 0;
00092   lvl = Msg::GetLevelCode(level.c_str());
00093 
00094   // Walk over the input string and strip out the individual format flags
00095   vector<string> fmtlist;
00096   JobCommand::StringTok(fmtlist,fmtflags," +,");
00097   vector<string>::iterator itr(fmtlist.begin());
00098   vector<string>::iterator itrEnd(fmtlist.end());
00099   for (; itr!=itrEnd; ++itr) {
00100     int format = Msg::GetFormatCode( (*itr).c_str() );   
00101     if (raiseFlag) { ms->AddFormat(lvl,format);    }
00102     else           { ms->RemoveFormat(lvl,format); }
00103   }
00104 }
00105 
00106 //......................................................................
00107 
00108 void JobCMsgModule::SetDefaultFormat(const char* fmtflags,
00109                                      const char* level )
00110 {
00111 //======================================================================
00112 // Purpose: Sets the default format flags for all newly created streams
00113 // 
00114 // Inputs: 
00115 //   fmtflags  : List of format flags to affect (space, comma, sepatated)
00116 //   level     : What format level. Blank = all levels
00117 //======================================================================
00118   Int_t lvl;
00119 
00120   lvl = Msg::GetLevelCode(level);
00121   if(lvl==-1) lvl=99;
00122 
00123   // Walk over the input string and strip out the individual format flags
00124   int format = 0;
00125   vector<string> fmtlist;
00126   JobCommand::StringTok(fmtlist,fmtflags," +,");
00127   vector<string>::iterator itr(fmtlist.begin());
00128   vector<string>::iterator itrEnd(fmtlist.end());
00129   for (; itr!=itrEnd; ++itr) {
00130     format |= Msg::GetFormatCode( (*itr).c_str() );   
00131   }
00132 
00133   MsgService::Instance()->SetDefaultFormat(format,lvl);
00134 
00135 }
00136 
00137 //......................................................................
00138 
00139 void JobCMsgModule::HandleCommand(JobCommand *cmd) 
00140 {
00141 //======================================================================
00142 // Purpose: Handle a command related to the message service
00143 // configuration
00144 //
00145 // Inputs: cmd - a job command
00146 //======================================================================
00147   string c = cmd->PopCmd();
00148 
00149   if (c == "SetLevel")   {
00150     const char *stream = cmd->PopOpt();
00151     const char *level  = cmd->PopOpt();
00152     this->SetLevel(stream, level);
00153     return;
00154   }
00155   if (c == "Attach") {
00156     const char *stream = cmd->PopOpt();
00157     const char *os     = cmd->PopOpt();
00158     this->Attach(stream, os);
00159     return;
00160   }
00161   if (c == "Format") {
00162     std::string stream = cmd->PopOpt();
00163     std::string flags  = cmd->PopOpt();
00164     std::string add    = cmd->PopOpt();
00165     if (add == "Remove") {
00166       this->Format(stream.c_str(), flags.c_str(), false);
00167     }
00168     else {
00169       this->Format(stream.c_str(), flags.c_str(), true);
00170     }
00171     return;
00172   }
00173   if (c == "Concat") {
00174     this->Concat();
00175     return;
00176   }
00177   if (c == "Report") {
00178     this->Report();
00179     return;
00180   }
00181   if (c == "Stats") {
00182     this->Stats();
00183     return;
00184   }
00185   else {
00186     MSG("JobC",Msg::kWarning) << 
00187       "Module " << this->GetName() << 
00188       " does not implement command " << c << "." << endl;    
00189   }
00190 }
00191 
00192 //......................................................................
00193 
00194 void JobCMsgModule::Help() 
00195 {
00196 //======================================================================
00197 // Purpose: Print help for this module
00198 //======================================================================
00199 #define _NL_ <<"\n"<<
00200   const char *n = this->GetName();
00201   MSG("JobC",Msg::kInfo) <<
00202     "Help for '" << this->GetName() << "' module:\n" <<
00203     " " << this->GetName() << 
00204     " is a module for configuring the message service. It can be used" _NL_
00205     " to set log levels, direct output, set formats etc. for the" _NL_
00206     " various package streams. For example, to lower the print" _NL_
00207     " threshold for the stream 'Trk' use:" _NL_
00208     " /Msg/SetLevel Trk Debug" _NL_
00209     "" _NL_
00210     " Possible commands are:" _NL_
00211     "" _NL_
00212     " /" << n << "/SetLevel <stream> <level>" _NL_
00213     "" _NL_
00214     "  Set the log level for <stream> to <level> where <level> is" _NL_
00215     "  Verbose,Debug,Info,Error,Warning,Fatal" _NL_
00216     "" _NL_
00217     " /" << n << "/Attach <stream>::<loglevel> <ostream>" _NL_
00218     "" _NL_
00219     "  Attach the output stream <ostream> to the list of output streams" _NL_
00220     "  messages sent to <stream> of type <loglevel> get send to." _NL_
00221     "  <ostream> can be cout, cerr, clog, or file name. <loglevel> can" _NL_
00222     "  be Verbose, Debug, Info, Warning, Error, Fatal, or All." _NL_
00223     "" _NL_
00224     " /" << n << "/Format <stream>::<loglevel> <Add/Remove> <format>" _NL_
00225     "" _NL_
00226     "  Add/Remove additional tags to messages. Possibilities are:" _NL_
00227     "  Priority, Time, File, CVSId, Line, Host, and PID."  << 
00228     "" _NL_ endl;
00229 }
00230 
00231 //......................................................................
00232 
00233 void JobCMsgModule::Stats()
00234 {  
00235   MsgService::Instance()->PrintStatistics();
00236 }
00237 
00238 //......................................................................
00239 
00240 void JobCMsgModule::Report() 
00241 {  
00242   cerr << (*MsgService::Instance());
00243 }
00244 
00245 //......................................................................
00246 
00247 void JobCMsgModule::Reset() 
00248 { 
00249   sorry(); 
00250 }
00251 
00252 //......................................................................
00253 
00254 void JobCMsgModule::SetLevel(const char* stream, const char* level)
00255 {
00256 //======================================================================
00257 // Purpose: Set the print threshold for a given stream. Expects options
00258 // in the following order: <stream_name> <level>
00259 //
00260 // Inputs: cmd - job command
00261 //======================================================================
00262   MsgStream *s = MsgService::Instance()->GetStream(stream);
00263   if (s==0) {
00264     MSG("JobC", Msg::kWarning) <<
00265       "Can not find message stream " << stream << "." << endl;
00266     return;
00267   }
00268   
00269   Msg::LogLevel_t lvl = Msg::GetLevelCode(level);
00270   if (lvl>=0) {
00271     MSG("JobC", Msg::kDebug) << 
00272       "Setting level '" << level << "' for stream " << stream << "\n";
00273     s->SetLogLevel(lvl);
00274   }
00275   else {
00276     MSG("JobC", Msg::kInfo) << 
00277       "'" << level << "' is not valid log level. " <<
00278       "Using default log level 'Info'" << endl;
00279     s->SetLogLevel(Msg::kInfo);
00280   }
00281 }
00282 

Generated on Mon Feb 15 11:06:49 2010 for loon by  doxygen 1.3.9.1