ApplicationStartupDebugTools.cpp

00001 
00002 #include "ApplicationStartupDebugTools.h"
00003 #include "SctNames.h"
00004 #include "URandom.h"
00005 #include <iostream>
00006 #include <unistd.h>
00007 #include <mrs/message.h>
00008 
00009 namespace Sct {
00010 
00011   std::string ApplicationStartupDebugTools::programArgs(const int argc,
00012                                                         const char * const * const argv) {
00013     std::ostringstream os;
00014     if (argv) {
00015       bool first=true;
00016       for (int i=0; i<argc; ++i) {
00017         if (argv[i]) {
00018           if (first) {
00019             first=false;
00020           } else {
00021             os << " ";
00022           };
00023           os << argv[i];
00024         };
00025       };
00026     };
00027     return os.str();
00028   };
00029 
00030   void ApplicationStartupDebugTools::announceStartOfMain(const int argc, // make me and the next arg 0 if you don't have access to us!
00031                                                          const char * const * const argv,
00032                                                          const std::string & file,
00033                                                          const unsigned int line,
00034                                                          const std::string & message,
00035                                                          unsigned int delaySeconds) {
00036     
00037     const bool announceToMrsIfAvailable = true;
00038     const bool announceToStdOut = true;
00039     const bool announceToSomewhere = announceToMrsIfAvailable || announceToStdOut;
00040 
00041     if (delaySeconds==0 && false) {
00042       delaySeconds = (URandom::urand<unsigned int>() & 7) + 1; // 1 to 8 seconds delay
00043     };
00044 
00045     const bool delayThings = (true && delaySeconds>0);
00046 
00047     const std::string progArgs = announceToSomewhere?programArgs(argc,argv):"";
00048     
00049     if (announceToSomewhere) {
00050       std::ostringstream os;
00051       os << "ApplicationStartupDebugTools::announceStartOfMain called by ["
00052          <<progArgs<<"] in File["<<file<<"] at Line["<<line
00053          <<"] with additional Message["<<message<<"].";
00054       if (delayThings) {
00055         os << " There will now be a delay of " << delaySeconds << " seconds.";
00056       };
00057       
00058       const std::string messageString = os.str();
00059 
00060       if (announceToMrsIfAvailable) {
00061         IPCPartition p(SctNames::getPartitionName());
00062         if(p.isValid()) {
00063           MRSStream mrs(p);
00064           mrs << MRS_INFORMATION << "AppStartupDbgTools" << MRS_TEXT(messageString) << ENDM;
00065         };
00066       };
00067 
00068       if (announceToStdOut) {
00069         std::cout << "AppStartupDbgTools: ["<< messageString << "]" << std::endl;
00070       };
00071 
00072     };
00073       
00074     if (delayThings) {
00075       sleep(delaySeconds);
00076     };
00077 
00078     if (announceToSomewhere && delayThings) {
00079       std::ostringstream os;
00080       os << "Endo of delay for announceStartOfMain called by ["<<progArgs<<"]";
00081       
00082       const std::string messageString = os.str();
00083 
00084       if (announceToMrsIfAvailable) {
00085         IPCPartition p(SctNames::getPartitionName());
00086         if(p.isValid()) {
00087           MRSStream mrs(p);
00088           mrs << MRS_INFORMATION << "AppStartupDbgTools" << MRS_TEXT(messageString) << ENDM;
00089         };
00090       };
00091       
00092       if (announceToStdOut) {
00093         std::cout << "AppStartupDbgTools: ["<< messageString << "]" << std::endl;
00094       };
00095     };
00096 
00097     
00098   };
00099   
00100 };

Generated on Mon Feb 6 14:01:16 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6