Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-05-23 23:48:34

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