<commit_before>/**
 * @file prefixedoutstream.hpp
 * @author <NAME>
 * @author <NAME>
 *
 * Implementation of templated PrefixedOutStream member functions.
 */
#ifndef __MLPACK_CORE_UTIL_PREFIXEDOUTSTREAM_IMPL_HPP
#define __MLPACK_CORE_UTIL_PREFIXEDOUTSTREAM_IMPL_HPP

// Just in case it hasn't been included.
#include "prefixedoutstream.hpp"
#include "backtrace.hpp"
#include <iostream>

namespace mlpack {
namespace util {

template<typename T>
PrefixedOutStream& PrefixedOutStream::operator<<(const T& s)
{
  BaseLogic<T>(s);
  return *this;
}

template<typename T>
void PrefixedOutStream::BaseLogic(const T& val)
{
  // We will use this to track whether or not we need to terminate at the end of
  // this call (only for streams which terminate after a newline).
  bool newlined = false;
  std::string line;

  // If we need to, output the prefix.
  PrefixIfNeeded();

  std::ostringstream convert;
  convert << val;

  if (convert.fail())
  {
    PrefixIfNeeded();
    if (!ignoreInput)
    {
      destination << "Failed lexical_cast<std::string>(T) for output; output"
          " not shown." << std::endl;
      newlined = true;
    }
  }
  else
  {
    line = convert.str();

    // If the length of the casted thing was 0, it may have been a stream
    // manipulator, so send it directly to the stream and don't ask questions.
    if (line.length() == 0)
    {
      // The prefix cannot be necessary at this point.
      if (!ignoreInput) // Only if the user wants it.
        destination << val;

      return;
    }

    // Now, we need to check for newlines in retrieved backtrace.
    // If we find one, output up until the newline, then output the newline
    // and the prefix and continue looking.
    size_t nl;
    size_t pos = 0;
    if(fatal)
    {
      Backtrace bt;
      std::string btLine = bt.ToString();
      while ((nl = btLine.find('\n', pos)) != std::string::npos)
      {
	PrefixIfNeeded();
	
	destination << btLine.substr(pos, nl - pos);
	destination << std::endl;
	newlined = true;
	  
	carriageReturned = true; // Regardless of whether or not we display it.
    
	pos = nl + 1;
      }
    }
     
    //The same logic like above, but this time for 'line'.
    pos = 0;
    while ((nl = line.find('\n', pos)) != std::string::npos)
    {
      PrefixIfNeeded();
<commit_msg>Work with mlpack::backtrace only when HAS_BFD_DL..<commit_after>/**
 * @file prefixedoutstream.hpp
 * @author <NAME>
 * @author <NAME>
 *
 * Implementation of templated PrefixedOutStream member functions.
 */
#ifndef __MLPACK_CORE_UTIL_PREFIXEDOUTSTREAM_IMPL_HPP
#define __MLPACK_CORE_UTIL_PREFIXEDOUTSTREAM_IMPL_HPP

// Just in case it hasn't been included.
#include "prefixedoutstream.hpp"

#ifdef HAS_BFD_DL
  #include "backtrace.hpp"
#endif

#include <iostream>

namespace mlpack {
namespace util {

template<typename T>
PrefixedOutStream& PrefixedOutStream::operator<<(const T& s)
{
  BaseLogic<T>(s);
  return *this;
}

template<typename T>
void PrefixedOutStream::BaseLogic(const T& val)
{
  // We will use this to track whether or not we need to terminate at the end of
  // this call (only for streams which terminate after a newline).
  bool newlined = false;
  std::string line;

  // If we need to, output the prefix.
  PrefixIfNeeded();

  std::ostringstream convert;
  convert << val;

  if (convert.fail())
  {
    PrefixIfNeeded();
    if (!ignoreInput)
    {
      destination << "Failed lexical_cast<std::string>(T) for output; output"
          " not shown." << std::endl;
      newlined = true;
    }
  }
  else
  {
    line = convert.str();

    // If the length of the casted thing was 0, it may have been a stream
    // manipulator, so send it directly to the stream and don't ask questions.
    if (line.length() == 0)
    {
      // The prefix cannot be necessary at this point.
      if (!ignoreInput) // Only if the user wants it.
        destination << val;

      return;
    }

    // Now, we need to check for newlines in retrieved backtrace.
    // If we find one, output up until the newline, then output the newline
    // and the prefix and continue looking.
    size_t nl;
    size_t pos = 0;
#ifdef HAS_BFD_DL
    if(fatal)
    {
      Backtrace bt;
      std::string btLine = bt.ToString();
      while ((nl = btLine.find('\n', pos)) != std::string::npos)
      {
	PrefixIfNeeded();
	
	destination << btLine.substr(pos, nl - pos);
	destination << std::endl;
	newlined = true;
	  
	carriageReturned = true; // Regardless of whether or not we display it.
    
	pos = nl + 1;
      }
    }
#endif     
    //The same logic like above, but this time for 'line'.
    pos = 0;
    while ((nl = line.find('\n', pos)) != std::string::npos)
    {
      PrefixIfNeeded();

{'fraction_non_alphanumeric': 0.11502546689303905, 'fraction_numerical': 0.0021222410865874364, 'mean_word_length': 2.8854080791426218, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3306382', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1503, 'n_words': 505}