Changeset 418


Ignore:
Timestamp:
Aug 1, 2005 1:46:30 PM (14 years ago)
Author:
claird
Message:

Made the Journal class (which used to work with files) a
base class and created a new class FileJournal? which
inherits off of Journal. Now users could create specialized
Journal classes for their particular needs.

Also changed the std::vector<Journal*> to a
std::vector< SmartPtr?<Journal> > to have smart ptrs in the
handling of Journals. (Useful now that a user can create a
special Journal and give it to the Journalist...)

Location:
branches/dev
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/dev/Apps/StdInterface/IpIpoptApplication.cpp

    r417 r418  
    3131# ifdef IP_DEBUG
    3232    DebugJournalistWrapper::SetJournalist(GetRawPtr(jnlst_));
    33     Journal* debug_jrnl = jnlst_->AddJournal("Debug", "debug.out", J_SUMMARY);
     33    SmartPtr<Journal> debug_jrnl = jnlst_->AddFileJournal("Debug", "debug.out", J_SUMMARY);
    3434    debug_jrnl->SetPrintLevel(J_DBG, J_ALL);
    3535# endif
     
    3939
    4040
    41     Journal* stdout_jrnl = NULL;
     41    SmartPtr<Journal> stdout_jrnl = NULL;
    4242    if (create_console_out) {
    4343      stdout_jrnl =
    44         jnlst_->AddJournal("console", "stdout", J_SUMMARY);
     44        jnlst_->AddFileJournal("console", "stdout", J_SUMMARY);
    4545      stdout_jrnl->SetPrintLevel(J_DBG, J_NONE);
    4646    }
     
    104104        file_print_level = print_level;
    105105      }
    106       Journal* file_jrnl = jnlst_->AddJournal("OutputFile", output_filename.c_str(), file_print_level);
     106      SmartPtr<Journal> file_jrnl = jnlst_->AddFileJournal("OutputFile", output_filename.c_str(), file_print_level);
    107107      file_jrnl->SetPrintLevel(J_DBG, J_NONE);
    108108    }
  • branches/dev/Common/IpJournalist.cpp

    r403 r418  
    2020  Journalist::~Journalist()
    2121  {
    22     // delete the journals
    23     for (Index i=0; i<(Index)journals_.size(); i++) {
    24       Journal* journal = journals_[i];
    25       delete journal;
    26     }
    27 
    2822    journals_.clear();
    2923  }
     
    204198  }
    205199
    206   Journal* Journalist::AddJournal(
     200  bool Journalist::AddJournal(const SmartPtr<Journal> jrnl)
     201  {
     202    DBG_ASSERT(IsValid(jrnl));
     203    std::string name = jrnl->Name();
     204
     205    SmartPtr<Journal> temp = GetJournal(name);
     206    DBG_ASSERT(IsNull(temp));
     207    if (IsValid(temp)) {
     208      return false;
     209    }
     210
     211    journals_.push_back(jrnl);
     212    return true;
     213  }
     214
     215  SmartPtr<Journal> Journalist::AddFileJournal(
    207216    const std::string& journal_name,
    208217    const std::string& fname,
     
    210219  )
    211220  {
    212     // check for an existing journal of the same name
    213     Journal* retValue = GetJournal(journal_name);
    214     if (!retValue) {
    215       // journal does not already exist, add a new one
    216       Journal* temp = new Journal(journal_name, default_level);
    217 
    218       // Open the file (Note:, a fname of "stdout" is handled by the
    219       // Journal class to mean stdout, etc.
    220       if (temp->Open(fname.c_str())) {
    221         // journal was created, add it to the list
    222         journals_.push_back(temp);
    223         retValue = temp;
    224       }
    225       else {
    226         // journal could not be created
    227         delete temp;
    228       }
    229     }
    230 
    231     return retValue;
     221    SmartPtr<FileJournal> temp = new FileJournal(journal_name, default_level);
     222
     223    // Open the file (Note:, a fname of "stdout" is handled by the
     224    // Journal class to mean stdout, etc.
     225    if (temp->Open(fname.c_str()) && AddJournal(GetRawPtr(temp))) {
     226      return GetRawPtr(temp);
     227    }
     228    return NULL;
    232229  }
    233230
     
    239236  }
    240237
    241   Journal* Journalist::GetJournal(
     238  SmartPtr<Journal> Journalist::GetJournal(
    242239    const std::string& journal_name
    243240  )
    244241  {
    245     Journal* retValue = NULL;
     242    SmartPtr<Journal> retValue = NULL;
    246243
    247244    // try to find the journal
    248245    for (Index i=0; i<(Index)journals_.size(); i++) {
    249       Journal* tmp = journals_[i];
     246      SmartPtr<Journal> tmp = journals_[i];
    250247      if (tmp->Name() == journal_name) {
    251248        retValue = tmp;
     
    266263  )
    267264      :
    268       name_(name),
    269       file_(NULL)
     265      name_(name)
    270266  {
    271267    for (Index i=0; i<J_LAST_CATEGORY; i++) {
     
    275271
    276272  Journal::~Journal()
     273  {}
     274
     275  std::string Journal::Name()
     276  {
     277    return name_;
     278  }
     279
     280  bool Journal::IsAccepted(
     281    EJournalCategory category,
     282    EJournalLevel level
     283  ) const
     284  {
     285    if (print_levels_[(Index)category] >= (Index) level) {
     286      return true;
     287    }
     288
     289    return false;
     290  }
     291
     292  void Journal::SetPrintLevel(
     293    EJournalCategory category,
     294    EJournalLevel level)
     295  {
     296    print_levels_[(Index)category] = (Index) level;
     297  }
     298
     299  void Journal::SetAllPrintLevels(
     300    EJournalLevel level)
     301  {
     302    for (Index category=(Index)J_DBG;
     303         category<(Index)J_LAST_CATEGORY;
     304         category++) {
     305      print_levels_[category] = (Index) level;
     306    }
     307  }
     308
     309
     310  ///////////////////////////////////////////////////////////////////////////
     311  //                 Implementation of the FileJournal class                   //
     312  ///////////////////////////////////////////////////////////////////////////
     313
     314  FileJournal::FileJournal(
     315    const std::string& name,
     316    EJournalLevel default_level
     317  )
     318      :
     319      Journal(name, default_level),
     320      file_(NULL)
     321  {}
     322
     323  FileJournal::~FileJournal()
    277324  {
    278325    if (file_ && file_ != stdout && file_ != stderr) {
     
    283330  }
    284331
    285   std::string Journal::Name()
    286   {
    287     return name_;
    288   }
    289 
    290   bool Journal::Open(const char* fname)
     332
     333  bool FileJournal::Open(const char* fname)
    291334  {
    292335    if (file_ && file_ != stdout && file_ != stderr) {
     
    315358  }
    316359
    317   bool Journal::IsAccepted(
    318     EJournalCategory category,
    319     EJournalLevel level
    320   ) const
    321   {
    322     if (print_levels_[(Index)category] >= (Index) level) {
    323       return true;
    324     }
    325 
    326     return false;
    327   }
    328 
    329   void Journal::SetPrintLevel(
    330     EJournalCategory category,
    331     EJournalLevel level)
    332   {
    333     print_levels_[(Index)category] = (Index) level;
    334   }
    335 
    336   void Journal::SetAllPrintLevels(
    337     EJournalLevel level)
    338   {
    339     for (Index category=(Index)J_DBG;
    340          category<(Index)J_LAST_CATEGORY;
    341          category++) {
    342       print_levels_[category] = (Index) level;
    343     }
    344   }
    345 
    346   void Journal::Print(const char* str)
     360
     361  void FileJournal::PrintImpl(const char* str)
    347362  {
    348363    DBG_START_METH("Journal::Print", 0);
     
    353368  }
    354369
    355   void Journal::Printf(const char* pformat, va_list ap)
     370  void FileJournal::PrintfImpl(const char* pformat, va_list ap)
    356371  {
    357372    DBG_START_METH("Journal::Printf", 0);
     
    362377  }
    363378
    364   void Journal::PrintVector(const std::string name, const Vector& vector, Index indent, std::string prefix)
     379  void FileJournal::PrintVectorImpl(const std::string name, const Vector& vector, Index indent, std::string prefix)
    365380  {
    366381    DBG_START_METH("Journal::PrintVector", 0);
     
    371386  }
    372387
    373   void Journal::PrintMatrix(const std::string name, const Matrix& matrix, Index indent, std::string prefix)
     388  void FileJournal::PrintMatrixImpl(const std::string name, const Matrix& matrix, Index indent, std::string prefix)
    374389  {
    375390    DBG_START_METH("Journal::PrintMatrix", 0);
     
    380395  }
    381396
    382   void Journal::FlushBuffer()
     397  void FileJournal::FlushBufferImpl()
    383398  {
    384399    if (file_) {
  • branches/dev/Common/IpJournalist.hpp

    r331 r418  
    1919#include "IpTypes.hpp"
    2020#include "IpReferenced.hpp"
     21#include "IpSmartPtr.hpp"
    2122
    2223namespace Ipopt
     
    2526  // forward declarations
    2627  class Journal;
     28  class FileJournal;
    2729  class Matrix;
    2830  class Vector;
     
    7678   * Authors use the
    7779   * Journals: You can add as many Journals as you like to the
    78    * Journalist with the AddJournal method. Each one represents
    79    * a different printing location (or file).  Then, you can
    80    * call the "print" methods of the Journalist to output
     80   * Journalist with the AddJournal or the AddFileJournal methods.
     81   * Each one represents a different printing location (or file).
     82   * Then, you can call the "print" methods of the Journalist to output
    8183   * information to each of the journals.
    8284   *
     
    8486   * appropriately with an EJournalCategory and EJournalLevel.
    8587   *
    86    * The AddJournal
     88   * The AddFileJournal
    8789   * method returns a pointer to the newly created Journal object
    8890   * (if successful) so you can set Acceptance criteria for that
     
    185187     * criteria for that file.
    186188     *
    187      * Use these methods to setup the wanted journals (files).
     189     * Use these methods to setup the journals (files or other output).
    188190     * These are the internal objects that keep track of the print levels
    189191     * for each category. Then use the internal Journal objects to
    190192     * set specific print levels for each category (or keep defaults).
    191193     * 
    192      * Note: the lifetime of these internal objects is controlled by
    193      * the Journalist class. Do not try to delete and do not try to keep the
    194      * pointer outside of the current scope.
    195194     */
    196195    //@{
    197196    /** Add a new journal.  The location_name is a string identifier,
    198197     *  which can be used to obtain the pointer to the new Journal at
    199      *  a later point using the GetJournal method.  fname is the name
     198     *  a later point using the GetJournal method.
     199     *  The default_level is
     200     *  used to initialize the * printing level for all categories.
     201     */
     202    bool AddJournal(const SmartPtr<Journal> jrnl);
     203
     204    /** Add a new FileJournal. fname is the name
    200205     *  of the * file to which this Journal corresponds.  Use
    201206     *  fname="stdout" * for stdout, and use fname="stderr" for
    202207     *  stderr.  This method * returns the Journal pointer so you can
    203      *  set specific acceptance * criteria.  It returns NULL if there
    204      *  was a problem creating a * new Journal.  The default_level is
    205      *  used to initialize the * printing level for all categories.
    206      */
    207     Journal* AddJournal(
     208     *  set specific acceptance criteria.  It returns NULL if there
     209     *  was a problem creating a new Journal.   
     210     */
     211    SmartPtr<Journal> AddFileJournal(
    208212      const std::string& location_name,    /** identifier */
    209213      const std::string& fname,
     
    214218     *  the acceptance criteria at runtime.
    215219     */
    216     Journal* GetJournal(const std::string& location_name);
     220    SmartPtr<Journal> GetJournal(const std::string& location_name);
    217221    //@}
    218222
     
    235239    //** Private Data Members. */
    236240    //@{
    237     std::vector<Journal*> journals_;
     241    std::vector< SmartPtr<Journal> > journals_;
    238242    //@}
    239243  };
    240244
    241   /** Journal class (part of the Journalist implementation.).  This
    242    * class stores the specific information for one journal, the file
    243    * location (stdout, stderr, or disk), and the acceptance criteria.
     245  /** Journal class (part of the Journalist implementation.). This
     246   *  class is the base class for all Journals. It controls the
     247   *  acceptance criteria for print statements etc. Derived classes
     248   *  like the FileJournal - output those messages to specific locations
    244249   */
    245   class Journal
     250  class Journal : public ReferencedObject
    246251  {
    247252  public:
     
    250255
    251256    /** Destructor. */
    252     ~Journal();
     257    virtual ~Journal();
    253258
    254259    /** Get the name of the Journal */
     
    264269      EJournalLevel level
    265270    );
     271
     272    /**@name Journal Output Methods. These methods are called by the
     273     *  Journalist who first checks if the output print level and category
     274     *  are acceptable.
     275     *  Calling the Print methods explicitly (instead of through the
     276     *  Journalist will output the message regardless of print level
     277     *  and category. You should use the Journalist to print & flush instead
     278     */
     279    //@{
     280    /** Ask if a particular print level/category is accepted by the
     281     * journal.
     282     */
     283    bool IsAccepted(
     284      EJournalCategory category, EJournalLevel level
     285    ) const;
     286
     287    /** Print to the designated output location */
     288    void Print(const char* str)
     289    {
     290      PrintImpl(str);
     291    }
     292
     293    /** Printf to the designated output location */
     294    void Printf(const char* pformat, va_list ap)
     295    {
     296      PrintfImpl(pformat, ap);
     297    }
     298
     299    /** Print vector to the designated output location */
     300    void PrintVector(std::string name, const Vector& vector,
     301                     Index indent, std::string prefix)
     302    {
     303      PrintVectorImpl(name, vector, indent, prefix);
     304    }
     305
     306    /** Print matrix to the designated output location */
     307    void PrintMatrix(const std::string name, const Matrix& matrix,
     308                     Index indent, std::string prefix)
     309    {
     310      PrintMatrixImpl(name, matrix, indent, prefix);
     311    }
     312
     313    /** Flush output buffer.*/
     314    void FlushBuffer()
     315    {
     316      FlushBufferImpl();
     317    }
     318    //@}
     319
     320  protected:
     321    /**@name Implementation version of Print methods. Derived classes
     322     * should overload the Impl methods.
     323     */
     324    //@{
     325    /** Print to the designated output location */
     326    virtual void PrintImpl(const char* str)=0;
     327
     328    /** Printf to the designated output location */
     329    virtual void PrintfImpl(const char* pformat, va_list ap)=0;
     330
     331    /** Print vector to the designated output location */
     332    virtual void PrintVectorImpl(std::string name, const Vector& vector, Index indent, std::string prefix)=0;
     333
     334    /** Print matrix to the designated output location */
     335    virtual void PrintMatrixImpl(const std::string name, const Matrix& matrix, Index indent, std::string prefix)=0;
     336
     337    /** Flush output buffer.*/
     338    virtual void FlushBufferImpl()=0;
     339    //@}
    266340
    267341  private:
     
    289363    /** vector of integers indicating the level for each category */
    290364    Index print_levels_[J_LAST_CATEGORY];
    291 
    292     /** FILE pointer for the output destination */
    293     FILE* file_;
    294 
    295     /**@name Private methods for the Journalist (friend) to call*/
    296     //@{
     365  };
     366
     367
     368  /** FileJournal class. This is a particular Journal implementation that
     369   *  writes to a file for output. It can write to (stdout, stderr, or disk)
     370   *  by using "stdout" and "stderr" as filenames.
     371   */
     372  class FileJournal : public Journal
     373  {
     374  public:
     375    /** Constructor. */
     376    FileJournal(const std::string& name, EJournalLevel default_level);
     377
     378    /** Destructor. */
     379    virtual ~FileJournal();
     380
    297381    /** Open a new file for the output location.
    298382     *  Special Names: stdout means stdout,
     
    304388    bool Open(const char* fname);
    305389
    306     /** Ask if a particular print level/category is accepted by the
    307      * journal.
    308      */
    309     bool IsAccepted(
    310       EJournalCategory category, EJournalLevel level
    311     ) const;
    312 
     390  protected:
     391    /**@name Implementation version of Print methods - Overloaded from
     392     * Journal base class.
     393     */
     394    //@{
    313395    /** Print to the designated output location */
    314     void Print(const char* str);
     396    virtual void PrintImpl(const char* str);
    315397
    316398    /** Printf to the designated output location */
    317     void Printf(const char* pformat, va_list ap);
     399    virtual void PrintfImpl(const char* pformat, va_list ap);
    318400
    319401    /** Print vector to the designated output location */
    320     void PrintVector(std::string name, const Vector& vector, Index indent, std::string prefix);
     402    virtual void PrintVectorImpl(std::string name, const Vector& vector, Index indent, std::string prefix);
    321403
    322404    /** Print matrix to the designated output location */
    323     void PrintMatrix(const std::string name, const Matrix& matrix, Index indent, std::string prefix);
    324 
    325     /** Flush output buffer */
    326     void FlushBuffer();
    327     //@}
    328 
    329     friend class Journalist;
     405    virtual void PrintMatrixImpl(const std::string name, const Matrix& matrix, Index indent, std::string prefix);
     406
     407    /** Flush output buffer.*/
     408    virtual void FlushBufferImpl();
     409    //@}
     410
     411  private:
     412    /**@name Default Compiler Generated Methods
     413     * (Hidden to avoid implicit creation/calling).
     414     * These methods are not implemented and
     415     * we do not want the compiler to implement
     416     * them for us, so we declare them private
     417     * and do not define them. This ensures that
     418     * they will not be implicitly created/called. */
     419    //@{
     420    /** Default Constructor */
     421    FileJournal();
     422
     423    /** Copy Constructor */
     424    FileJournal(const FileJournal&);
     425
     426    /** Overloaded Equals Operator */
     427    void operator=(const FileJournal&);
     428    //@}
     429
     430    /** FILE pointer for the output destination */
     431    FILE* file_;
     432    //@}
    330433  };
    331434}
Note: See TracChangeset for help on using the changeset viewer.