• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

LogAccess.h

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 /***************************************************************************
00025  Logging - LogAccess.h
00026 
00027 
00028  ***************************************************************************/
00029 #ifndef LOG_ACCESS_H
00030 #define LOG_ACCESS_H
00031 #include "libts.h"
00032 #include "LogField.h"
00033 
00034 /*-------------------------------------------------------------------------
00035   LogAccess
00036 
00037   This class defines the logging system interface for extracting
00038   information required to process a log entry.  This accessor is
00039   implemented as an abstract base class with virtual functions for
00040   accessing the data based on the derived class.
00041 
00042   NOTE that some accessors are declared non-virtual, meaning that they
00043   provide an implementation in this base class that is NOT TO BE
00044   OVERRIDDEN.  This is used for fields that are already part of the
00045   LogEntryHeader, and thus do not need to be marshalled.  So, these
00046   functions do nothing and return 0.
00047 
00048   Each function has the ability to marshal its data into a buffer that is
00049   provided, and return the number of bytes that were marshalled.  In the
00050   absence of a marshalling buffer, the routines will simply return the
00051   number of bytes that would be needed to marshal.  This allows for the
00052   same functions to be used for both buffer length computations and data
00053   movement.
00054 
00055   Logging deals with values of just two possible data types: integers
00056   (including enum) and strings.   Because the integers are multi-byte
00057   values that might need special alignment needs when being marshalled,
00058   this base class provides a static member function called marshal_int()
00059   that handles this (including checking for a NULL buffer).  The template
00060   for implementing integer and enum marshalling routines is:
00061 
00062       int marshal_some_int_value (char *buf)
00063       {
00064           if (buf) {
00065               int64_t val = what_the_value_should_be;
00066               marshal_int (buf, val);
00067           }
00068           return INK_MIN_ALIGN;
00069       }
00070 
00071   String values don't need byte swapping, but we do want to ensure things
00072   like trailing NULLS and padding.  The best way to do this is to provide a
00073   marshalling routine that takes a source buffer, a string, and its length,
00074   and makes sure the string is copied into the source buffer with a
00075   trailing NULL.  We've also provided our own strlen() function within the
00076   class to adjust for trailing NULL as well.  So, here is how it would look
00077   to actually use these functions for marshalling a string value:
00078 
00079       int marshal_some_string_value (char *buf)
00080       {
00081           char *str = compute_or_locate_string_value ();
00082           int len = LogAccess::strlen (str);
00083           if (buf) {
00084               marshal_str (buf, str, len);
00085           }
00086           return len;
00087       }
00088 
00089   -------------------------------------------------------------------------*/
00090 
00091 // DEFAULT_STR_LEN MUST be less than INK_MIN_ALIGN
00092 #define DEFAULT_STR     "-"
00093 #define DEFAULT_STR_LEN 1
00094 
00095 #define DEFAULT_INT_FIELD {\
00096     if (buf) { \
00097       int64_t i = 0; \
00098       marshal_int (buf, i); \
00099     } \
00100     return INK_MIN_ALIGN; \
00101 }
00102 
00103 #define DEFAULT_STR_FIELD {\
00104     char * str = NULL; \
00105     int len = INK_MIN_ALIGN; \
00106     if (buf) { \
00107       marshal_str (buf, str, len); \
00108     } \
00109     return len; \
00110 }
00111 
00112 #define DEFAULT_IP_FIELD {\
00113     int len = sizeof(LogFieldIp); \
00114     if (buf) { \
00115       len = marshal_ip (buf, NULL); \
00116     } \
00117     return len; \
00118 }
00119 
00120 // should be at least 22 bytes to always accomodate a converted
00121 // MgmtInt, MgmtIntCounter or MgmtFloat. 22 bytes is enough for 64 bit
00122 // ints + sign + eos, and enough for %e floating point representation
00123 // + eos
00124 //
00125 #define MARSHAL_RECORD_LENGTH 32
00126 
00127 enum LogEntryType
00128 {
00129   LOG_ENTRY_HTTP = 0,
00130   LOG_ENTRY_ICP,
00131   N_LOG_ENTRY_TYPES
00132 };
00133 
00134 enum LogFinishCodeType
00135 {
00136   LOG_FINISH_FIN = 0,
00137   LOG_FINISH_INTR,
00138   LOG_FINISH_TIMEOUT,
00139   N_LOG_FINISH_CODE_TYPES
00140 };
00141 
00142 enum LogCacheWriteCodeType
00143 {
00144   LOG_CACHE_WRITE_NONE = 0,
00145   LOG_CACHE_WRITE_LOCK_MISSED,
00146   LOG_CACHE_WRITE_LOCK_ABORTED,
00147   LOG_CACHE_WRITE_ERROR,
00148   LOG_CACHE_WRITE_COMPLETE,
00149   N_LOG_CACHE_WRITE_TYPES
00150 };
00151 
00152 
00153 class LogAccess
00154 {                               // Abstract Base Class
00155 public:
00156   inkcoreapi LogAccess()
00157     : initialized(false)
00158   { }
00159 
00160   inkcoreapi virtual ~LogAccess()
00161   { }
00162 
00163   inkcoreapi virtual void init();
00164 
00165   virtual LogEntryType entry_type() = 0;
00166 
00167   //
00168   // client -> proxy fields
00169   //
00170   inkcoreapi virtual int marshal_client_host_ip(char *);        // STR
00171   inkcoreapi virtual int marshal_client_host_port(char *);      // INT
00172   inkcoreapi virtual int marshal_client_auth_user_name(char *); // STR
00173   int marshal_client_req_timestamp_sec(char *); // INT
00174 
00175   inkcoreapi virtual int marshal_client_req_text(char *);       // STR
00176   inkcoreapi virtual int marshal_client_req_http_method(char *);        // STR
00177   inkcoreapi virtual int marshal_client_req_url(char *);        // STR
00178   inkcoreapi virtual int marshal_client_req_url_canon(char *);  // STR
00179   inkcoreapi virtual int marshal_client_req_unmapped_url_canon(char *); // STR
00180   inkcoreapi virtual int marshal_client_req_unmapped_url_path(char *);  // STR
00181   inkcoreapi virtual int marshal_client_req_unmapped_url_host(char *);  // STR
00182   inkcoreapi virtual int marshal_client_req_url_path(char *);   // STR
00183   inkcoreapi virtual int marshal_client_req_url_scheme(char *); // STR
00184   inkcoreapi virtual int marshal_client_req_http_version(char *);       // INT
00185   inkcoreapi virtual int marshal_client_req_header_len(char *); // INT
00186   inkcoreapi virtual int marshal_client_req_body_len(char *);   // INT
00187   inkcoreapi virtual int marshal_client_finish_status_code(char *);     // INT
00188 
00189   //
00190   // proxy -> client fields
00191   //
00192   inkcoreapi virtual int marshal_proxy_resp_content_type(char *);       // STR
00193   inkcoreapi virtual int marshal_proxy_resp_squid_len(char *);  // INT
00194   inkcoreapi virtual int marshal_proxy_resp_content_len(char *);        // INT
00195   inkcoreapi virtual int marshal_proxy_resp_status_code(char *);        // INT
00196   inkcoreapi virtual int marshal_proxy_resp_header_len(char *); // INT
00197   inkcoreapi virtual int marshal_proxy_finish_status_code(char *);      // INT
00198   inkcoreapi virtual int marshal_cache_result_code(char *);     // INT
00199 
00200   //
00201   // proxy -> server fields
00202   //
00203   inkcoreapi virtual int marshal_proxy_req_header_len(char *);  // INT
00204   inkcoreapi virtual int marshal_proxy_req_body_len(char *);    // INT
00205   inkcoreapi virtual int marshal_proxy_req_server_name(char *); // STR
00206   inkcoreapi virtual int marshal_proxy_req_server_ip(char *);   // INT
00207   inkcoreapi virtual int marshal_proxy_hierarchy_route(char *); // INT
00208   inkcoreapi virtual int marshal_proxy_host_name(char *);       // STR
00209   inkcoreapi virtual int marshal_proxy_host_ip(char *); // STR
00210 
00211   //
00212   // server -> proxy fields
00213   //
00214   inkcoreapi virtual int marshal_server_host_ip(char *);        // INT
00215   inkcoreapi virtual int marshal_server_host_name(char *);      // STR
00216   inkcoreapi virtual int marshal_server_resp_status_code(char *);       // INT
00217   inkcoreapi virtual int marshal_server_resp_content_len(char *);       // INT
00218   inkcoreapi virtual int marshal_server_resp_header_len(char *);        // INT
00219   inkcoreapi virtual int marshal_server_resp_http_version(char *);      // INT
00220 
00221   //
00222   // cache -> client fields
00223   //
00224   inkcoreapi virtual int marshal_cache_resp_status_code(char *);  // INT
00225   inkcoreapi virtual int marshal_cache_resp_content_len(char *);  // INT
00226   inkcoreapi virtual int marshal_cache_resp_header_len(char *);   // INT
00227   inkcoreapi virtual int marshal_cache_resp_http_version(char *); // INT
00228 
00229 
00230   inkcoreapi virtual void set_client_req_url(char *, int) {};        // STR
00231   inkcoreapi virtual void set_client_req_url_canon(char *, int) {};  // STR
00232   inkcoreapi virtual void set_client_req_unmapped_url_canon(char *, int) {}; // STR
00233   inkcoreapi virtual void set_client_req_unmapped_url_path(char *, int) {};  // STR
00234   inkcoreapi virtual void set_client_req_unmapped_url_host(char *, int) {};  // STR
00235   inkcoreapi virtual void set_client_req_url_path(char *, int) {};   // STR
00236 
00237   //
00238   // congestion control -- client_retry_after_time
00239   //
00240   inkcoreapi virtual int marshal_client_retry_after_time(char *);       // INT
00241 
00242   //
00243   // cache write fields
00244   //
00245   inkcoreapi virtual int marshal_cache_write_code(char *);      // INT
00246   inkcoreapi virtual int marshal_cache_write_transform_code(char *);    // INT
00247 
00248   // other fields
00249   //
00250   inkcoreapi virtual int marshal_transfer_time_ms(char *);      // INT
00251   inkcoreapi virtual int marshal_transfer_time_s(char *);       // INT
00252   inkcoreapi virtual int marshal_file_size(char *);     // INT
00253   inkcoreapi virtual int marshal_plugin_identity_id(char *); // INT
00254   inkcoreapi virtual int marshal_plugin_identity_tag(char *); // STR
00255   int marshal_entry_type(char *);       // INT
00256 
00257 
00258   // named fields from within a http header
00259   //
00260   inkcoreapi virtual int marshal_http_header_field(LogField::Container container, char *field, char *buf);
00261   inkcoreapi virtual int marshal_http_header_field_escapify(LogField::Container container, char *field, char *buf);
00262 
00263   //
00264   // named records.config int variables
00265   //
00266   int marshal_config_int_var(char *config_var, char *buf);
00267 
00268   //
00269   // named records.config string variables
00270   //
00271   int marshal_config_str_var(char *config_var, char *buf);
00272 
00273   //
00274   // generic record access
00275   //
00276   int marshal_record(char *record, char *buf);
00277 
00278   //
00279   // unmarshalling routines
00280   //
00281   // They used to return a string; now they unmarshal directly into the
00282   // destination buffer supplied.
00283   //
00284   static int64_t unmarshal_int(char **buf);
00285   static int unmarshal_itoa(int64_t val, char *dest, int field_width = 0, char leading_char = ' ');
00286   static int unmarshal_itox(int64_t val, char *dest, int field_width = 0, char leading_char = ' ');
00287   static int unmarshal_int_to_str(char **buf, char *dest, int len);
00288   static int unmarshal_int_to_str_hex(char **buf, char *dest, int len);
00289   static int unmarshal_str(char **buf, char *dest, int len, LogSlice *slice = NULL);
00290   static int unmarshal_ttmsf(char **buf, char *dest, int len);
00291   static int unmarshal_http_version(char **buf, char *dest, int len);
00292   static int unmarshal_http_text(char **buf, char *dest, int len, LogSlice *slice = NULL);
00293   static int unmarshal_http_status(char **buf, char *dest, int len);
00294   static int unmarshal_ip(char** buf, IpEndpoint* dest);
00295   static int unmarshal_ip_to_str(char **buf, char *dest, int len);
00296   static int unmarshal_ip_to_hex(char** buf, char* dest, int len);
00297   static int unmarshal_hierarchy(char **buf, char *dest, int len, Ptr<LogFieldAliasMap> map);
00298   static int unmarshal_finish_status(char **buf, char *dest, int len, Ptr<LogFieldAliasMap> map);
00299   static int unmarshal_cache_code(char **buf, char *dest, int len, Ptr<LogFieldAliasMap> map);
00300   static int unmarshal_entry_type(char **buf, char *dest, int len, Ptr<LogFieldAliasMap> map);
00301   static int unmarshal_cache_write_code(char **buf, char *dest, int len, Ptr<LogFieldAliasMap> map);
00302   static int unmarshal_client_protocol_stack(char **buf, char *dest, int len, Ptr<LogFieldAliasMap> map);
00303 
00304   static int unmarshal_with_map(int64_t code, char *dest, int len, Ptr<LogFieldAliasMap> map, const char *msg = 0);
00305 
00306   static int unmarshal_record(char **buf, char *dest, int len);
00307 
00308   //
00309   // our own strlen function that pads strings to even int64_t boundaries
00310   // so that there are no alignment problems with the int values.
00311   //
00312   static int round_strlen(int len);
00313   static int strlen(char const* str);
00314 
00315 public:
00316   inkcoreapi static void marshal_int(char *dest, int64_t source);
00317   inkcoreapi static void marshal_str(char *dest, const char *source, int padded_len);
00318   inkcoreapi static void marshal_mem(char *dest, const char *source, int actual_len, int padded_len);
00319   inkcoreapi static int marshal_ip(char *dest, sockaddr const* ip);
00320 
00321   bool initialized;
00322 
00323 private:
00324   // -- member functions that are not allowed --
00325   LogAccess(const LogAccess & rhs);     // no copies
00326   LogAccess & operator=(LogAccess & rhs);       // or assignment
00327 };
00328 
00329 inline int
00330 LogAccess::round_strlen(int len)
00331 {
00332   return INK_ALIGN_DEFAULT(len);
00333 }
00334 
00335 /*-------------------------------------------------------------------------
00336   LogAccess::strlen
00337 
00338   Take trailing null and alignment padding into account.  This makes sure
00339   that strings in the LogBuffer are laid out properly.
00340   -------------------------------------------------------------------------*/
00341 
00342 inline int
00343 LogAccess::strlen(char const* str)
00344 {
00345   if (str == NULL || str[0] == 0) {
00346     return round_strlen(sizeof(DEFAULT_STR));
00347   } else {
00348     return (int) (round_strlen(((int)::strlen(str) + 1)));      // actual bytes for string
00349   }
00350 }
00351 
00352 inline void
00353 LogAccess::marshal_int(char *dest, int64_t source)
00354 {
00355   // TODO: This used to do htonl on the source. TS-1156
00356   *((int64_t *)dest) = source;
00357 }
00358 
00359 /*-------------------------------------------------------------------------
00360   resolve_logfield_string
00361 
00362   This external function takes a format string and a LogAccess context and
00363   resolves any known fields to return a new, resolved string.
00364   -------------------------------------------------------------------------*/
00365 
00366 char *resolve_logfield_string(LogAccess * context, const char *format_str);
00367 
00368 #endif

Generated by  doxygen 1.7.1