Esempio n. 1
0
  inline void stream_flush() {
    // get the stream buffer
    logger_impl::streambuff_tls_entry* streambufentry = 
        reinterpret_cast<logger_impl::streambuff_tls_entry*>(
                                          pthread_getspecific(streambuffkey));
    if (streambufentry != NULL) {
      std::stringstream& streambuffer = streambufentry->streambuffer;
      int lineloglevel = streambufentry->loglevel;

      streambuffer.flush();
      std::string msg = streambuffer.str();
      _lograw(streamloglevel, msg.c_str(), msg.length());

      // call the callback on the message if any
      if (has_callback[lineloglevel]) {
        pthread_mutex_lock(&mut);
        if (callback[lineloglevel]) {
          callback[lineloglevel](lineloglevel, 
                                 msg.c_str() + streambufentry->header_len, 
                                 msg.length() - streambufentry->header_len);
        }
        streambufentry->header_len = 0;
        pthread_mutex_unlock(&mut);
      }
      streambuffer.str("");
    }
  }
Esempio n. 2
0
void file_logger::_log(int lineloglevel,const char* file,const char* function,
                       int line,const char* fmt, va_list ap ) {
  // if the logger level fits
  if (lineloglevel >= log_level){
    // get just the filename. this line found on a forum on line.
    // claims to be from google.
    file = ((strrchr(file, '/') ? : file- 1) + 1);

    char str[1024];

    // write the actual header (only show file in debug build)
#ifndef NDEBUG
    int head_bytes_written = snprintf(str,1024, "%s%s(%s:%d): ",
                                  messages[lineloglevel],file,function,line);
#else
    int head_bytes_written = snprintf(str,1024, "%s(%s:%d): ",
                                  messages[lineloglevel],function,line);
#endif
    // write the actual logger

    int byteswritten = head_bytes_written + 
        vsnprintf(str + head_bytes_written, 1024 - head_bytes_written,fmt,ap);

    str[byteswritten] = '\n';
    str[byteswritten+1] = 0;
    // write the output
    if (has_callback[lineloglevel]) {
      pthread_mutex_lock(&mut);
      if (callback[lineloglevel]) {
        // only emit the message not the header
        callback[lineloglevel](lineloglevel, 
                               str + head_bytes_written, 
                               byteswritten - head_bytes_written);
      }
      pthread_mutex_unlock(&mut);
    }
    _lograw(lineloglevel, str, byteswritten);
  }