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