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