2 //=============================================================================
4 * @file test_callback.cpp
6 * This program tests the <ACE_Log_Msg> class wrt writing to user
7 * defined callback objects. In particular, it tests to make sure
8 * that nested callbacks don't deadlock.
10 * @author Irfan Pyarali <irfan@cs.wustl.edu>
12 //=============================================================================
15 #include "ace/OS_main.h"
16 #include "ace/Log_Msg.h"
17 #include "ace/Log_Msg_Callback.h"
18 #include "ace/Log_Record.h"
19 #include "ace/OS_NS_stdio.h"
24 Subclassing from ACE_Log_Msg_Callback means that an instance of
25 * Logger can be a target of a callback.
27 class Logger
: public ACE_Log_Msg_Callback
30 // Constructor sets whether we're testing "recursive" callback
32 Logger (int be_recursive
= 1);
34 /// Logging callback hook.
35 virtual void log (ACE_Log_Record
&log_record
);
37 void verbose (int be_verbose
);
40 /// Flag for testing verbose logging.
43 /// Flag for testing recursive callback logging.
48 Logger::verbose (int be_verbose
)
50 this->verbose_logging_
= be_verbose
;
53 Logger::Logger (int be_recursive
)
54 : recursive_ (be_recursive
)
59 Logger::log (ACE_Log_Record
&log_record
)
69 if (!this->verbose_logging_
)
74 log_record
.msg_data ()));
76 ACE_OS::printf ("Recursive Logger callback = %s",
77 ACE_TEXT_ALWAYS_CHAR (log_record
.msg_data ()));
81 ACE_TCHAR verbose_msg
[ACE_Log_Record::MAXVERBOSELOGMSGLEN
];
82 int result
= log_record
.format_msg (ACE_LOG_MSG
->local_host (),
83 ACE_LOG_MSG
->flags (),
85 ACE_Log_Record::MAXVERBOSELOGMSGLEN
);
93 ACE_OS::printf ("Recursive Logger callback = %s",
94 ACE_TEXT_ALWAYS_CHAR (verbose_msg
));
98 // Cleanup on the way out.
100 this->recursive_
= 1;
104 ACE_TMAIN (int, ACE_TCHAR
*[])
106 // This message should show up in stderr.
107 ACE_DEBUG ((LM_DEBUG
,
108 "(%t) first message\n"));
110 ACE_LOG_MSG
->clr_flags (ACE_Log_Msg::STDERR
);
112 // This message should not show up anywhere since we disabled STDERR.
113 ACE_DEBUG ((LM_DEBUG
,
114 "(%t) second message\n"));
116 ACE_LOG_MSG
->set_flags (ACE_Log_Msg::MSG_CALLBACK
);
118 // This message should not show up anywhere since no callback object
119 // has been specified.
120 ACE_DEBUG ((LM_DEBUG
,
121 "(%t) third message\n"));
123 // Create a callback object and make it "verbose".
127 // Set the callback object.
128 ACE_LOG_MSG
->msg_callback (&logger
);
130 // This message should show up via the Logger callback.
131 ACE_DEBUG ((LM_DEBUG
,
132 "(%t) fourth message\n"));
134 ACE_LOG_MSG
->set_flags (ACE_Log_Msg::VERBOSE_LITE
);
136 // This message should show up via the Logger callback (somewhat
138 ACE_DEBUG ((LM_DEBUG
,
139 "(%t) fifth message\n"));
141 ACE_LOG_MSG
->set_flags (ACE_Log_Msg::VERBOSE
);
143 // This message should show up via the Logger callback (really
145 ACE_DEBUG ((LM_DEBUG
,
146 "(%t) sixth message\n"));
150 // This message should show up via the Logger callback (not
152 ACE_DEBUG ((LM_DEBUG
,
153 "(%t) seventh message\n"));
155 ACE_LOG_MSG
->set_flags (ACE_Log_Msg::STDERR
);
157 // This message should show up in stderr and the Logger callback.
158 // The one from the Logger callback will not be verbose, but the one
159 // from stderr should be verbose.
160 ACE_DEBUG ((LM_DEBUG
,
161 "(%t) eighth message\n"));