artdaq_mfextensions  v1_02_02
MFTest.cc
1 //#define NDEBUG
2 #define ML_DEBUG // always enable debug
3 
4 #if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
5 # include "fhiclcpp/ParameterSet.h"
6 # include "fhiclcpp/make_ParameterSet.h"
7 # include <fstream>
8 # include <sstream>
9 #endif
10 #include <iostream>
11 #include <stdlib.h>
12 #include <cstdio>
13 
14 #include "messagefacility/MessageLogger/MessageLogger.h"
15 
16 void anotherLogger()
17 {
18  // Set module name
19 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
20  mf::SetApplicationName("anotherLogger");
21 # else
22  mf::SetModuleName("anotherLogger");
23 # endif
24 
25  mf::LogWarning("warn1 | warn2") << "Followed by a WARNING message.";
26  mf::LogDebug("debug") << "The debug message in the other thread";
27 
28  return;
29 }
30 
31 int main()
32 {
33  try
34  {
35  // Start MessageFacility Service
36 # if MESSAGEFACILITY_HEX_VERSION >= 0x20002 // an indication of a switch from s48 to s50
37  std::ostringstream ss;
38  std::ifstream logfhicl("MessageFacility.cfg");
39  if (logfhicl.is_open()) {
40  std::stringstream fhiclstream;
41  fhiclstream << logfhicl.rdbuf();
42  ss << fhiclstream.str();
43  }
44  fhicl::ParameterSet pset;
45  std::string pstr(ss.str());
46  fhicl::make_ParameterSet(pstr, pset);
47  mf::StartMessageFacility( pset );
48 # else
49  mf::StartMessageFacility(
50  mf::MessageFacilityService::MultiThread,
51  mf::MessageFacilityService::ConfigurationFile(
52  "MessageFacility.cfg",
53  mf::MessageFacilityService::logCF("mylog")));
54 # endif
55  }
56  catch (std::exception& e)
57  {
58  std::cerr << "Catched\n" << e.what();
59  exit(-1);
60  }
61 
62  // Set module name for the main thread
63  mf::SetApplicationName("mftest");
64 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
65  mf::SetModuleName("MFTest");
66  mf::SetContext("pre-event");
67 # endif
68 
69  // Start up another logger in a seperate thread
70  //boost::thread loggerThread(anotherLogger);
71 
72  // Issue messages with different severity levels
73  //mf::LogError("err1|err2") << "This is an ERROR message.";
74  //mf::LogWarning("warning") << "Followed by a WARNING message.";
75 
76  // Switch context
77 # if MESSAGEFACILITY_HEX_VERSION < 0x20002 // v2_00_02 is s50, pre v2_00_02 is s48
78  mf::SetContext("pro-event");
79 # endif
80 
81  //mf::SwitchChannel(2);
82 
83  char buf[100];
84 
85  // Log Debugs
86  for (int i = 0; i < 2; ++i)
87  {
88  if (i % 1000 == 0)
89  {
90  sprintf(buf, "mftest-%d", i);
91  mf::SetApplicationName(buf);
92  }
93 
94  mf::LogError("catError") << "Error information. " << i;
95  mf::LogWarning("catWarning") << "Warning information. " << i;
96  mf::LogInfo("catInfo") << "Info information. " << i;
97  LOG_DEBUG("debug") << "DEBUG information. " << i;
98 
99  //sleep(1);
100  usleep(400000);
101  }
102 
103  // Thread join
104  //loggerThread.join();
105 
106  mf::LogStatistics();
107 
108  //sleep(2);
109 
110  return 0;
111 }