Merge pull request #2254 from esohns/fstat_size_64_bits
[ACE_TAO.git] / ACE / examples / Log_Msg / test_callback.cpp
blob5ccef91eed597fb8f63bb67a84fca71554ad45e1
2 //=============================================================================
3 /**
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"
22 /**
23 * @class Logger
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
29 public:
30 // Constructor sets whether we're testing "recursive" callback
31 // logging!
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);
39 private:
40 /// Flag for testing verbose logging.
41 int verbose_logging_;
43 /// Flag for testing recursive callback logging.
44 int recursive_;
47 void
48 Logger::verbose (int be_verbose)
50 this->verbose_logging_ = be_verbose;
53 Logger::Logger (int be_recursive)
54 : recursive_ (be_recursive)
58 void
59 Logger::log (ACE_Log_Record &log_record)
61 int use_log_msg = 0;
63 if (this->recursive_)
65 this->recursive_ = 0;
66 use_log_msg = 1;
69 if (!this->verbose_logging_)
71 if (use_log_msg)
72 ACE_DEBUG ((LM_DEBUG,
73 "Logger::log->%s",
74 log_record.msg_data ()));
75 else
76 ACE_OS::printf ("Recursive Logger callback = %s",
77 ACE_TEXT_ALWAYS_CHAR (log_record.msg_data ()));
79 else
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 (),
84 verbose_msg,
85 ACE_Log_Record::MAXVERBOSELOGMSGLEN);
86 if (result == 0)
88 if (use_log_msg)
89 ACE_DEBUG ((LM_DEBUG,
90 "Logger::log->%s",
91 verbose_msg));
92 else
93 ACE_OS::printf ("Recursive Logger callback = %s",
94 ACE_TEXT_ALWAYS_CHAR (verbose_msg));
98 // Cleanup on the way out.
99 if (use_log_msg)
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".
124 Logger logger;
125 logger.verbose (1);
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
137 // verbosely).
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
144 // verbosely).
145 ACE_DEBUG ((LM_DEBUG,
146 "(%t) sixth message\n"));
148 logger.verbose (0);
150 // This message should show up via the Logger callback (not
151 // verbosely).
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"));
162 return 0;