Changeset 126:5017012fdcc2 in rrlib_logging


Ignore:
Timestamp:
09.07.2014 14:31:40 (6 years ago)
Author:
Michael Arndt <m_arndt@…>
Branch:
default
Phase:
public
Message:

Optimization: just check once if the file descriptor is a TTY. This eliminates (at least on a recent Linux) two ioctl() per (colored) logging output.

Location:
messages
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • messages/tFormattingBuffer.cpp

    r110 r126  
    8585  collect_multi_line_pad_width(false), 
    8686  pad_before_next_character(false) 
    87 {} 
     87{ 
     88  this->is_a_tty = isatty(util::GetFileDescriptor(this->sink)); 
     89} 
    8890 
    8991tFormattingBuffer::tFormattingBuffer(const tFormattingBuffer &other) : 
     
    9395  collect_multi_line_pad_width(other.collect_multi_line_pad_width), 
    9496  pad_before_next_character(other.pad_before_next_character) 
    95 {} 
     97{ 
     98  this->is_a_tty = isatty(util::GetFileDescriptor(this->sink)); 
     99} 
    96100 
    97101//---------------------------------------------------------------------- 
     
    107111    this->collect_multi_line_pad_width = other.collect_multi_line_pad_width; 
    108112    this->pad_before_next_character = other.pad_before_next_character; 
     113    this->is_a_tty = isatty(util::GetFileDescriptor(this->sink)); 
    109114  } 
    110115  return *this; 
     
    119124  snprintf(control_sequence, sizeof(control_sequence), "\033[;%u;3%um", effect, color); 
    120125  const size_t length = strlen(control_sequence); 
    121  
    122   int file_descriptor = util::GetFileDescriptor(this->sink); 
    123   if (file_descriptor > 0 && isatty(file_descriptor)) 
    124   { 
    125     for (size_t i = 0; i < length; ++i) 
    126     { 
    127       this->sink->sputc(control_sequence[i]); 
    128     } 
     126  if (is_a_tty) 
     127  { 
     128    this->sink->sputn(control_sequence, length); 
    129129  } 
    130130} 
     
    137137  const char *control_sequence = "\033[;0m"; 
    138138  const size_t length = strlen(control_sequence); 
    139   int file_descriptor = util::GetFileDescriptor(this->sink); 
    140   if (file_descriptor > 0 && isatty(file_descriptor)) 
    141   { 
    142     for (size_t i = 0; i < length; ++i) 
    143     { 
    144       this->sink->sputc(control_sequence[i]); 
    145     } 
     139  if (is_a_tty) 
     140  { 
     141    this->sink->sputn(control_sequence, length); 
    146142  } 
    147143} 
  • messages/tFormattingBuffer.h

    r110 r126  
    146146 
    147147  std::streambuf *sink; 
     148  bool is_a_tty; 
    148149 
    149150  bool ends_with_newline; 
Note: See TracChangeset for help on using the changeset viewer.