Changeset 49:075058b4f654 in rrlib_logging


Ignore:
Timestamp:
07.01.2011 00:59:58 (10 years ago)
Author:
Tobias Föhst <foehst@…>
Branch:
default
Phase:
public
Convert:
svn:2179b62a-598e-4714-ba03-d8986cbf34aa/trunk@50
Message:

Fixed bug that truncated RRLIB_LOG_MESSAGE output

File:
1 edited

Legend:

Unmodified
Added
Removed
  • tLogDomain.h

    r38 r49  
    100100  friend class tLogDomainRegistry; 
    101101 
    102   tLogDomain *parent; 
    103   std::vector<tLogDomain *> children; 
    104  
    105   tLogDomainConfigurationSharedPointer configuration; 
    106  
    107   mutable tLogStreamBuffer stream_buffer; 
    108   mutable std::ofstream file_stream; 
    109  
    110   std::tr1::shared_ptr<boost::recursive_mutex> mutex; 
    111  
    112   /*! The ctor of a top level domain 
    113    * 
    114    * This ctor is to be called by the registry that creates the top level 
    115    * domain. 
    116    * 
    117    * \param configuration   The configuration for the new domain 
    118    */ 
    119   tLogDomain(tLogDomainConfigurationSharedPointer configuration); 
    120  
    121   /*! The ctor for a new sub domain 
    122    * 
    123    * This ctor is to be called by the registry to create a new subdomain 
    124    * with a given configuration 
    125    * 
    126    * \param configuration   The configuration for the new domain 
    127    * \param parent          The parent domain 
    128    */ 
    129   tLogDomain(tLogDomainConfigurationSharedPointer configuration, tLogDomain &parent); 
    130  
    131   /*! 
    132    * \returns Shared Pointer to output mutex that is shared by all logging domains 
    133    */ 
    134   static std::tr1::shared_ptr<boost::recursive_mutex> GetMutex() 
    135   { 
    136     static std::tr1::shared_ptr<boost::recursive_mutex> mutex(new boost::recursive_mutex()); 
    137     return mutex; 
    138   } 
    139  
    140   /*! Recursively configure the subtree that begins in this domain 
    141    * 
    142    * If the domain is configured by its parent, the configuration is 
    143    * copied and propagated to this domain's children 
    144    */ 
    145   void ConfigureSubTree(); 
    146  
    147   /*! Open the file stream for file output 
    148    * 
    149    * This method creates a new file which name is build using a prefix 
    150    * and the full qualified domain name. 
    151    * If the file already exists, it will be truncated. 
    152    * 
    153    * \returns Whether the file stream could be opened or not 
    154    */ 
    155   const bool OpenFileOutputStream() const; 
    156  
    157   /*! Setup the output stream to be used in this domain 
    158    * 
    159    * A domain can stream its input to stdout, stderr, an own file and/or its parent's file. 
    160    * 
    161    *\param mask   The bitmask that selects the output streams to use 
    162    */ 
    163   void SetupOutputStream(int mask) const; 
    164  
    165   /*! Get the current time as string for internal use in messages 
    166    * 
    167    * This method formats the current time as string that can be used in 
    168    * messages. 
    169    * 
    170    * \returns The current time as string 
    171    */ 
    172   const std::string GetTimeString() const; 
    173  
    174   /*! Get the domain's name as string for internal use in messages 
    175    * 
    176    * This method formats the name as string that can be used in 
    177    * messages. This string is padded with spaces to the length of the 
    178    * longest domain name 
    179    * 
    180    * \returns The padded name as string 
    181    */ 
    182   const std::string GetNameString() const; 
    183  
    184   /*! Get the given message level as string for internal use in messages 
    185    * 
    186    * This method formats the given level as string that can be used in 
    187    * messages. 
    188    * 
    189    * \param level   The level that should be represented as string 
    190    * 
    191    * \returns The given level as padded string 
    192    */ 
    193   const std::string GetLevelString(tLogLevel level) const; 
    194  
    195   /*! Get the given location as string for internal use in messages 
    196    * 
    197    * This method formats given location consisting of a file name and a 
    198    * line number as string that can be used in messages. 
    199    * 
    200    * \param file   The file name (e.g. from __FILE__) 
    201    * \param line   The line number (e.g. from __LINE__) 
    202    * 
    203    * \returns The given location as string 
    204    */ 
    205   const std::string GetLocationString(const char *file, unsigned int line) const; 
    206  
    207   /*! Setup the underlying streambuffer to produce colored output 
    208    * 
    209    * This method sets up the underlying streambuffer for colored 
    210    * output according to the given level. 
    211    * 
    212    * \param level   The according log level 
    213    */ 
    214   void SetupOutputStreamColor(tLogLevel level) const; 
    215  
    216 //---------------------------------------------------------------------- 
    217 // Public methods 
     102//---------------------------------------------------------------------- 
     103// Public methods and typedefs 
    218104//---------------------------------------------------------------------- 
    219105public: 
     
    324210    } 
    325211    this->SetupOutputStream(this->configuration->sink_mask); 
    326  
    327212    if (level == eLL_USER) 
    328213    { 
    329214      return stream_proxy; 
    330215    } 
    331  
    332216    if (this->GetPrintTime()) 
    333217    { 
     
    335219    } 
    336220    this->SetupOutputStreamColor(level); 
    337  
    338221#ifndef _RRLIB_LOGGING_LESS_OUTPUT_ 
    339222    if (this->GetPrintName()) 
     
    400283      return; 
    401284    } 
    402     char formatted_string_buffer[1024]; 
    403285 
    404286    va_list printf_args; 
    405287    va_start(printf_args, fmt); 
    406     vsnprintf(formatted_string_buffer, sizeof(formatted_string_buffer), fmt, printf_args); 
     288    this->VPrintMessage(description, function, file, line, level, fmt, printf_args); 
    407289    va_end(printf_args); 
    408  
    409     this->GetMessageStream(description, function, file, line, level) << formatted_string_buffer; 
    410290  } 
    411291 
     
    417297      return; 
    418298    } 
    419     char formatted_string_buffer[1024]; 
    420299 
    421300    va_list printf_args; 
    422301    va_start(printf_args, fmt); 
    423     vsnprintf(formatted_string_buffer, sizeof(formatted_string_buffer), fmt, printf_args); 
     302    this->VPrintMessage(description, function, file, line, level, fmt, printf_args); 
    424303    va_end(printf_args); 
    425  
    426     this->GetMessageStream(description, function, file, line, level) << formatted_string_buffer; 
     304  } 
     305 
     306//---------------------------------------------------------------------- 
     307// Private fields and methods 
     308//---------------------------------------------------------------------- 
     309private: 
     310 
     311  tLogDomain *parent; 
     312  std::vector<tLogDomain *> children; 
     313 
     314  tLogDomainConfigurationSharedPointer configuration; 
     315 
     316  mutable tLogStreamBuffer stream_buffer; 
     317  mutable std::ofstream file_stream; 
     318 
     319  std::tr1::shared_ptr<boost::recursive_mutex> mutex; 
     320 
     321  /*! The ctor of a top level domain 
     322   * 
     323   * This ctor is to be called by the registry that creates the top level 
     324   * domain. 
     325   * 
     326   * \param configuration   The configuration for the new domain 
     327   */ 
     328  tLogDomain(tLogDomainConfigurationSharedPointer configuration); 
     329 
     330  /*! The ctor for a new sub domain 
     331   * 
     332   * This ctor is to be called by the registry to create a new subdomain 
     333   * with a given configuration 
     334   * 
     335   * \param configuration   The configuration for the new domain 
     336   * \param parent          The parent domain 
     337   */ 
     338  tLogDomain(tLogDomainConfigurationSharedPointer configuration, tLogDomain &parent); 
     339 
     340  /*! 
     341   * \returns Shared Pointer to output mutex that is shared by all logging domains 
     342   */ 
     343  static std::tr1::shared_ptr<boost::recursive_mutex> GetMutex() 
     344  { 
     345    static std::tr1::shared_ptr<boost::recursive_mutex> mutex(new boost::recursive_mutex()); 
     346    return mutex; 
     347  } 
     348 
     349  /*! Recursively configure the subtree that begins in this domain 
     350   * 
     351   * If the domain is configured by its parent, the configuration is 
     352   * copied and propagated to this domain's children 
     353   */ 
     354  void ConfigureSubTree(); 
     355 
     356  /*! Open the file stream for file output 
     357   * 
     358   * This method creates a new file which name is build using a prefix 
     359   * and the full qualified domain name. 
     360   * If the file already exists, it will be truncated. 
     361   * 
     362   * \returns Whether the file stream could be opened or not 
     363   */ 
     364  const bool OpenFileOutputStream() const; 
     365 
     366  /*! Setup the output stream to be used in this domain 
     367   * 
     368   * A domain can stream its input to stdout, stderr, an own file and/or its parent's file. 
     369   * 
     370   *\param mask   The bitmask that selects the output streams to use 
     371   */ 
     372  void SetupOutputStream(int mask) const; 
     373 
     374  /*! Get the current time as string for internal use in messages 
     375   * 
     376   * This method formats the current time as string that can be used in 
     377   * messages. 
     378   * 
     379   * \returns The current time as string 
     380   */ 
     381  const std::string GetTimeString() const; 
     382 
     383  /*! Get the domain's name as string for internal use in messages 
     384   * 
     385   * This method formats the name as string that can be used in 
     386   * messages. This string is padded with spaces to the length of the 
     387   * longest domain name 
     388   * 
     389   * \returns The padded name as string 
     390   */ 
     391  const std::string GetNameString() const; 
     392 
     393  /*! Get the given message level as string for internal use in messages 
     394   * 
     395   * This method formats the given level as string that can be used in 
     396   * messages. 
     397   * 
     398   * \param level   The level that should be represented as string 
     399   * 
     400   * \returns The given level as padded string 
     401   */ 
     402  const std::string GetLevelString(tLogLevel level) const; 
     403 
     404  /*! Get the given location as string for internal use in messages 
     405   * 
     406   * This method formats given location consisting of a file name and a 
     407   * line number as string that can be used in messages. 
     408   * 
     409   * \param file   The file name (e.g. from __FILE__) 
     410   * \param line   The line number (e.g. from __LINE__) 
     411   * 
     412   * \returns The given location as string 
     413   */ 
     414  const std::string GetLocationString(const char *file, unsigned int line) const; 
     415 
     416  /*! Setup the underlying streambuffer to produce colored output 
     417   * 
     418   * This method sets up the underlying streambuffer for colored 
     419   * output according to the given level. 
     420   * 
     421   * \param level   The according log level 
     422   */ 
     423  void SetupOutputStreamColor(tLogLevel level) const; 
     424 
     425  template <typename TDescription> 
     426  inline void VPrintMessage(const TDescription &description, const char *function, const char *file, int line, tLogLevel level, const char *fmt,  va_list printf_args) const 
     427  { 
     428    char temp; 
     429    int needed_buffer_size = vsnprintf(&temp, 1, fmt, printf_args); 
     430    if (needed_buffer_size > 0) 
     431    { 
     432      char formatted_string_buffer[needed_buffer_size + 1]; 
     433      vsnprintf(formatted_string_buffer, sizeof(formatted_string_buffer), fmt, printf_args); 
     434      this->GetMessageStream(description, function, file, line, level) << formatted_string_buffer; 
     435    } 
    427436  } 
    428437 
Note: See TracChangeset for help on using the changeset viewer.