Line data Source code
1 : /**
2 : * @file logger.hpp logger interface definition
3 : *
4 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
5 : * Licensing/copyright details are in the COPYING file that you should have
6 : * received with this code.
7 : *
8 : cd build/logging
9 : make clean basic_functionality_example CXX_DEFINES=-DTRY_COMPILE=0
10 :
11 : Run:
12 : install/logging/bin/basic_functionality_example
13 : or
14 : DUNEDAQ_ERS_VERBOSITY_LEVEL=2 install/logging/bin/basic_functionality_example
15 : or
16 : TRACE_LVLS=-1 install/logging/bin/basic_functionality_example
17 : */
18 :
19 : #include <string>
20 : #ifndef DUNEDAQ_PACKAGE_NAME // this could/may be (tbd) set by the build system
21 : # define DUNEDAQ_PACKAGE_NAME "Logging_" // becomes an ERS Qualifier
22 : #endif
23 : #include <logging/Logging.hpp>
24 : #include <ers/Issue.hpp>
25 :
26 : /** \def ers::File2 This is the base class for all file related issues.
27 : */
28 0 : ERS_DECLARE_ISSUE(ers, // namespace
29 : File2, // issue class name
30 : ERS_EMPTY, // no message
31 : ((const char *)file_name ) // single attribute
32 : )
33 : /** \def ers::CantOpenFile2 This issue is reported when a certain file can
34 : * not be opened by any reason.
35 : */
36 0 : ERS_DECLARE_ISSUE_BASE(ers, // namespace
37 : CantOpenFile2, // issue class name
38 : ers::File2, // base class name
39 : "Can not open \"" << file_name << "\" file arg2=" << arg2 << " arg3=" << arg3, // message
40 : ((const char *)file_name ), // base class attributes
41 : ((int) arg2) ((const char*) arg3) // two attributes in this class
42 : )
43 :
44 0 : ERS_DECLARE_ISSUE(appframework, // namespace
45 : CommandNotRegistered2, // issue class name
46 : "Command '" << command_name
47 : << "' does not have an entry in the CommandOrderMap!"
48 : << " UserModules will receive this command in an unspecified order!",
49 : ((const char *)command_name ) // single attribute
50 : )
51 :
52 0 : ERS_DECLARE_ISSUE(appframework, // namespace
53 : MyExit, // issue class name
54 : "exiting",
55 : ERS_EMPTY // no attributes in this class
56 : )
57 :
58 0 : void ex_thread( volatile const int *spinlock, int thread_idx )
59 : {
60 0 : while(*spinlock); // The main program thread will clear this
61 : // once all thread are created and given a
62 : // chance to get here.
63 0 : for (auto uu=0; uu<5; ++uu)
64 0 : TLOG_DEBUG(8) << "tidx " << thread_idx << " fast LOG_DEBUG(8) #" <<uu;
65 0 : }
66 :
67 :
68 0 : int main(/*int argc, char *argv[]*/)
69 : {
70 : // FOR THIS EXAMPLE ONLY -- NOT NORMALLY NEEDED!! --v--v--v--v--v--v
71 : // activate TRACE memory buffer for debugging
72 0 : std::string tfile="/tmp/trace_buffer_"+std::string(getenv("USER"))+"_basic";
73 0 : system( ("rm -f "+tfile).c_str() );
74 0 : setenv("TRACE_FILE",tfile.c_str(),0);
75 0 : setenv("TRACE_LVLM","-1",0);
76 0 : setenv("TRACE_LVLS","0xff",0);
77 0 : TRACE_CNTL("reset");
78 : // --^--^--^--^--^--^--^--^--^--^--^--^--^--^--^--^--^--^--^--^--^--^--
79 :
80 0 : setenv("DUNEDAQ_APPLICATION_NAME","LOGGING_BASIC_FUN_APP",0);
81 0 : dunedaq::logging::Logging::setup("test", "basic_functionality_example");
82 :
83 0 : TLOG_DEBUG( 0 ) << "a message which doesn't go to the central logger";
84 0 : TLOG() << "another example of a message that doesn't go to the central logger";
85 0 : TLOG("MYNAME") << "yet another example -- messages are controllable via name+level";
86 :
87 : // ers::Message message(ERS_HERE,"Using TRACE_FILE="+tfile);
88 : // message.add_qualifier( "Logging_qual2" );
89 : // TLOG() << message;
90 :
91 : //----------------------------------------
92 :
93 0 : ers::fatal( ers::CantOpenFile2(ERS_HERE,"My_Fatal_FileName - usually associated with throw or exit",4,"four") );
94 : //ers::error( ers::Message( ERS_HERE, "this is ers::error( ers::Message( ERS_HERE, \"this is ...\" ) )" ) );
95 0 : ers::error( ers::CantOpenFile2(ERS_HERE,"My_Error_FileName",5,"five") );
96 : # if TRY_COMPILE & 0x1
97 : // see what happens with: make clean install CXX_DEFINES=-DTRY_COMPILE=1
98 : ers::error( "error with just a string" );
99 : # endif
100 : // ers::warning( ers::CantOpenFile2(ERS_HERE,"My_Warn_FileName",6,"six") );
101 : // ers::warning( ers::Message(ERS_HERE,"My_Warn_Message with ignored macro param") );
102 : // ers::info( ers::Message(ERS_HERE,"a specific TraceStreamer method with ers::Message isn't defined.") );
103 :
104 : //----------------------------------------
105 :
106 0 : TLOG("TEST1") << appframework::CommandNotRegistered2(ERS_HERE,"MyCommand");
107 0 : TLOG() << "LOG_LOG() stating LOG_DEBUG(n)'s follow -- they must be enabled via trace_cntl or DUNEDAQ_ERS_DEBUG_LEVEL";
108 :
109 : // TLOG_DEBUG(6)<< ers::Message(ERS_HERE,"A LOG_DEBUG(6) using ers::Message - The Logging has just been setup. ERS bug - 1st DEBUG is always DEBUG_0");
110 0 : TLOG_DEBUG(6)<< ers::CantOpenFile2(ERS_HERE,"My_Fatal_FileName_via_LOG_DEBUG_6",7,"seven");
111 0 : TLOG_DEBUG(0) << "hello - debug level 0";
112 0 : TLOG_DEBUG(5) << "hello - debug level 5";
113 0 : TLOG_DEBUG(6) << "hello - debug level 6";
114 0 : TLOG_DEBUG(7) << ers::CantOpenFile2(ERS_HERE,"My_d07_FileName",8,"eight");
115 :
116 0 : TLOG_DEBUG(8,"TEST2") << "testing name argument";
117 0 : TLOG_DEBUG(55) << "debug lvl 55";
118 0 : TLOG_DEBUG(56) << "debug lvl 56";
119 0 : TLOG_DEBUG(63) << "debug lvl 63";
120 0 : TLOG_DEBUG(64) << "debug lvl 64";
121 :
122 0 : TLOG() << "\ntshow follows:\n\n";
123 0 : system( "TRACE_TIME_FMT='%Y-%b-%d %H:%M:%S,%%03d' TRACE_SHOW='%H%x%N %T %e %l %8L %m' trace_cntl show | trace_delta -ct 1 -d 1" );
124 :
125 0 : TLOG() << "\nOne could try the same with DUNEDAQ_ERS_VERBOSITY_LEVEL=2 or 3\n";
126 :
127 0 : TLOG() << "\nNow, fast multithread...\n";
128 0 : const int kNumThreads=5;
129 0 : std::thread threads[kNumThreads];
130 0 : int spinlock=1;
131 0 : for (int uu=0; uu<kNumThreads; ++uu)
132 0 : threads[uu] = std::thread(ex_thread,&spinlock,uu);
133 :
134 0 : usleep(20000);
135 0 : spinlock = 0;
136 :
137 0 : for (unsigned uu=0; uu<kNumThreads; ++uu)
138 0 : threads[uu].join();
139 :
140 0 : TLOG() << "\ntshow follows:\n\n";
141 0 : system( "TRACE_SHOW='%H%x%N %T %P %i %C %e %3L %R %m' trace_cntl show -c 25 | trace_delta -ct 1 -d 1" );
142 :
143 0 : throw( appframework::MyExit(ERS_HERE) );
144 : return (0);
145 0 : } // main
|