Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:44

0001 /*----------------------------------------------------------------------
0002 
0003    This is a generic main that can be used with any plugin and a 
0004    PSet script. It shows the minimum machinery necessary for a
0005    "standalone" program to issue MessageLogger messages.
0006    N. B. In this context, standalone means a job where the user
0007    has provided the main program instead of supplying a module
0008    for cmsRun to call on.
0009 
0010 ----------------------------------------------------------------------*/
0011 
0012 #include <cmath>
0013 #include <exception>
0014 #include <iostream>
0015 #include <iomanip>
0016 #include <fstream>
0017 #include <string>
0018 #include <vector>
0019 #include "FWCore/Utilities/interface/Exception.h"
0020 #include "FWCore/PluginManager/interface/ProblemTracker.h"
0021 #include "FWCore/Utilities/interface/Presence.h"
0022 #include "FWCore/ParameterSetReader/interface/ParameterSetReader.h"
0023 #include "FWCore/PluginManager/interface/PresenceFactory.h"
0024 #include "FWCore/ServiceRegistry/interface/Service.h"
0025 
0026 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0027 
0028 // -----------------------------------------------
0029 
0030 std::string indirectWarn(int /*num*/) {
0031   //  std::cout << "  Returning the string Emit Warning level message " << num << std::endl;
0032   return std::string("\t\tEmit Warning level message ");
0033 }
0034 
0035 std::string indirectInfo(int /*num*/) {
0036   //  std::cout << "  Returning the string Emit Info level message " << num << std::endl;
0037   return std::string("\t\tEmit Info level message ");
0038 }
0039 
0040 void DoMyStuff() {
0041   // Issue several types of logger messages.  This function could
0042   // be substantially more complex. This example is about as simple
0043   // as can be.
0044 
0045   double d = M_PI;
0046   edm::LogWarning("cat_A") << "Test of std::setprecision(p):"
0047                            << " Pi with precision 12 is " << std::setprecision(12) << d;
0048 
0049   for (int i = 0; i < 25; ++i) {
0050     //  edm::LogInfo("cat_B")    << "\t\tEmit Info level message " << i+1;
0051     edm::LogInfo("cat_B") << indirectInfo(i + 1) << i + 1;
0052     //  edm::LogWarning("cat_C") << "\t\tEmit Warning level message " << i+1;
0053     edm::LogWarning("cat_C") << indirectWarn(i + 1) << i + 1;
0054   }
0055 }
0056 
0057 int main(int, char* argv[]) {
0058   std::string const kProgramName = argv[0];
0059 
0060   int rc = 0;
0061   try {
0062     // A.  Instantiate a plug-in manager first.
0063     edm::AssertHandler ah;
0064 
0065     // B.  Load the message service plug-in.  Forget this and bad things happen!
0066     //     In particular, the job hangs as soon as the output buffer fills up.
0067     //     That's because, without the message service, there is no mechanism for
0068     //     emptying the buffers.
0069     std::shared_ptr<edm::Presence> theMessageServicePresence;
0070     theMessageServicePresence =
0071         std::shared_ptr<edm::Presence>(edm::PresenceFactory::get()->makePresence("SingleThreadMSPresence").release());
0072 
0073     // C.  Manufacture a configuration and establish it.
0074     std::string config =
0075         "process x = {"
0076         "service = MessageLogger {"
0077         "untracked vstring destinations = {'infos.mlog','warnings.mlog'}"
0078         "untracked PSet infos = {"
0079         "untracked string threshold = 'INFO'"
0080         "untracked PSet default = {untracked int32 limit = 1000000}"
0081         "}"
0082         "untracked PSet warnings = {"
0083         "untracked string threshold = 'WARNING'"
0084         "untracked PSet default = {untracked int32 limit = 1000000}"
0085         "}"
0086         "untracked vstring categories = {}"
0087         "}"
0088         "service = JobReportService{}"
0089         "service = SiteLocalConfigService{}"
0090         "}";
0091 
0092     // D.  Create the services.
0093     std::unique_ptr<edm::ParameterSet> params;
0094     edm::makeParameterSets(config, params);
0095     edm::ServiceToken tempToken(edm::ServiceRegistry::createServicesFromConfig(std::move(params)));
0096 
0097     // E.  Make the services available.
0098     edm::ServiceRegistry::Operate operate(tempToken);
0099 
0100     //  Generate a bunch of messages.
0101     DoMyStuff();
0102   }
0103 
0104   //  Deal with any exceptions that may have been thrown.
0105   catch (cms::Exception& e) {
0106     std::cout << "cms::Exception caught in " << kProgramName << "\n" << e.explainSelf();
0107     rc = 1;
0108   } catch (std::exception& e) {
0109     std::cout << "Standard library exception caught in " << kProgramName << "\n" << e.what();
0110     rc = 1;
0111   } catch (...) {
0112     std::cout << "Unknown exception caught in " << kProgramName;
0113     rc = 2;
0114   }
0115 
0116   return rc;
0117 }