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

TestDemo.cxx

Go to the documentation of this file.
00001 
00002 // $Id: TestDemo.cxx,v 1.3 2006/10/28 05:07:14 gmieg Exp $
00003 //
00004 // A simple program to demonstrate how clients use the message service
00005 //
00006 // messier@huhepl.harvard.edu
00008 #include "MessageService/MsgService.h"
00009 
00010 //
00011 // If you plan to use the message service you need a line that looks
00012 // like the following at the top of the file. Note that cvs will fill
00013 // in everything between the ":" and the final "$" automatically so
00014 // you can leave that part blank.
00015 //
00016 CVSID("$Id: TestDemo.cxx,v 1.3 2006/10/28 05:07:14 gmieg Exp $");
00017 
00018 //......................................................................
00019 
00020 static void theBasics()
00021 {
00022 //======================================================================
00023 // Send some messages to the stream -- this is basically all a
00024 // typical program should ever have to use. In the following the
00025 // "Bsx" would get replaced with the package prefix of the package
00026 // that was sending the message
00027 //======================================================================
00028   try {
00029     MSG("Bsx", Msg::kVerbose) << "This is a  VERBOSE message\n";
00030     MSG("Bsx", Msg::kDebug)   << "This is a  DEBUG   message\n";
00031     MSG("Bsx", Msg::kInfo)    << "This is an INFO    message\n";
00032     MSG("Bsx", Msg::kWarning) << "This is a  WARNING message\n";
00033     MSG("Bsx", Msg::kError)   << "This is an ERROR   message\n";
00034     MSG("Bsx", Msg::kFatal)   << "This is a  FATAL   message\n";
00035   }
00036   catch(MSGException) {
00037     cerr << endl << endl;
00038     cerr << "***************** MSGException *****************" << endl;
00039     cerr << "******** MSGException caught (TestDemo) ********" << endl;
00040     cerr << "***************** MSGException *****************" << endl;
00041     cerr << endl;
00042     MSG("Bsx", Msg::kError)   << "This is a  FATAL   message\n";
00043   }
00044 }
00045 
00046 //......................................................................
00047 
00048 static void fancyStuff()
00049 //======================================================================
00050 // Show how to configure the message service and message streams
00051 //======================================================================
00052 {
00053   // Get pointers to the message service and to the stream I want ("Str")
00054   // If you just want to use the defaults these line are not needed
00055   MsgService* msvc = MsgService::Instance();
00056   MsgStream*  mstr = msvc->GetStream("Fan");
00057 
00058   // Set the log level for the stream. If you just want the default
00059   // (kInfo) this line is not needed
00060   mstr->SetLogLevel(Msg::kDebug);
00061 
00062   // Set where the output should go. These are the defaults so
00063   // techically this code fragment is not needed, however, "cerr" and
00064   // "cout" could be replaced with filenames if the user wants the
00065   // output in a text file
00066   mstr->AttachOStream(Msg::kVerbose, "cerr");
00067   mstr->AttachOStream(Msg::kInfo,    "cout");
00068 
00069   // If you don't like the default information that gets printed with
00070   // the messages you can change them like this:
00071 
00072   // Set the format flags for verbose messages
00073   mstr->SetFormat(Msg::kVerbose, Msg::kPriority + Msg::kName);
00074 
00075   // Add a format flag for warning messages
00076   mstr->AddFormat(Msg::kWarning, Msg::kTime);
00077 
00078   // Lower a format flag for warning messages
00079   mstr->RemoveFormat(Msg::kWarning, Msg::kName);
00080 }
00081 
00082 //......................................................................
00083 
00084 static void fancyMessages()
00085 {
00086 //======================================================================
00087 // Print some more messages just to make the example more interesting
00088 // Use the strem configured in "fancyStuff" above
00089 //======================================================================
00090 
00091   try {
00092     MSG("Fan", Msg::kVerbose) << "This is a  VERBOSE message\n";
00093     MSG("Fan", Msg::kDebug)   << "This is a  DEBUG   message\n";
00094     MSG("Fan", Msg::kInfo)    << "This is an INFO    message\n";
00095     MSG("Fan", Msg::kWarning) << "This is a  WARNING message\n";
00096     MSG("Fan", Msg::kError)   << "This is an ERROR   message\n";
00097     MSG("Fan", Msg::kFatal)   << "This is a  FATAL   message\n";
00098   }
00099   catch(MSGException) {
00100     cerr << endl << endl;
00101     cerr << "***************** MSGException *****************" << endl;
00102     cerr << "******** MSGException caught (TestDemo) ********" << endl;
00103     cerr << "***************** MSGException *****************" << endl;
00104     cerr << endl;
00105     MSG("Fan", Msg::kError)   << "This is a  FATAL   message\n";
00106   }
00107 }
00108 
00109 //......................................................................
00110 
00111 static void manyMessageExample()
00112 {
00113 //======================================================================
00114 // Give an example where several messages all go to the same stream
00115 //======================================================================
00116   // If you have lots of messages going to the same stream you can do this:
00117   MsgStream *m1 = &MSGSTREAM("Mny", Msg::kError);
00118   (*m1) << " ===== *\n";
00119   (*m1) << "* Messages like this can save a little time and typing.  *\n";
00120   (*m1) << "* However, notice that only one header gets printed!     *\n";
00121   (*m1) << "* That's why I don't recommend using this for errors and *\n";
00122   (*m1) << "* warnings.                                              *\n";
00123   (*m1) << "* ====================================================== *\n";
00124 }
00125 
00126 //......................................................................
00127 
00128 static void printStatistics() 
00129 {
00130 //======================================================================
00131 // Show how to print message service usage statistics
00132 //======================================================================
00133   // Print message service usage statistics
00134   MsgService::Instance()->PrintStatistics();
00135 }
00136 
00137 //......................................................................
00138 
00139 int main(void) { 
00140 
00141   theBasics();
00142   fancyStuff();
00143   fancyMessages();
00144   manyMessageExample();
00145   printStatistics();
00146   
00147   return EXIT_SUCCESS;
00148 }

Generated on Mon Nov 23 05:28:34 2009 for loon by  doxygen 1.3.9.1