This class provides a way to dynamically configure the ACE logging mechanism at run time as well as enable the mechanisms for limiting log file size and log file backup/rotation capability. More...
#include <Logging_Strategy.h>
Public Member Functions | |
ACE_Logging_Strategy (void) | |
Constructor. | |
~ACE_Logging_Strategy (void) | |
Destructor. | |
virtual int | init (int argc, ACE_TCHAR *argv[]) |
Dynamic linking initialization hook. | |
virtual int | fini (void) |
Dynamic linking termination hook. | |
virtual int | handle_timeout (const ACE_Time_Value &tv, const void *arg) |
virtual int | handle_close (ACE_HANDLE, ACE_Reactor_Mask) |
virtual void | reactor (ACE_Reactor *r) |
virtual ACE_Reactor * | reactor (void) const |
int | parse_args (int argc, ACE_TCHAR *argv[]) |
void | log_msg (ACE_Log_Msg *log_msg) |
Protected Member Functions | |
void | tokenize (ACE_TCHAR *flag_string) |
Tokenize to set all the flags. | |
void | priorities (ACE_TCHAR *priority_string, ACE_Log_Msg::MASK_TYPE mask) |
Tokenize to set priorities (either process or thread one). | |
Protected Attributes | |
u_long | thread_priority_mask_ |
Current thread's priority mask set by priorities . | |
u_long | process_priority_mask_ |
Process-wide priority mask set by priorities . | |
u_long | flags_ |
Flags we keep track of. | |
ACE_TCHAR * | filename_ |
File name we're logging to. | |
ACE_TCHAR * | logger_key_ |
Logger key for distributed logging. | |
ACE_TCHAR * | program_name_ |
Program name to be used for n format specifier. | |
bool | wipeout_logfile_ |
bool | fixed_number_ |
bool | order_files_ |
int | count_ |
int | max_file_number_ |
u_long | interval_ |
u_long | max_size_ |
ACE_Log_Msg * | log_msg_ |
ACE_Log_Msg instance to work with. |
This class provides a way to dynamically configure the ACE logging mechanism at run time as well as enable the mechanisms for limiting log file size and log file backup/rotation capability.
Depending upon when this service is invoked and with what flags, the output of other network services can be controlled. The output can be streamed to stderr, to a file, to a logging daemon, or it can be set to be "silent". If logging records are output to a file, the file can be set to a maximum size and repeatedly split into new files. The log file size can be limited at any logging point (i.e., application, client logging daemon, or server logging daemon) by specifying the -i
sample_interval_in_secs | and -m | |
max_size_in_KB | options for the Logging_Strategy class in a svc.conf file. |
By default, two logfiles are generated. It's possible, however, to generate as many logfiles as necessary to store all the information. To achieve this, it is only necessary to indicate the maximum size of the logfiles via the -m option and the process will generate automatically the logfiles. You can control the total number of logfiles created via the -n option.
By using the -o option we can also choose the mode of organization of the files, e.g., the first one is the normal used in Unix systems (when cron rotates the logs it keeps the lowest number the most recent one), the second is for increasing speed (we only create a new log file, and don't rotate the others (fewer accesses to disk)).
By default, the ACE_Logging_Strategy
uses the singleton reactor, i.e., what's returned by ACE_Reactor::instance()
. If you want to set the reactor used by ACE_Logging_Strategy
to something other than the singleton reactor you'll need to get a pointer to the ACE_Logging_Strategy
instance and do this
ACE_Reactor my_reactor; ACE_Logging_Strategy *logging_strategy = ...... // Get instance.
logging_strategy->reactor (&my_reactor);
and then logging_strategy will use your reactor. If you're dynamically linking the ACE_Logging_Strategy
then you can use the ACE_Dynamic_Service
template to get a pointer to the ACE_Logging_Strategy
.
Definition at line 80 of file Logging_Strategy.h.
ACE_Logging_Strategy::ACE_Logging_Strategy | ( | void | ) |
Constructor.
Definition at line 231 of file Logging_Strategy.cpp.
: thread_priority_mask_ (0), process_priority_mask_ (0), flags_ (0), filename_ (0), logger_key_ (0), program_name_ (0), wipeout_logfile_ (false), fixed_number_ (false), order_files_ (false), count_ (0), max_file_number_ (1), // 2 files by default (max file number + 1) interval_ (ACE_DEFAULT_LOGFILE_POLL_INTERVAL), max_size_ (0), log_msg_ (ACE_Log_Msg::instance ()) { #if defined (ACE_DEFAULT_LOGFILE) this->filename_ = ACE::strnew (ACE_DEFAULT_LOGFILE); #else /* ACE_DEFAULT_LOGFILE */ ACE_NEW (this->filename_, ACE_TCHAR[MAXPATHLEN + 1]); // Get the temporary directory if (ACE::get_temp_dir (this->filename_, MAXPATHLEN - 7) == -1) // 7 for "logfile" { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Temporary path too long, ") ACE_TEXT ("defaulting to current directory\n"))); this->filename_[0] = 0; } // Add the filename to the end ACE_OS::strcat (this->filename_, ACE_TEXT ("logfile")); #endif /* ACE_DEFAULT_LOGFILE */ }
ACE_Logging_Strategy::~ACE_Logging_Strategy | ( | void | ) |
Destructor.
Definition at line 270 of file Logging_Strategy.cpp.
{ // This is allocated in constructor, so it must be deallocated in // the destructor! delete [] this->filename_; }
int ACE_Logging_Strategy::fini | ( | void | ) | [virtual] |
Dynamic linking termination hook.
Reimplemented from ACE_Shared_Object.
Definition at line 278 of file Logging_Strategy.cpp.
{ delete [] this->filename_; this->filename_ = 0; // Avoid double deletions. delete [] this->logger_key_; delete [] this->program_name_; if (this->reactor () && this->interval_ > 0 && this->max_size_ > 0) this->reactor ()->cancel_timer (this); return 0; }
int ACE_Logging_Strategy::handle_close | ( | ACE_HANDLE | , | |
ACE_Reactor_Mask | ||||
) | [virtual] |
This function helps to cancel timer events for this logging strategy in reactor during shutdown.
Definition at line 551 of file Logging_Strategy.cpp.
{ // This will reset reactor member and cancel timer events. this->reactor (0); return 0; }
int ACE_Logging_Strategy::handle_timeout | ( | const ACE_Time_Value & | tv, | |
const void * | arg | |||
) | [virtual] |
Timeout handler which tests logfile size. If the current logfile size exceeds max_size_
, the current logfile is closed, saved to logfile.old, and a new logfile is reopened.
Definition at line 402 of file Logging_Strategy.cpp.
{ #if defined (ACE_LACKS_IOSTREAM_TOTALLY) if ((size_t) ACE_OS::ftell (this->log_msg_->msg_ostream ()) > this->max_size_) #else if ((size_t) this->log_msg_->msg_ostream ()->tellp () > this->max_size_) #endif /* ACE_LACKS_IOSTREAM_TOTALLY */ { // Lock out any other logging. if (this->log_msg_->acquire ()) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Cannot acquire lock!\n")), -1); // Close the current ostream. #if defined (ACE_LACKS_IOSTREAM_TOTALLY) FILE *output_file = (FILE *) this->log_msg_->msg_ostream (); ACE_OS::fclose (output_file); // We'll call msg_ostream() modifier later. #else ofstream *output_file = (ofstream *) this->log_msg_->msg_ostream (); output_file->close (); #endif /* ACE_LACKS_IOSTREAM_TOTALLY */ // Save current logfile to logfile.old analyze if it was set any // fixed number for the log_files. if (fixed_number_) { if (max_file_number_ < 1) //we only want one file { // Just unlink the file. ACE_OS::unlink (this->filename_); // Open a new log file with the same name. #if defined (ACE_LACKS_IOSTREAM_TOTALLY) output_file = ACE_OS::fopen (this->filename_, ACE_TEXT ("wt")); if (output_file == 0) return -1; this->log_msg_->msg_ostream (output_file); #else output_file->open (ACE_TEXT_ALWAYS_CHAR (this->filename_), ios::out); #endif /* ACE_LACKS_IOSTREAM_TOTALLY */ // Release the lock previously acquired. this->log_msg_->release (); return 0; } } count_++; // Set the number of digits of the log_files labels. int digits = 1, res = count_; while((res = (res / 10))>0) digits++; if (ACE_OS::strlen (this->filename_) + digits <= MAXPATHLEN) { ACE_TCHAR backup[MAXPATHLEN+1]; // analyse if it was chosen the mode which will order the // log_files if (order_files_) { ACE_TCHAR to_backup[MAXPATHLEN+1]; // reorder the logs starting at the oldest (the biggest // number) watch if we reached max_file_number_. int max_num; if (fixed_number_ && count_ > max_file_number_) // count_ will always be bigger than max_file_number_, // so do nothing so to always reorder files from // max_file_number_. max_num = max_file_number_; else max_num = count_; for (int i = max_num ; i > 1 ;i--) { ACE_OS::sprintf (backup, ACE_TEXT ("%s.%d"), this->filename_, i); ACE_OS::sprintf (to_backup, ACE_TEXT ("%s.%d"), this->filename_, i - 1); // Remove any existing old file; ignore error as // file may not exist. ACE_OS::unlink (backup); // Rename the current log file to the name of the // backup log file. ACE_OS::rename (to_backup, backup); } ACE_OS::sprintf (backup, ACE_TEXT ("%s.1"), this->filename_); } else { if (fixed_number_ && count_>max_file_number_) count_ = 1; // start over from 1 ACE_OS::sprintf (backup, ACE_TEXT ("%s.%d"), this->filename_, count_); } // Remove any existing old file; ignore error as file may // not exist. ACE_OS::unlink (backup); // Rename the current log file to the name of the backup log // file. ACE_OS::rename (this->filename_, backup); } else ACE_ERROR ((LM_ERROR, ACE_TEXT ("Backup file name too long; ") ACE_TEXT ("backup logfile not saved.\n"))); // Open a new log file by the same name #if defined (ACE_LACKS_IOSTREAM_TOTALLY) output_file = ACE_OS::fopen (this->filename_, ACE_TEXT ("wt")); if (output_file == 0) return -1; this->log_msg_->msg_ostream (output_file); #else output_file->open (ACE_TEXT_ALWAYS_CHAR (this->filename_), ios::out); #endif /* ACE_LACKS_IOSTREAM_TOTALLY */ // Release the lock previously acquired. this->log_msg_->release (); } return 0; }
int ACE_Logging_Strategy::init | ( | int | argc, | |
ACE_TCHAR * | argv[] | |||
) | [virtual] |
Dynamic linking initialization hook.
Reimplemented from ACE_Shared_Object.
Definition at line 294 of file Logging_Strategy.cpp.
{ ACE_TRACE ("ACE_Logging_Strategy::init"); // Store current priority masks for changes in <parse_args>. this->process_priority_mask_ = this->log_msg_->priority_mask (ACE_Log_Msg::PROCESS); this->thread_priority_mask_ = this->log_msg_->priority_mask (ACE_Log_Msg::THREAD); // Use the options hook to parse the command line arguments. this->parse_args (argc, argv); // Setup priorities (to original if not specified on command line) this->log_msg_->priority_mask (thread_priority_mask_, ACE_Log_Msg::THREAD); this->log_msg_->priority_mask (process_priority_mask_, ACE_Log_Msg::PROCESS); // Check if any flags were specified. If none were specified, let // the default behavior take effect. if (this->flags_ != 0) { // Clear all flags this->log_msg_->clr_flags (ACE_Log_Msg::STDERR | ACE_Log_Msg::LOGGER | ACE_Log_Msg::OSTREAM | ACE_Log_Msg::VERBOSE | ACE_Log_Msg::VERBOSE_LITE | ACE_Log_Msg::SILENT | ACE_Log_Msg::SYSLOG); // Check if OSTREAM bit is set if (ACE_BIT_ENABLED (this->flags_, ACE_Log_Msg::OSTREAM)) { int delete_ostream = 0; #if defined (ACE_LACKS_IOSTREAM_TOTALLY) FILE *output_file = this->log_msg_->msg_ostream (); if (wipeout_logfile_) { // close and re-open a stream if such exits if (output_file && ACE_OS::fclose (output_file) == -1) return -1; output_file = ACE_OS::fopen (this->filename_, ACE_TEXT ("wt")); } // open a stream only if such doesn't exists else if (output_file == 0) output_file = ACE_OS::fopen (this->filename_, ACE_TEXT ("at")); if (output_file == 0) return -1; #else ostream *output_file = this->log_msg_->msg_ostream (); // Create a new ofstream to direct output to the file. if (wipeout_logfile_) { ACE_NEW_RETURN (output_file, ofstream (ACE_TEXT_ALWAYS_CHAR (this->filename_)), -1); delete_ostream = 1; } else if (output_file == 0) { ACE_NEW_RETURN (output_file, ofstream (ACE_TEXT_ALWAYS_CHAR (this->filename_), ios::app | ios::out), -1); delete_ostream = 1; } if (output_file->rdstate () != ios::goodbit) { if (delete_ostream) delete output_file; return -1; } #endif /* ACE_LACKS_IOSTREAM_TOTALLY */ // Set the <output_file> that'll be used by the rest of the // code. this->log_msg_->msg_ostream (output_file, delete_ostream); // Setup a timeout handler to perform the maximum file size // check (if required). if (this->interval_ > 0 && this->max_size_ > 0) { if (this->reactor () == 0) // Use singleton. this->reactor (ACE_Reactor::instance ()); } } // Now set the flags for Log_Msg this->log_msg_->set_flags (this->flags_); } return this->log_msg_->open (this->program_name_, this->log_msg_->flags (), this->logger_key_); }
void ACE_Logging_Strategy::log_msg | ( | ACE_Log_Msg * | log_msg | ) |
Definition at line 588 of file Logging_Strategy.cpp.
{ this->log_msg_ = log_msg; }
int ACE_Logging_Strategy::parse_args | ( | int | argc, | |
ACE_TCHAR * | argv[] | |||
) |
Parse arguments provided in svc.conf file.
Definition at line 139 of file Logging_Strategy.cpp.
{ ACE_TRACE ("ACE_Logging_Strategy::parse_args"); ACE_TCHAR *temp; // Perform data member initializations. BTW, do *not* initialize // <thread_priority_mask_> or <process_priority_mask_> here to avoid // unduing the behavior in <init>, where these are set by // <ACE_Log_Msg::instance>. this->flags_ = 0; this->wipeout_logfile_ = false; this->count_ = 0; this->fixed_number_ = false; this->order_files_ = false; this->max_file_number_ = 1; this->interval_ = ACE_DEFAULT_LOGFILE_POLL_INTERVAL; this->max_size_ = 0; ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("f:i:k:m:n:N:op:s:t:w"), 0); for (int c; (c = get_opt ()) != -1; ) { switch (c) { case 'f': temp = get_opt.opt_arg (); // Now tokenize the string to get all the flags this->tokenize (temp); // If LOGGER was specified, set up the default logger key. // The key can be changed by the -k option also, so if it's // been set already, don't set it. if (ACE_BIT_ENABLED (this->flags_, ACE_Log_Msg::LOGGER) && this->logger_key_ == 0) this->logger_key_ = ACE::strnew (ACE_DEFAULT_LOGGER_KEY); break; case 'i': // Interval (in secs) at which logfile size is sampled. this->interval_ = ACE_OS::strtoul (get_opt.opt_arg (), 0, 10); break; case 'k': // Ensure that the LOGGER flag is set ACE_SET_BITS (this->flags_, ACE_Log_Msg::LOGGER); delete [] this->logger_key_; this->logger_key_ = ACE::strnew (get_opt.opt_arg ()); break; case 'm': // Maximum logfile size (in KB). Must be a non-zero value. this->max_size_ = ACE_OS::strtoul (get_opt.opt_arg (), 0, 10); this->max_size_ <<= 10; // convert from KB to bytes. break; case 'n': delete [] this->program_name_; this->program_name_ = ACE::strnew (get_opt.opt_arg ()); break; case 'N': // The max number for the log_file being created this->max_file_number_ = ACE_OS::atoi (get_opt.opt_arg ()) - 1; this->fixed_number_ = true; break; case 'o': // Log_files generation order this->order_files_ = true; break; case 'p': temp = get_opt.opt_arg (); // Now tokenize the string to setup process log priority this->priorities (temp, ACE_Log_Msg::PROCESS); break; case 's': // Ensure that the OSTREAM flag is set ACE_SET_BITS (this->flags_, ACE_Log_Msg::OSTREAM); delete [] this->filename_; this->filename_ = ACE::strnew (get_opt.opt_arg ()); break; case 't': temp = get_opt.opt_arg (); // Now tokenize the string to setup thread log priority this->priorities (temp, ACE_Log_Msg::THREAD); break; case 'w': // Cause the logfile to be wiped out, both on startup and on // reconfigure. this->wipeout_logfile_ = true; break; default: break; } } return 0; }
void ACE_Logging_Strategy::priorities | ( | ACE_TCHAR * | priority_string, | |
ACE_Log_Msg::MASK_TYPE | mask | |||
) | [protected] |
Tokenize to set priorities (either process or thread one).
void ACE_Logging_Strategy::reactor | ( | ACE_Reactor * | r | ) | [virtual] |
Reactor accessors. If reactor changes then we need remove this event handler from previous reactor and scheduler for timer events in a new one.
Definition at line 560 of file Logging_Strategy.cpp.
{ if (this->reactor () != r) { if (this->reactor () && this->interval_ > 0 && this->max_size_ > 0) { this->reactor ()->cancel_timer (this); } ACE_Service_Object::reactor (r); if (this->reactor ()) { this->reactor ()->schedule_timer (this, 0, ACE_Time_Value (this->interval_), ACE_Time_Value (this->interval_)); } } }
ACE_Reactor * ACE_Logging_Strategy::reactor | ( | void | ) | const [virtual] |
Definition at line 582 of file Logging_Strategy.cpp.
{ return ACE_Service_Object::reactor (); }
void ACE_Logging_Strategy::tokenize | ( | ACE_TCHAR * | flag_string | ) | [protected] |
Tokenize to set all the flags.
Definition at line 111 of file Logging_Strategy.cpp.
{ ACE_TCHAR *strtokp; for (ACE_TCHAR *flag = ACE_OS::strtok_r (flag_string, ACE_TEXT ("|"), &strtokp); flag != 0; flag = ACE_OS::strtok_r (0, ACE_TEXT ("|"), &strtokp)) { if (ACE_OS::strcmp (flag, ACE_TEXT ("STDERR")) == 0) ACE_SET_BITS (this->flags_, ACE_Log_Msg::STDERR); else if (ACE_OS::strcmp (flag, ACE_TEXT ("LOGGER")) == 0) ACE_SET_BITS (this->flags_, ACE_Log_Msg::LOGGER); else if (ACE_OS::strcmp (flag, ACE_TEXT ("OSTREAM")) == 0) ACE_SET_BITS (this->flags_, ACE_Log_Msg::OSTREAM); else if (ACE_OS::strcmp (flag, ACE_TEXT ("VERBOSE")) == 0) ACE_SET_BITS (this->flags_, ACE_Log_Msg::VERBOSE); else if (ACE_OS::strcmp (flag, ACE_TEXT ("VERBOSE_LITE")) == 0) ACE_SET_BITS (this->flags_, ACE_Log_Msg::VERBOSE_LITE); else if (ACE_OS::strcmp (flag, ACE_TEXT ("SILENT")) == 0) ACE_SET_BITS (this->flags_, ACE_Log_Msg::SILENT); else if (ACE_OS::strcmp (flag, ACE_TEXT ("SYSLOG")) == 0) ACE_SET_BITS (this->flags_, ACE_Log_Msg::SYSLOG); } }
int ACE_Logging_Strategy::count_ [protected] |
This tells us in what file we last wrote. It will be increased to enable multiple log files
Definition at line 188 of file Logging_Strategy.h.
ACE_TCHAR* ACE_Logging_Strategy::filename_ [protected] |
File name we're logging to.
Definition at line 166 of file Logging_Strategy.h.
bool ACE_Logging_Strategy::fixed_number_ [protected] |
If true we have a maximum number of log files we can write. Default value is false, i.e., no maximum number.
Definition at line 180 of file Logging_Strategy.h.
u_long ACE_Logging_Strategy::flags_ [protected] |
Flags we keep track of.
Definition at line 163 of file Logging_Strategy.h.
u_long ACE_Logging_Strategy::interval_ [protected] |
If non-zero, sampling interval (in secs) at which maximum logfile size is checked, otherwise logfile size can grow indefinitely. Default value is 0.
Definition at line 198 of file Logging_Strategy.h.
ACE_Log_Msg* ACE_Logging_Strategy::log_msg_ [protected] |
ACE_Log_Msg instance to work with.
Definition at line 205 of file Logging_Strategy.h.
ACE_TCHAR* ACE_Logging_Strategy::logger_key_ [protected] |
Logger key for distributed logging.
Definition at line 169 of file Logging_Strategy.h.
int ACE_Logging_Strategy::max_file_number_ [protected] |
Tells us what is the maximum log file to write. We will write max_file_number_
+ 1 files (includes the current log file). Default value is 1, i.e., 2 files by default.
Definition at line 193 of file Logging_Strategy.h.
u_long ACE_Logging_Strategy::max_size_ [protected] |
Maximum logfile size (in KB). Default value is ACE_DEFAULT_MAX_LOGFILE_SIZE.
Definition at line 202 of file Logging_Strategy.h.
bool ACE_Logging_Strategy::order_files_ [protected] |
If true we order the files as we rotate them. Default value is false, i.e., we do not rotate files by default.
Definition at line 184 of file Logging_Strategy.h.
u_long ACE_Logging_Strategy::process_priority_mask_ [protected] |
Process-wide priority mask set by priorities
.
Definition at line 160 of file Logging_Strategy.h.
ACE_TCHAR* ACE_Logging_Strategy::program_name_ [protected] |
Program name to be used for n format specifier.
Definition at line 172 of file Logging_Strategy.h.
u_long ACE_Logging_Strategy::thread_priority_mask_ [protected] |
Current thread's priority mask set by priorities
.
Definition at line 157 of file Logging_Strategy.h.
bool ACE_Logging_Strategy::wipeout_logfile_ [protected] |
If true then wipeout the logfile, otherwise append to it. Default value is false.
Definition at line 176 of file Logging_Strategy.h.