1 #include "sys.h"
2 #include <libcwd/debug.h>
3 #ifndef LIBCWD_USE_STRSTREAM
4 #include <sstream>
5 #else
6 #include <strstream>
7 #endif
8 #include <iostream>
9 
10 using namespace std;
11 
12 // CWDEBUG must be define in this test
13 
14 #ifndef CWDEBUG
15 #error "CWDEBUG not defined"
16 #endif
17 
18 // Creation of debug objects
19 
20 libcwd::debug_ct my_own_do;
21 namespace example { libcwd::debug_ct my_own_do; }
22 #define MyOwnDout(cntrl, data) LibcwDout(::libcwd::channels, my_own_do, cntrl, data)
23 #define ExampleDout(cntrl, data) LibcwDout(::libcwd::channels, example::my_own_do, cntrl, data)
24 
25 #ifdef THREADTEST
26 pthread_mutex_t dummy_mutex = PTHREAD_MUTEX_INITIALIZER;
27 #endif
28 
29 MAIN_FUNCTION
30 { PREFIX_CODE
31   Debug( check_configuration() );
32 
33 #if CWDEBUG_LOCATION
34   // Make sure we initialized the bfd stuff before we turn on WARNING.
35   Debug( (void)pc_mangled_function_name((void*)exit) );
36 #endif
37 
38 #ifndef LIBCWD_USE_STRSTREAM
39   ostringstream dummy;	// Do this before turning on debug output
40 #else
41   ostrstream dummy;
42 #endif
43 
44   // Test initial ostreams.
45   ostream* my_own_os = my_own_do.get_ostream();
46 #ifndef THREADTEST
47   ostream* libcwd_os = libcwd::libcw_do.get_ostream();
48 #endif
49   ostream* coutp = &cout;
50   ostream* cerrp = &cerr;
51 
52   if (my_own_os != cerrp
53 #ifndef THREADTEST
54      || libcwd_os != cerrp	// Already set to cout in threads_threads.cc
55 #endif
56      )
57     DoutFatal(dc::fatal, "Initial ostream not cerr");
58 
59   THREADED( Debug( my_own_do.set_ostream(cerrp, &cerr_mutex) ) );
60   THREADED( Debug( example::my_own_do.set_ostream(cerrp, &cerr_mutex) ) );
61 
62   Debug( libcw_do.on() );
63 #if CWDEBUG_DEBUG
64   // Get rid of the `first_time'.
65   Debug( my_own_do.on() );
66   Debug( my_own_do.off() );
67   Debug( example::my_own_do.on() );
68   Debug( example::my_own_do.off() );
69 #endif
70   Debug( dc::notice.on() );
71   Debug( dc::debug.on() );
72 
73   int a, b, c;
74   a = b = c = 0;
75 
76   // 1.1.2.1 Turning debug output on and off
77 
78   Dout(dc::notice, "Dout Turned on " << ++a);
79   MyOwnDout(dc::notice, "MyOwnDout Turned off " << ++b);
80   ExampleDout(dc::notice, "ExampleDout Turned off " << ++c);
81 
82   Debug( my_own_do.on() );
83 
84   Dout(dc::debug, "Dout Turned on " << ++a);
85   MyOwnDout(dc::debug, "MyOwnDout Turned on " << ++b);
86   ExampleDout(dc::debug, "ExampleDout Turned off " << ++c);
87 
88   Debug( example::my_own_do.on() );
89   Debug( libcw_do.off() );
90 
91   Dout(dc::notice, "Dout Turned off " << ++a);
92   MyOwnDout(dc::notice, "MyOwnDout Turned on " << ++b);
93   ExampleDout(dc::notice, "ExampleDout Turned on " << ++c);
94 
95   for (int i = 0; i < 5; ++i)
96   {
97     Debug( my_own_do.off() );
98     MyOwnDout(dc::debug, "MyOwnDout " << ++b);
99     ExampleDout(dc::debug, "ExampleDout " << ++c);
100   }
101 
102   for (int i = 0; i < 5; ++i)
103   {
104     Debug( my_own_do.on() );
105     MyOwnDout(dc::notice, "MyOwnDout " << ++b);
106     ExampleDout(dc::notice, "ExampleDout " << ++c);
107   }
108 
109   Debug( libcw_do.on() );
110   Dout(dc::debug, a << b << c);
111 
112   // 1.1.2.2 Setting the prefix formatting attributes
113 
114   Debug( libcw_do.margin().assign("***********libcw_do*", 20) );
115   Debug( my_own_do.margin().assign("**********my_own_do*", 20) );
116   Debug( example::my_own_do.margin().assign("*example::my_own_do*", 20) );
117 
118   Debug( libcw_do.marker().assign("|marker1|", 9) );
119   Debug( my_own_do.marker().assign("|marker2|", 9) );
120   Debug( example::my_own_do.marker().assign("|marker3|", 9) );
121 
122   Dout(dc::debug, "No indent");
123   Dout(dc::notice, "No indent");
124   Dout(dc::warning, "No indent");
125 
126   Debug( libcw_do.set_indent(8) );
127   Debug( my_own_do.set_indent(11) );
128   Debug( example::my_own_do.set_indent(13) );
129 
130   // 1.1.2.3 Retrieving the prefix formatting attributes
131 
132   Dout(dc::warning, "Dout text " << libcwd::libcw_do.get_indent() << ", \"" << libcwd::libcw_do.margin().c_str() << "\", \"" << libcwd::libcw_do.marker().c_str() << "\".");
133   MyOwnDout(dc::warning, "MyOwnDout text " << my_own_do.get_indent() << ", \"" << my_own_do.margin().c_str() << "\", \"" << my_own_do.marker().c_str() << "\".");
134   ExampleDout(dc::warning, "ExampleDout text " << example::my_own_do.get_indent() << ", \"" << example::my_own_do.margin().c_str() << "\", \"" << example::my_own_do.marker().c_str() << "\".");
135 
136   // Manipulating the format strings.
137 
138   Debug( libcw_do.push_margin() );
139   Debug( my_own_do.push_margin() );
140   Debug( example::my_own_do.push_margin() );
141 
142   Debug( libcw_do.margin().append("1Alibcw_do1", 11) );
143   Debug( my_own_do.margin().append("1Amy_own_do1", 12) );
144   Debug( example::my_own_do.margin().append("1Aexample::my_own_do1", 21) );
145 
146   Dout(dc::warning, "Dout text " << libcwd::libcw_do.get_indent() << ", \"" << libcwd::libcw_do.margin().c_str() << "\", \"" << libcwd::libcw_do.marker().c_str() << "\".");
147   MyOwnDout(dc::warning, "MyOwnDout text " << my_own_do.get_indent() << ", \"" << my_own_do.margin().c_str() << "\", \"" << my_own_do.marker().c_str() << "\".");
148   ExampleDout(dc::warning, "ExampleDout text " << example::my_own_do.get_indent() << ", \"" << example::my_own_do.margin().c_str() << "\", \"" << example::my_own_do.marker().c_str() << "\".");
149 
150   Debug( libcw_do.margin().prepend("1Plibcw_do1", 11) );
151   Debug( my_own_do.margin().prepend("1Pmy_own_do1", 12) );
152   Debug( example::my_own_do.margin().prepend("1Pexample::my_own_do1", 21) );
153 
154   Debug( libcw_do.push_margin() );
155   Debug( my_own_do.push_margin() );
156   Debug( example::my_own_do.push_margin() );
157 
158   Dout(dc::warning, "Dout text " << libcwd::libcw_do.get_indent() << ", \"" << libcwd::libcw_do.margin().c_str() << "\", \"" << libcwd::libcw_do.marker().c_str() << "\".");
159   MyOwnDout(dc::warning, "MyOwnDout text " << my_own_do.get_indent() << ", \"" << my_own_do.margin().c_str() << "\", \"" << my_own_do.marker().c_str() << "\".");
160   ExampleDout(dc::warning, "ExampleDout text " << example::my_own_do.get_indent() << ", \"" << example::my_own_do.margin().c_str() << "\", \"" << example::my_own_do.marker().c_str() << "\".");
161 
162   Debug( libcw_do.margin().assign("", 0) );
163   Debug( my_own_do.margin().assign("*", 1) );
164   Debug( example::my_own_do.margin().assign("XYZ", 3) );
165 
166   Dout(dc::warning, "Dout text " << libcwd::libcw_do.get_indent() << ", \"" << libcwd::libcw_do.margin().c_str() << "\", \"" << libcwd::libcw_do.marker().c_str() << "\".");
167   MyOwnDout(dc::warning, "MyOwnDout text " << my_own_do.get_indent() << ", \"" << my_own_do.margin().c_str() << "\", \"" << my_own_do.marker().c_str() << "\".");
168   ExampleDout(dc::warning, "ExampleDout text " << example::my_own_do.get_indent() << ", \"" << example::my_own_do.margin().c_str() << "\", \"" << example::my_own_do.marker().c_str() << "\".");
169 
170   Debug( libcw_do.margin().append("2Alibcw_do2", 11) );
171   Debug( my_own_do.margin().append("2Amy_own_do2", 12) );
172   Debug( example::my_own_do.margin().append("2Aexample::my_own_do2", 21) );
173 
174   Dout(dc::warning, "Dout text " << libcwd::libcw_do.get_indent() << ", \"" << libcwd::libcw_do.margin().c_str() << "\", \"" << libcwd::libcw_do.marker().c_str() << "\".");
175   MyOwnDout(dc::warning, "MyOwnDout text " << my_own_do.get_indent() << ", \"" << my_own_do.margin().c_str() << "\", \"" << my_own_do.marker().c_str() << "\".");
176   ExampleDout(dc::warning, "ExampleDout text " << example::my_own_do.get_indent() << ", \"" << example::my_own_do.margin().c_str() << "\", \"" << example::my_own_do.marker().c_str() << "\".");
177 
178   Debug( libcw_do.pop_margin() );
179   Debug( my_own_do.pop_margin() );
180   Debug( example::my_own_do.pop_margin() );
181 
182   Dout(dc::warning, "Dout text " << libcwd::libcw_do.get_indent() << ", \"" << libcwd::libcw_do.margin().c_str() << "\", \"" << libcwd::libcw_do.marker().c_str() << "\".");
183   MyOwnDout(dc::warning, "MyOwnDout text " << my_own_do.get_indent() << ", \"" << my_own_do.margin().c_str() << "\", \"" << my_own_do.marker().c_str() << "\".");
184   ExampleDout(dc::warning, "ExampleDout text " << example::my_own_do.get_indent() << ", \"" << example::my_own_do.margin().c_str() << "\", \"" << example::my_own_do.marker().c_str() << "\".");
185 
186   Debug( libcw_do.margin().append("3Alibcw_do3", 11) );
187   Debug( my_own_do.margin().append("3Amy_own_do3", 12) );
188   Debug( example::my_own_do.margin().append("3Aexample::my_own_do3", 21) );
189 
190   Dout(dc::warning, "Dout text " << libcwd::libcw_do.get_indent() << ", \"" << libcwd::libcw_do.margin().c_str() << "\", \"" << libcwd::libcw_do.marker().c_str() << "\".");
191   MyOwnDout(dc::warning, "MyOwnDout text " << my_own_do.get_indent() << ", \"" << my_own_do.margin().c_str() << "\", \"" << my_own_do.marker().c_str() << "\".");
192   ExampleDout(dc::warning, "ExampleDout text " << example::my_own_do.get_indent() << ", \"" << example::my_own_do.margin().c_str() << "\", \"" << example::my_own_do.marker().c_str() << "\".");
193 
194   Debug( libcw_do.pop_margin() );
195   Debug( my_own_do.pop_margin() );
196   Debug( example::my_own_do.pop_margin() );
197 
198   Dout(dc::warning, "Dout text " << libcwd::libcw_do.get_indent() << ", \"" << libcwd::libcw_do.margin().c_str() << "\", \"" << libcwd::libcw_do.marker().c_str() << "\".");
199   MyOwnDout(dc::warning, "MyOwnDout text " << my_own_do.get_indent() << ", \"" << my_own_do.margin().c_str() << "\", \"" << my_own_do.marker().c_str() << "\".");
200   ExampleDout(dc::warning, "ExampleDout text " << example::my_own_do.get_indent() << ", \"" << example::my_own_do.margin().c_str() << "\", \"" << example::my_own_do.marker().c_str() << "\".");
201 
202   // 1.1.2.4 Setting and getting the output stream
203 
204   Debug( libcw_do.margin().assign("> ", 2) );
205   Debug( my_own_do.margin().assign("* ", 2) );
206   Debug( example::my_own_do.margin().assign("- ", 2) );
207 
208   Debug( libcw_do.marker().assign(": ", 2) );
209   Debug( my_own_do.marker().assign(": ", 2) );
210   Debug( example::my_own_do.marker().assign(": ", 2) );
211 
212   Debug( libcw_do.set_indent(0) );
213   Debug( my_own_do.set_indent(0) );
214   Debug( example::my_own_do.set_indent(0) );
215 
216   my_own_do.set_ostream(&cout COMMA_THREADED(&cout_mutex));
217   my_own_os = my_own_do.get_ostream();
218 #ifndef THREADTEST
219   libcwd_os = libcwd::libcw_do.get_ostream();
220 #endif
221 
222   if (my_own_os != coutp
223 #ifndef THREADTEST
224       || libcwd_os != cerrp
225 #endif
226       )
227     DoutFatal(dc::core, "set_ostream failed");
228 
229   MyOwnDout(dc::notice, "This is written to cout");
230   my_own_do.set_ostream(&dummy COMMA_THREADED(&dummy_mutex));
231   MyOwnDout(dc::notice, "This is written to an ostringstream");
232   cout << flush;
233   Dout(dc::notice, "This is written to cerr");
234   my_own_do.set_ostream(cerrp COMMA_THREADED(&cerr_mutex));
235   MyOwnDout(dc::notice, "This is written to cerr");
236 #ifdef LIBCWD_USE_STRSTREAM
237   dummy << ends;
238 #endif
239   Dout(dc::warning, "Was written to ostringstream: \"" << dummy.str() << '"');
240 
241   EXIT(0);
242 }
243