00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Logging_Strategy.h 00006 * 00007 * $Id: Logging_Strategy.h 91064 2010-07-12 10:11:24Z johnnyw $ 00008 * 00009 * @author Prashant Jain <pjain@cs.wustl.edu> 00010 * @author Orlando Ribeiro <oribeiro@inescporto.pt> 00011 */ 00012 //============================================================================= 00013 00014 #ifndef ACE_LOGGING_STRATEGY_H 00015 #define ACE_LOGGING_STRATEGY_H 00016 00017 #include "ace/Service_Object.h" 00018 #include "ace/Log_Msg.h" 00019 00020 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00021 # pragma once 00022 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00023 00024 #if !defined (ACE_DEFAULT_LOGFILE_POLL_INTERVAL) 00025 #define ACE_DEFAULT_LOGFILE_POLL_INTERVAL 600 /* Seconds */ 00026 #endif /* ACE_DEFAULT_LOGFILE_POLL_INTERVAL */ 00027 00028 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00029 00030 /** 00031 * @class ACE_Logging_Strategy 00032 * 00033 * @brief 00034 * This class provides a way to dynamically configure the ACE logging 00035 * mechanism at run time as well as enable the mechanisms for limiting 00036 * log file size and log file backup/rotation capability. 00037 * 00038 * Depending upon when this service is invoked and with what 00039 * flags, the output of other network services can be 00040 * controlled. The output can be streamed to stderr, to a file, 00041 * to a logging daemon, or it can be set to be "silent". 00042 * If logging records are output to a file, the file can be set 00043 * to a maximum size and repeatedly split into new files. The 00044 * log file size can be limited at any logging point (i.e., 00045 * application, client logging daemon, or server logging daemon) 00046 * by specifying the -i @param sample_interval_in_secs and -m 00047 * @param max_size_in_KB options for the Logging_Strategy class in a 00048 * svc.conf file. 00049 * 00050 * By default, two logfiles are generated. It's possible, however, to 00051 * generate as many logfiles as necessary to store all the 00052 * information. To achieve this, it is only necessary to indicate the 00053 * maximum size of the logfiles via the -m option and the process will 00054 * generate automatically the logfiles. You can control the total 00055 * number of logfiles created via the -n option. 00056 * 00057 * By using the -o option we can also choose the mode of organization 00058 * of the files, e.g., the first one is the normal used in Unix 00059 * systems (when cron rotates the logs it keeps the lowest number the 00060 * most recent one), the second is for increasing speed (we only 00061 * create a new log file, and don't rotate the others (fewer accesses 00062 * to disk)). 00063 * 00064 * By default, the @c ACE_Logging_Strategy uses the singleton reactor, 00065 * i.e., what's returned by @c ACE_Reactor::instance(). If you want 00066 * to set the reactor used by @c ACE_Logging_Strategy to something 00067 * other than the singleton reactor you'll need to get a pointer to 00068 * the @c ACE_Logging_Strategy instance and do this 00069 * 00070 * ACE_Reactor my_reactor; 00071 * ACE_Logging_Strategy *logging_strategy = ...... // Get instance. 00072 * 00073 * logging_strategy->reactor (&my_reactor); 00074 * 00075 * and then logging_strategy will use your reactor. If you're 00076 * dynamically linking the @c ACE_Logging_Strategy then you can use 00077 * the @c ACE_Dynamic_Service template to get a pointer to the 00078 * @c ACE_Logging_Strategy. 00079 */ 00080 class ACE_Export ACE_Logging_Strategy : public ACE_Service_Object 00081 { 00082 public: 00083 /// Constructor. 00084 ACE_Logging_Strategy (void); 00085 00086 /// Destructor. 00087 ~ACE_Logging_Strategy (void); 00088 00089 /// Dynamic linking initialization hook. 00090 virtual int init (int argc, ACE_TCHAR *argv[]); 00091 00092 /// Dynamic linking termination hook. 00093 virtual int fini (void); 00094 00095 /** 00096 * Timeout handler which tests logfile size. If the current logfile 00097 * size exceeds @c max_size_, the current logfile is closed, saved to 00098 * logfile.old, and a new logfile is reopened. 00099 */ 00100 virtual int handle_timeout (const ACE_Time_Value& tv, 00101 const void* arg); 00102 00103 /** 00104 * This function helps to cancel timer events for this logging strategy 00105 * in reactor during shutdown. 00106 */ 00107 virtual int handle_close (ACE_HANDLE, 00108 ACE_Reactor_Mask); 00109 00110 /** 00111 * Reactor accessors. If reactor changes then we need remove this 00112 * event handler from previous reactor and scheduler for timer events 00113 * in a new one. 00114 */ 00115 virtual void reactor (ACE_Reactor *r); 00116 00117 virtual ACE_Reactor * reactor (void) const; 00118 00119 /** 00120 * Parse arguments provided in svc.conf file. 00121 * @arg '-f' Pass in the flags (such as OSTREAM, STDERR, LOGGER, VERBOSE, 00122 * SILENT, VERBOSE_LITE) used to control logging. 00123 * @arg '-i' The interval (in seconds) at which the logfile size is sampled 00124 * (default is 0, i.e., do not sample by default). 00125 * @arg '-k' Set the logging key. 00126 * @arg '-m' Maximum logfile size in Kbytes. 00127 * @arg '-n' Set the program name for the %n format specifier. 00128 * @arg '-N' The maximum number of logfiles that we want created. 00129 * @arg '-o' Specifies that we want the no standard logfiles ordering 00130 * (fastest processing in handle_timeout()). Default is not to 00131 * order logfiles. 00132 * @arg '-p' Pass in the process-wide priorities to either enable (e.g., 00133 * DEBUG, INFO, WARNING, NOTICE, ERROR, CRITICAL, ALERT, 00134 * EMERGENCY) or to disable (e.g., ~DEBUG, ~INFO, ~WARNING, 00135 * ~NOTICE, ~ERROR, ~CRITICAL, ~ALERT, ~EMERGENCY). 00136 * @arg '-s' Ensure that the OSTREAM flag is set and log to the @a filename. 00137 * @arg '-t' Pass in the thread-wide priorities to either enable (e.g., 00138 * DEBUG, INFO, WARNING, NOTICE, ERROR, CRITICAL, ALERT, 00139 * EMERGENCY) or to disable (e.g., ~DEBUG, ~INFO, ~WARNING, 00140 * ~NOTICE, ~ERROR, ~CRITICAL, ~ALERT, ~EMERGENCY). 00141 * @arg '-w' Cause the logfile to be wiped out, both on startup and on 00142 * reconfiguration. 00143 */ 00144 int parse_args (int argc, ACE_TCHAR *argv[]); 00145 00146 void log_msg (ACE_Log_Msg *log_msg); 00147 00148 protected: 00149 /// Tokenize to set all the flags 00150 void tokenize (ACE_TCHAR *flag_string); 00151 00152 /// Tokenize to set priorities (either process or thread one). 00153 void priorities (ACE_TCHAR *priority_string, 00154 ACE_Log_Msg::MASK_TYPE mask); 00155 00156 /// Current thread's priority mask set by @c priorities 00157 u_long thread_priority_mask_; 00158 00159 /// Process-wide priority mask set by @c priorities 00160 u_long process_priority_mask_; 00161 00162 /// Flags we keep track of. 00163 u_long flags_; 00164 00165 /// File name we're logging to. 00166 ACE_TCHAR *filename_; 00167 00168 /// Logger key for distributed logging. 00169 ACE_TCHAR *logger_key_; 00170 00171 /// Program name to be used for %n format specifier. 00172 ACE_TCHAR *program_name_; 00173 00174 /// If true then wipeout the logfile, otherwise append to it. 00175 /// Default value is false. 00176 bool wipeout_logfile_; 00177 00178 /// If true we have a maximum number of log files we can write. 00179 /// Default value is false, i.e., no maximum number. 00180 bool fixed_number_; 00181 00182 /// If true we order the files as we rotate them. Default value 00183 /// is false, i.e., we do not rotate files by default. 00184 bool order_files_; 00185 00186 /// This tells us in what file we last wrote. It will be increased 00187 /// to enable multiple log files 00188 int count_; 00189 00190 /// Tells us what is the maximum log file to write. We will write 00191 /// @c max_file_number_ + 1 files (includes the current log file). 00192 /// Default value is 1, i.e., 2 files by default. 00193 int max_file_number_; 00194 00195 /// If non-zero, sampling interval (in secs) at which maximum logfile 00196 /// size is checked, otherwise logfile size can grow indefinitely. 00197 /// Default value is 0. 00198 u_long interval_; 00199 00200 /// Maximum logfile size (in KB). Default value is 00201 /// ACE_DEFAULT_MAX_LOGFILE_SIZE. 00202 u_long max_size_; 00203 00204 /// ACE_Log_Msg instance to work with 00205 ACE_Log_Msg *log_msg_; 00206 }; 00207 00208 ACE_END_VERSIONED_NAMESPACE_DECL 00209 00210 ACE_STATIC_SVC_DECLARE_EXPORT(ACE, ACE_Logging_Strategy) 00211 00212 ACE_FACTORY_DECLARE (ACE, ACE_Logging_Strategy) 00213 00214 #endif /* ACE_LOGGING_STRATEGY_H */