ACE stderr( )、 、 (Output Stream)、
C/S ,ACE 。 。
ACE
ACE (logging sink) ACE_Log_Msg open ,
set_flags clr_flags 。
open :
/**
* Initialize the ACE logging facility. Supplies the program name
* that is available to each logging message call. Default arguments
* set up logging to STDERR only.
*
* @param prog_name The name of the calling program.
* @param options_flags A bitwise-or of options flags used to set
* the initial behavior and logging sink(s).
* (see the enum above for the valid values).
* @param logger_key The name of ACE_FIFO rendezvous point where
* the local client logger daemon is listening
* for logging messages. Only meaningful if the
* LOGGER bit is set in the @a flags argument.
*/
int open (const ACE_TCHAR *prog_name,
u_long options_flags = ACE_Log_Msg::STDERR,
const ACE_TCHAR *logger_key = 0);
prog_name , , ACE_Log_Msg program_name
, ACE_Log_Msg::VERBOSE , ,
program_name。
:
STDERR Write messages to STDERR
LOGGER Write messages to the local client logger daemon
OSTREAM Write messages to the assigned output stream
MSG_CALLBACK Write messages to the callback object
VERBOSE Prepends program name, timestamp, host name, process ID,
and message priority to each message
VERBOSE_LITE Prepends timestamp and message priority to each message
SILENT Do not print messages at all
SYSLOG Write messages to the system's event log
CUSTOM Write messages to the user-provided back end
logger key LOGGER(C/S ) ,
, :ACE_DEFAULT_LOGGER_KEY。
, sink 。
set_flags、clr_flags :
// Enable the bits in the logger's options flags.
void set_flags (unsigned long f);
// Disable the bits in the logger's options flags.
void clr_flags (unsigned long f);
open options_flags, set_flags
( ),clr_flags 。
sink 。
1. stderr
:
ACE_LOG_MSG->open (argv[0], ACE_Log_Msg::STDERR);
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("test/n")));
:
ACE_LOG_MSG->set_flags (ACE_Log_Msg::STDERR);
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("test/n")));
2. System Logger
ACE_LOG_MSG->open(argv[0], ACE_Log_Msg::SYSLOG, ACE_TEXT ("ACE log"));
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("test/n")));
System Logger set_flags、clr_flags 。Windows ,
Event Log , Event Source Open 。Unix/Linux
syslog facility。 Windows LM_STARTUP、LM_SHUTDOWN、LM_TRACE、
LM_DEBUG、LM_INFO EVENTLOG_INFORMATION_TYPE;LM_NOTICE、LM_WARNING
EVENTLOG_WARNING_TYPE、LM_ERROR、LM_CRITICAL、LM_ALERT、LM_EMERGENCY
EVENTLOG_ERROR_TYPE。
3. Output Streams
(ofstream):
#include < ace/Log_Msg.h >
#include < ace/streams.h >
int ACE_TMAIN (int, ACE_TCHAR *argv[])
{
ACE_OSTREAM_TYPE *output = new ofstream ("test.txt");
ACE_LOG_MSG->msg_ostream (output, 1);
ACE_LOG_MSG->set_flags (ACE_Log_Msg::OSTREAM);
ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR);
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("test/n")));
return 0;
}
msg_ostream assign ACE OSTREAM, log sink
ACE_Log_Msg::OSTREAM, log sink set_flags open ,
set_flags clr_flags
ACE_Log_Msg::STDERR( sink), stderr 。
msg_ostream ( , ):
/**
* delete_stream == 1, forces Log_Msg.h to delete the stream in
* its own ~dtor (assumes control of the stream)
* use only with proper ostream (eg: fstream), not (cout, cerr)
*/
void msg_ostream (ACE_OSTREAM_TYPE *, int delete_ostream);
delete_stream == 1, delete output;
ofstream。 ostream cout、cerr、clog ,
cout ,
:
ACE_LOG_MSG->msg_ostream (&cout);
ACE_LOG_MSG->open (argv[0], ACE_Log_Msg::OSTREAM);
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("test/n")));
4. Callback
ACE , , :
(1) ACE_Log_Msg_Callback ,
virtual void log (ACE_Log_Record &log_record).
(2) ACE_Log_Msg ( MyCallback):
Callback *callback = new Callback;
ACE_LOG_MSG->set_flags (ACE_Log_Msg::MSG_CALLBACK);
ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR);
ACE_LOG_MSG->msg_callback (callback);
(3) , ACE_DEBUG, MyCallback log 。
(4) :
ACE_LOG_MSG->clr_flags (ACE_Log_Msg::MSG_CALLBACK);
delete callback;
ACE_Log_Msg_Callback log ACE_Log_Record,
print :
int print (const ACE_TCHAR host_name[], u_long verbose_flag, FILE *fp);
:
int print (const ACE_TCHAR host_name[], u_long verbose_flag,
ACE_OSTREAM_TYPE &stream);
, ACE_Log_Msg::VERBOSE、VERBOSE_LITE
0( ), , :
log_record.print (ACE_TEXT (""), ACE_Log_Msg::VERBOSE, cout);
/* */
log_record.print (ACE_TEXT (""), 0, ofstream("test.txt",ios::app));
/* */
ACE_Log_Record , :
/* main.cpp */
#include < ace/Task.h >
#include < ace/Log_Msg.h >
#include " MyCallback.h "
int ACE_TMAIN (int, ACE_TCHAR *[])
{
MyCallback *callback = new MyCallback;
ACE_LOG_MSG->set_flags (ACE_Log_Msg::MSG_CALLBACK);
ACE_LOG_MSG->clr_flags (ACE_Log_Msg::STDERR);
ACE_LOG_MSG->msg_callback (callback);
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("test1/n")));
ACE_DEBUG ((LM_INFO, ACE_TEXT ("test2/n")));
ACE_LOG_MSG->clr_flags (ACE_Log_Msg::MSG_CALLBACK);
delete callback;
return 0;
}
/* MyCallback.h */
#include < ace/streams.h >
#include < ace/Log_Msg.h >
#include < ace/Log_Msg_Callback.h >
#include < ace/Log_Record.h >
#include < ace/streams.h >
#include < ace/Log_Msg_Callback.h >
#include < ace/Log_Record.h >
#include < ace/SString.h >
#include < ace/OS.h >
class MyCallback : public ACE_Log_Msg_Callback
{
public:
void log (ACE_Log_Record &log_record)
{
cerr << "Log Message Received:" << endl;
unsigned long msg_severity = log_record.type ();
ACE_Log_Priority prio = ACE_static_cast (ACE_Log_Priority, msg_severity);
const ACE_TCHAR *prio_name = ACE_Log_Record::priority_name (prio);
cerr << "/tType: "
<< ACE_TEXT_ALWAYS_CHAR (prio_name)
<< endl;
cerr << "/tLength: " << log_record.length () << endl;
const time_t epoch = log_record.time_stamp ().sec ();
cerr << "/tTime_Stamp: "
<< ACE_TEXT_ALWAYS_CHAR (ACE_OS::ctime (&epoch))
<< flush;
cerr << "/tPid: " << log_record.pid () << endl;
ACE_CString data (">> ");
data += ACE_TEXT_ALWAYS_CHAR (log_record.msg_data ());
cerr << "/tMsgData: " << data.c_str () << endl;
}
};
:
Log Message Received:
Type: LM_DEBUG
Length: 32
Time_Stamp: Mon Mar 27 17:03:06 2005
Pid: 2752
MsgData: >> test1
Log Message Received:
Type: LM_INFO
Length: 32
Time_Stamp: Mon Mar 27 17:03:06 2005
Pid: 2752
MsgData: >> test2
, ACE_Log_Msg , spawn ,
Callback , callback,
:ACE_LOG_MSG->msg_callback (callback);
5.C/S
(distributed logger),logging server
, logging requests. ( )
logging client daemon , proxy,
logging server 。 :
#include < ace/Log_Msg.h >
int ACE_TMAIN (int, ACE_TCHAR *argv[])
{
ACE_LOG_MSG->open (argv[0],
ACE_Log_Msg::LOGGER,
ACE_DEFAULT_LOGGER_KEY);
ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Sent to Logging Server/n")));
return 0;
}
:
(1) logging server:
%ACE_ROOT%/netsvcs/servers/main -f server.conf
(2) logging client daemon:
%ACE_ROOT%/netsvcs/servers/main -f client.conf
server.conf :
dynamic Logger Service_Object * ACE:_make_ACE_Logging_Strategy() "-s log.txt -f STDERR|OSTREAM"
dynamic Server_Logging_Service Service_Object * netsvcs:_make_ACE_Server_Logging_Acceptor()
active "-p 20009"
dynamic , logging strategy,
log.txt(-s ), STDERR。
logging server (listen) 20009。
client.conf :
dynamic Client_Logging_Service Service_Object * netsvcs:_make_ACE_Client_Logging_Acceptor()
active "-p 20009 -h localhost"
dynamic , logging client daemon 2009
( server.conf ), -h ip( )。
logging server logging client daemon , ,
logging server :
Sent to Logging Server
logging server (%ACE_ROOT%/netsvcs/servers/)
log.txt, :
starting up Logging Server at port 20009 on handle 1900
Sent to Logging Server
logging ,
。 ACE_SOCK_Stream l
ogging server , logging client daemon, 。
server.conf client.conf logging ACE
Runtime Configuration, 。 :
-f Specify ACE_Log_Msg flags (OSTREAM, STDERR, LOGGER, VERBOSE,
SILENT, VERBOSE_LITE) used to control logging.
-i The interval, in seconds, at which the log file size is sampled
(default is 0; do not sample by default).
-k Specify the rendezvous point for the client logger
( ACE_DEFAULT_LOGGER_KEY)
-m The maximum log file size in Kbytes.
-n Set the program name for the %n format specifier.
-N The maximum number of log files to create.
-o Request the standard log file ordering (keeps multiple log files
in numbered order). Default is not to order log files.
-p Pass in the processwide priorities to either enable (DEBUG, INFO,
WARNING,NOTICE, ERROR,CRITICAL, ALERT, EMERGENCY) or to disable
(~DEBUG, ~INFO, ~WARNING, ~NOTICE, ~ERROR, ~CRITICAL, ~ALERT, ~EMERGENCY).
-s Specify the file name used when OSTREAM is specified as an output target.
-t Pass in the per instance priorities to either enable (DEBUG, INFO,
WARNING, NOTICE, ERROR, CRITICAL, ALERT, EMERGENCY) or to disable
(~DEBUG, ~INFO, ~WARNING, ~NOTICE, ~ERROR, ~CRITICAL, ~ALERT, ~EMERGENCY).
-w Cause the log file to be wiped out on both start-up and reconfiguration.