Changed: Argument type of LogStream::addExtraLog is now a plain C-pointer,
hiding the fact that it uses shared_ptr internally. Also removed the NullStream object. Instead, the internal ostream pointer is set to NULL. Added: Default constructor for LogStream accepting a pointer as argument.
This commit is contained in:
		@@ -17,7 +17,9 @@
 | 
			
		||||
#include "tinyxml.h"
 | 
			
		||||
#include "IFEM.h"
 | 
			
		||||
#include "LogStream.h"
 | 
			
		||||
#include "PETScSupport.h"
 | 
			
		||||
#ifdef PARALLEL_PETSC
 | 
			
		||||
#include "petscsys.h"
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_OPENMP
 | 
			
		||||
#include <omp.h>
 | 
			
		||||
#endif
 | 
			
		||||
@@ -129,33 +131,28 @@ bool SIMoptions::parseOutputTag (const TiXmlElement* elem)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  else if (!strcasecmp(elem->Value(),"logging")) {
 | 
			
		||||
    int pid = 0;
 | 
			
		||||
#ifdef PARALLEL_PETSC
 | 
			
		||||
    MPI_Comm_rank(PETSC_COMM_WORLD,&pid);
 | 
			
		||||
#endif
 | 
			
		||||
    utl::getAttribute(elem,"output_pid",printPid);
 | 
			
		||||
    if (printPid != -1 && printPid != IFEM::getOptions().printPid) {
 | 
			
		||||
      IFEM::getOptions().printPid = printPid;
 | 
			
		||||
      int pid = 0;
 | 
			
		||||
#ifdef PARALLEL_PETSC
 | 
			
		||||
      MPI_Comm_rank(PETSC_COMM_WORLD,&pid);
 | 
			
		||||
#endif
 | 
			
		||||
      IFEM::cout.setPIDs(printPid==-1?pid:printPid, pid);
 | 
			
		||||
      if (printPid != -1 && pid != printPid)
 | 
			
		||||
        IFEM::cout.setStream(utl::NullStream);
 | 
			
		||||
 | 
			
		||||
      IFEM::cout << "IFEM: Printing output from PID " << printPid << " to screen" << std::endl;
 | 
			
		||||
      IFEM::cout.setPIDs(printPid,pid);
 | 
			
		||||
      if (printPid != pid)
 | 
			
		||||
        IFEM::cout.setNull();
 | 
			
		||||
      IFEM::cout <<"IFEM: Printing output from PID "<< printPid
 | 
			
		||||
                 <<" to console."<< std::endl;
 | 
			
		||||
    }
 | 
			
		||||
    utl::getAttribute(elem,"output_prefix",log_prefix);
 | 
			
		||||
    if (!log_prefix.empty() && log_prefix != IFEM::getOptions().log_prefix) {
 | 
			
		||||
      int pid = 0;
 | 
			
		||||
#ifdef PARALLEL_PETSC
 | 
			
		||||
      MPI_Comm_rank(PETSC_COMM_WORLD,&pid);
 | 
			
		||||
#endif
 | 
			
		||||
      if ((pid == 0 && printPid == -1) || pid == IFEM::getOptions().printPid)
 | 
			
		||||
        IFEM::cout << "IFEM: Logging output to files with prefix " << log_prefix << std::endl;
 | 
			
		||||
 | 
			
		||||
        IFEM::cout <<"IFEM: Logging output to files with prefix "
 | 
			
		||||
                   << log_prefix << std::endl;
 | 
			
		||||
      IFEM::getOptions().log_prefix = log_prefix;
 | 
			
		||||
      std::stringstream str;
 | 
			
		||||
      str << log_prefix << "_" << pid << ".log";
 | 
			
		||||
      std::shared_ptr<std::ostream> file(new std::ofstream(str.str()));
 | 
			
		||||
      IFEM::cout.addExtraLog(file);
 | 
			
		||||
      char cPid[12];
 | 
			
		||||
      sprintf(cPid,"_p%04d.log",pid);
 | 
			
		||||
      IFEM::cout.addExtraLog(new std::ofstream(log_prefix+cPid));
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -13,18 +13,14 @@
 | 
			
		||||
 | 
			
		||||
#include "LogStream.h"
 | 
			
		||||
 | 
			
		||||
namespace utl {
 | 
			
		||||
 | 
			
		||||
nullstream NullStream;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
LogStream::LogStream(std::ostream& out, int ppid, int mypid) :
 | 
			
		||||
utl::LogStream::LogStream(std::ostream& out, int ppid, int mypid) :
 | 
			
		||||
  m_out(&out), m_ppid(ppid), m_pid(mypid)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
LogStream& LogStream::operator<<(LogStream::StandardEndLine manip)
 | 
			
		||||
utl::LogStream& utl::LogStream::operator<<(LogStream::StandardEndLine manip)
 | 
			
		||||
{
 | 
			
		||||
  if (m_pid == m_ppid && m_out)
 | 
			
		||||
    manip(*m_out);
 | 
			
		||||
@@ -36,7 +32,7 @@ LogStream& LogStream::operator<<(LogStream::StandardEndLine manip)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
LogStream& LogStream::operator=(const LogStream& log2)
 | 
			
		||||
utl::LogStream& utl::LogStream::operator=(const LogStream& log2)
 | 
			
		||||
{
 | 
			
		||||
  m_out = log2.m_out;
 | 
			
		||||
  m_extra = log2.m_extra;
 | 
			
		||||
@@ -47,7 +43,14 @@ LogStream& LogStream::operator=(const LogStream& log2)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void LogStream::addExtraLog(std::shared_ptr<std::ostream>& extra, bool clear)
 | 
			
		||||
void utl::LogStream::addExtraLog(std::ostream* extra, bool clear)
 | 
			
		||||
{
 | 
			
		||||
  std::shared_ptr<std::ostream> file(extra);
 | 
			
		||||
  this->addExtraLog(file,clear);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void utl::LogStream::addExtraLog(std::shared_ptr<std::ostream> extra, bool clear)
 | 
			
		||||
{
 | 
			
		||||
  if (clear)
 | 
			
		||||
    m_extra.clear();
 | 
			
		||||
@@ -56,13 +59,7 @@ void LogStream::addExtraLog(std::shared_ptr<std::ostream>& extra, bool clear)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int LogStream::precision() const
 | 
			
		||||
{
 | 
			
		||||
  return m_out?m_out->precision():6;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int LogStream::precision(int streamsize)
 | 
			
		||||
int utl::LogStream::precision(int streamsize)
 | 
			
		||||
{
 | 
			
		||||
  int result = streamsize;
 | 
			
		||||
  if (m_out)
 | 
			
		||||
@@ -74,7 +71,7 @@ int LogStream::precision(int streamsize)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void LogStream::flush()
 | 
			
		||||
void utl::LogStream::flush()
 | 
			
		||||
{
 | 
			
		||||
  if (m_out)
 | 
			
		||||
    m_out->flush();
 | 
			
		||||
@@ -83,7 +80,7 @@ void LogStream::flush()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
std::ios_base::fmtflags LogStream::flags(std::ios_base::fmtflags flags)
 | 
			
		||||
std::ios_base::fmtflags utl::LogStream::flags(std::ios_base::fmtflags flags)
 | 
			
		||||
{
 | 
			
		||||
  std::ios_base::fmtflags result = flags;
 | 
			
		||||
  if (m_out)
 | 
			
		||||
@@ -93,5 +90,3 @@ std::ios_base::fmtflags LogStream::flags(std::ios_base::fmtflags flags)
 | 
			
		||||
 | 
			
		||||
  return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -11,63 +11,47 @@
 | 
			
		||||
//!
 | 
			
		||||
//==============================================================================
 | 
			
		||||
 | 
			
		||||
#ifndef LOGSTREAM_H_
 | 
			
		||||
#define LOGSTREAM_H_
 | 
			
		||||
#ifndef LOG_STREAM_H_
 | 
			
		||||
#define LOG_STREAM_H_
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <iomanip>
 | 
			
		||||
#include <memory>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
// this is the type of std::cout
 | 
			
		||||
typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
 | 
			
		||||
 | 
			
		||||
namespace utl {
 | 
			
		||||
 | 
			
		||||
//! \brief Null-stream object type
 | 
			
		||||
class nullstream : public std::ostream
 | 
			
		||||
//! \brief Logging stream class.
 | 
			
		||||
class LogStream
 | 
			
		||||
{
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
template<typename T>
 | 
			
		||||
nullstream& operator<<(nullstream& o, const T& x) { return o; }
 | 
			
		||||
 | 
			
		||||
extern nullstream NullStream;
 | 
			
		||||
 | 
			
		||||
//! \brief Logging stream class
 | 
			
		||||
class LogStream {
 | 
			
		||||
public:
 | 
			
		||||
  //! \brief Default constructor
 | 
			
		||||
  //! \param out The ostream to wrap
 | 
			
		||||
  //! \brief Default constructor.
 | 
			
		||||
  //! \param out The output stream to wrap
 | 
			
		||||
  LogStream(std::ostream* out = nullptr) : m_out(out) { m_ppid = m_pid = 0; }
 | 
			
		||||
  //! \brief Constructor initializing the output stream from a reference.
 | 
			
		||||
  //! \param out The output stream to wrap
 | 
			
		||||
  //! \param ppid The PID to print on
 | 
			
		||||
  //! \param mypid The PID of this process
 | 
			
		||||
  LogStream(std::ostream& out, int ppid=0, int mypid=0);
 | 
			
		||||
  LogStream(std::ostream& out, int ppid = 0, int mypid = 0);
 | 
			
		||||
 | 
			
		||||
  //! \brief Set PIDs for stream
 | 
			
		||||
  void setPIDs(int ppid, int mypid)
 | 
			
		||||
  {
 | 
			
		||||
    m_ppid = ppid;
 | 
			
		||||
    m_pid = mypid;
 | 
			
		||||
  }
 | 
			
		||||
  //! \brief Nullifies the output stream.
 | 
			
		||||
  void setNull() { m_out = nullptr; }
 | 
			
		||||
  //! \brief Sets the output stream.
 | 
			
		||||
  void setStream(std::ostream& out) { m_out = &out; }
 | 
			
		||||
  //! \brief Sets PIDs for the stream.
 | 
			
		||||
  void setPIDs(int ppid, int mypid) { m_ppid = ppid; m_pid = mypid; }
 | 
			
		||||
 | 
			
		||||
  //! \brief Set output stream
 | 
			
		||||
  void setStream(std::ostream& out)
 | 
			
		||||
  {
 | 
			
		||||
    m_out = &out;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  //! \brief Add an extra logging stream
 | 
			
		||||
  void addExtraLog(std::shared_ptr<std::ostream>& extra, bool clear=false);
 | 
			
		||||
  //! \brief Adds an extra logging stream.
 | 
			
		||||
  void addExtraLog(std::ostream* extra, bool clear = false);
 | 
			
		||||
  //! \brief Adds an extra logging stream.
 | 
			
		||||
  void addExtraLog(std::shared_ptr<std::ostream> extra, bool clear = false);
 | 
			
		||||
 | 
			
		||||
  //! \brief Write data to stream
 | 
			
		||||
    template<typename T>
 | 
			
		||||
  LogStream& operator <<(const T& data)
 | 
			
		||||
  {
 | 
			
		||||
    return write(data);
 | 
			
		||||
  }
 | 
			
		||||
  template<typename T>
 | 
			
		||||
  LogStream& operator<<(const T& data) { return this->write(data); }
 | 
			
		||||
 | 
			
		||||
  //! \brief Write data to stream
 | 
			
		||||
    template<typename T>
 | 
			
		||||
  template<typename T>
 | 
			
		||||
  LogStream& write(const T& data)
 | 
			
		||||
  {
 | 
			
		||||
    if (m_ppid == m_pid && m_out)
 | 
			
		||||
@@ -78,31 +62,33 @@ public:
 | 
			
		||||
    return *this;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  //! \brief Get current precision
 | 
			
		||||
  int precision() const;
 | 
			
		||||
 | 
			
		||||
  //! \brief Set precision of output
 | 
			
		||||
  int precision(int streamsize);
 | 
			
		||||
  //! \brief Get current precision
 | 
			
		||||
  int precision() const { return m_out ? m_out->precision() : 6; }
 | 
			
		||||
 | 
			
		||||
  //! \brief Check state of stream
 | 
			
		||||
  bool good() const { return m_out?m_out->good():true; }
 | 
			
		||||
  bool good() const { return m_out ? m_out->good() : true; }
 | 
			
		||||
 | 
			
		||||
  //! \brief Flush streams
 | 
			
		||||
  void flush();
 | 
			
		||||
 | 
			
		||||
  //! \brief Obtain stream flags
 | 
			
		||||
  std::ios_base::fmtflags flags() const { return m_out?m_out->flags():std::ios_base::fmtflags(); }
 | 
			
		||||
 | 
			
		||||
  //! \brief Set stream flags
 | 
			
		||||
  std::ios_base::fmtflags flags(std::ios_base::fmtflags fmtfl);
 | 
			
		||||
 | 
			
		||||
  //! \brief Assignment operator
 | 
			
		||||
  LogStream& operator=(const LogStream&);
 | 
			
		||||
 | 
			
		||||
  //! \brief This is the type of std::cout.
 | 
			
		||||
  typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
 | 
			
		||||
  //! \brief Function pointer type.
 | 
			
		||||
  typedef CoutType& (*StandardEndLine)(CoutType&);
 | 
			
		||||
 | 
			
		||||
  //! \brief Handling of the std::endl manipulator with friends
 | 
			
		||||
  LogStream& operator << (StandardEndLine manip);
 | 
			
		||||
 | 
			
		||||
protected:
 | 
			
		||||
  std::ostream* m_out; //!< Main output stream
 | 
			
		||||
  std::vector<std::shared_ptr<std::ostream>> m_extra; //!< Extra output streams
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user