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