Line data Source code
1 : // This file (no_trace.cc) was created by Ron Rechenmacher <ron@fnal.gov> on
2 : // Aug 22, 2019. "TERMS AND CONDITIONS" governing this file are in the README
3 : // or COPYING file. If you do not have such a file, one can be obtained by
4 : // contacting Ron or Fermi Lab in Batavia IL, 60510, phone: 630-840-3000.
5 : // $RCSfile: .emacs.gnu,v $
6 : // rev="$Revision: 1784 $$Date: 2026-10-09 13:51:46 -0500 (Fri, 09 Oct 2026) $";
7 :
8 : // g++ -g -O0 -Wall -std=c++11 -o trace_disabled{,.cc} && ./trace_disabled
9 :
10 : #include <stdio.h> // snprintf
11 : #include <unistd.h> // write
12 : #include <sstream> // std::ostringstream
13 :
14 : #define NO_TRACE
15 : #ifndef NO_TRACE
16 :
17 : # include <iostream> // std::cout
18 : # define TLOG(...) \
19 : if (0) std::cout
20 : # define TRACE(...)
21 : # define TRACEN(...)
22 : # define TRACEN_(...)
23 : # define TRACE_CNTL(...) (0L)
24 :
25 : #else
26 :
27 : # define TRACE_DEBUG_LVL 2
28 : # define TRACE_MSG_MAX 0x200
29 : # define TARG1(a1, ...) a1
30 : # define TLOG(...) \
31 : for ( \
32 : struct _S_ { \
33 : int once; \
34 : ssize_t sts; \
35 : std::string ss; \
36 : std::ostringstream oss; \
37 : _S_() : once(0) {} \
38 : } _xx; \
39 : TARG1(__VA_ARGS__, need_at_least_one) <= TRACE_DEBUG_LVL && _xx.once == 0; \
40 : _xx.once= 1, _xx.ss= _xx.oss.str(), _xx.ss.size() && _xx.ss[_xx.ss.size() - 1] != '\n' && (_xx.ss.append("\n"), 1), \
41 : _xx.sts= write(1, &_xx.ss[0], _xx.ss.size())) \
42 : _xx.oss
43 : # define TRACE(lvl, ...) \
44 : do \
45 : if (lvl <= TRACE_DEBUG_LVL) { /**wrap in do...while(0) for certain 'if' syntax */ \
46 : char obuf[TRACE_MSG_MAX]; \
47 : ssize_t sts __attribute__((__unused__)); \
48 : int nn= snprintf(obuf, sizeof(obuf), __VA_ARGS__); \
49 : if (nn >= (int)sizeof(obuf)) { /**truncated(but still terminated) */ \
50 : obuf[sizeof(obuf) - 2]= '\n'; \
51 : nn= (int)sizeof(obuf) - 1; \
52 : } else if (nn > 0 && obuf[nn - 1] != '\n' && nn == (sizeof(obuf) - 1)) \
53 : obuf[nn - 1]= '\n'; \
54 : else if (nn > 0 && obuf[nn - 1] != '\n') \
55 : obuf[nn++]= '\n'; \
56 : sts= write(1, obuf, nn); \
57 : } \
58 : while (0)
59 : # define TRACEN(nam, lvl, ...) TRACE(lvl, __VA_ARGS__)
60 : # define TARGS(arg1, ...) __VA_ARGS__
61 : # define TRACEN_(nam, lvl, ...) \
62 : do \
63 : if (lvl <= TRACE_DEBUG_LVL) { \
64 : std::ostringstream ostr__; \
65 : ostr__ << TARG1(__VA_ARGS__, not_used); \
66 : TRACE(lvl, &ostr__.str()[0], TARGS(__VA_ARGS__, 0 /*potential xtra 0 OK*/)); \
67 : } \
68 : while (0)
69 : # define TRACE_CNTL(...) (0L)
70 :
71 : #endif
72 :
73 0 : int main(int argc, char *argv[])
74 : {
75 0 : for (
76 0 : struct _S_ {
77 : int once;
78 : ssize_t sts;
79 : std::string ss;
80 : std::ostringstream oss;
81 0 : _S_() : once(0) {}
82 0 : } _xx;
83 0 : 2 <= 2 && _xx.once == 0;
84 0 : _xx.once= 1, _xx.ss= _xx.oss.str(), _xx.ss.size() && _xx.ss[_xx.ss.size() - 1] != '\n' && (_xx.ss.append("\n"), 1),
85 0 : _xx.sts= write(1, &_xx.ss[0], _xx.ss.size()))
86 0 : _xx.oss << "hello from " << argv[0];
87 :
88 0 : if (2 <= 2) {
89 0 : char obuf[20];
90 0 : ssize_t sts __attribute__((__unused__));
91 0 : int nn= snprintf(obuf, sizeof(obuf), "hello argc=%d", argc);
92 0 : if (nn >= (int)sizeof(obuf)) { //truncated(but still terminated)
93 0 : obuf[sizeof(obuf) - 2]= '\n';
94 0 : nn= (int)sizeof(obuf) - 1;
95 0 : } else if (obuf[nn - 1] != '\n' && nn == (sizeof(obuf) - 1))
96 0 : obuf[nn - 1]= '\n';
97 0 : else if (obuf[nn - 1] != '\n')
98 0 : obuf[nn++]= '\n';
99 0 : sts= write(1, obuf, nn);
100 : }
101 :
102 0 : TLOG(2) << "hi " << 2 << " there\n";
103 : #ifdef TEST_EMPTY_MESSAGE
104 : TLOG(2);
105 : #endif
106 0 : TRACE(2, "hi %d there", 3);
107 0 : TRACEN_("notUsed", 2,
108 : "hi %d there "
109 : << "joe",
110 : 55);
111 0 : if (1) TRACE(2, "in if statement");
112 : else
113 0 : TRACE(2, "in if else");
114 0 : if (1) TRACEN_("notUsed", 2, "hi %d there, then", 44);
115 : else
116 0 : TRACE(2, "in if else");
117 0 : TLOG(3, "name") << "don't see this";
118 :
119 0 : return (0);
120 : } // main
|