artdaq_mfextensions  v1_03_05
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  // Set module name
21  mf::SetApplicationName("anotherLogger");
22 
23  mf::LogWarning("warn1 | warn2") << "Followed by a WARNING message.";
24  mf::LogDebug("debug") << "The debug message in the other thread";
25 
26  return;
27 }
28 
29 int main() {
30  try {
31  // Start MessageFacility Service
32  std::ostringstream ss;
33  std::ifstream logfhicl("MessageFacility.cfg");
34  if (logfhicl.is_open()) {
35  std::stringstream fhiclstream;
36  fhiclstream << logfhicl.rdbuf();
37  ss << fhiclstream.str();
38  }
39  fhicl::ParameterSet pset;
40  std::string pstr(ss.str());
41  fhicl::make_ParameterSet(pstr, pset);
42  mf::StartMessageFacility(pset);
43  } catch (std::exception& e) {
44  std::cerr << "Catched\n" << 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  if (i % 1000 == 0) {
67  sprintf(buf, "mftest-%d", i);
68  mf::SetApplicationName(buf);
69  }
70 
71  mf::LogError("catError") << "Error information. " << i;
72  mf::LogWarning("catWarning") << "Warning information. " << i;
73  mf::LogInfo("catInfo") << "Info information. " << i;
74 #if MESSAGEFACILITY_HEX_VERSION < 0x20300
75  LOG_DEBUG("debug") << "DEBUG information. " << i;
76 #else
77  MF_LOG_DEBUG("debug") << "DEBUG information. " << i;
78 #endif
79 
80  // sleep(1);
81  usleep(400000);
82  }
83 
84  // Thread join
85  // loggerThread.join();
86 
87  mf::LogStatistics();
88 
89  // sleep(2);
90 
91  return 0;
92 }