1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
//=============================================================================
/**
* @file test_callback.cpp
*
* $Id: test_callback.cpp 93639 2011-03-24 13:32:13Z johnnyw $
*
* This program tests the <ACE_Log_Msg> class wrt writing to user
* defined callback objects. In particular, it tests to make sure
* that nested callbacks don't deadlock.
*
*
* @author Irfan Pyarali <irfan@cs.wustl.edu>
*/
//=============================================================================
#include "ace/OS_main.h"
#include "ace/Log_Msg.h"
#include "ace/Log_Msg_Callback.h"
#include "ace/Log_Record.h"
#include "ace/OS_NS_stdio.h"
/**
* @class Logger
Subclassing from ACE_Log_Msg_Callback means that an instance of
* Logger can be a target of a callback.
*/
class Logger : public ACE_Log_Msg_Callback
{
public:
// Constructor sets whether we're testing "recursive" callback
// logging!
Logger (int be_recursive = 1);
/// Logging callback hook.
virtual void log (ACE_Log_Record &log_record);
void verbose (int be_verbose);
private:
/// Flag for testing verbose logging.
int verbose_logging_;
/// Flag for testing recursive callback logging.
int recursive_;
};
void
Logger::verbose (int be_verbose)
{
this->verbose_logging_ = be_verbose;
}
Logger::Logger (int be_recursive)
: recursive_ (be_recursive)
{
}
void
Logger::log (ACE_Log_Record &log_record)
{
int use_log_msg = 0;
if (this->recursive_)
{
this->recursive_ = 0;
use_log_msg = 1;
}
if (!this->verbose_logging_)
{
if (use_log_msg)
ACE_DEBUG ((LM_DEBUG,
"Logger::log->%s",
log_record.msg_data ()));
else
ACE_OS::printf ("Recursive Logger callback = %s",
ACE_TEXT_ALWAYS_CHAR (log_record.msg_data ()));
}
else
{
ACE_TCHAR verbose_msg[ACE_Log_Record::MAXVERBOSELOGMSGLEN];
int result = log_record.format_msg (ACE_LOG_MSG->local_host (),
ACE_LOG_MSG->flags (),
verbose_msg);
if (result == 0)
{
if (use_log_msg)
ACE_DEBUG ((LM_DEBUG,
"Logger::log->%s",
verbose_msg));
else
ACE_OS::printf ("Recursive Logger callback = %s",
ACE_TEXT_ALWAYS_CHAR (verbose_msg));
}
}
// Cleanup on the way out.
if (use_log_msg)
this->recursive_ = 1;
}
int
ACE_TMAIN (int, ACE_TCHAR *[])
{
// This message should show up in stderr.
ACE_DEBUG ((LM_DEBUG,
"(%t) first message\n"));
ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR);
// This message should not show up anywhere since we disabled STDERR.
ACE_DEBUG ((LM_DEBUG,
"(%t) second message\n"));
ACE_LOG_MSG->set_flags (ACE_Log_Msg::MSG_CALLBACK);
// This message should not show up anywhere since no callback object
// has been specified.
ACE_DEBUG ((LM_DEBUG,
"(%t) third message\n"));
// Create a callback object and make it "verbose".
Logger logger;
logger.verbose (1);
// Set the callback object.
ACE_LOG_MSG->msg_callback (&logger);
// This message should show up via the Logger callback.
ACE_DEBUG ((LM_DEBUG,
"(%t) fourth message\n"));
ACE_LOG_MSG->set_flags (ACE_Log_Msg::VERBOSE_LITE);
// This message should show up via the Logger callback (somewhat
// verbosely).
ACE_DEBUG ((LM_DEBUG,
"(%t) fifth message\n"));
ACE_LOG_MSG->set_flags (ACE_Log_Msg::VERBOSE);
// This message should show up via the Logger callback (really
// verbosely).
ACE_DEBUG ((LM_DEBUG,
"(%t) sixth message\n"));
logger.verbose (0);
// This message should show up via the Logger callback (not
// verbosely).
ACE_DEBUG ((LM_DEBUG,
"(%t) seventh message\n"));
ACE_LOG_MSG->set_flags (ACE_Log_Msg::STDERR);
// This message should show up in stderr and the Logger callback.
// The one from the Logger callback will not be verbose, but the one
// from stderr should be verbose.
ACE_DEBUG ((LM_DEBUG,
"(%t) eighth message\n"));
return 0;
}
|