Skip to content

Commit

Permalink
add possibility to append to output file instead of truncating
Browse files Browse the repository at this point in the history
- add option file_append
- add default argument for file_append to various API functions
- closes #720
  • Loading branch information
svigerske committed Nov 4, 2023
1 parent 967403c commit 924f14f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ More detailed information about incremental changes can be found in the
Christoph Hansknecht].
- Print additional messages when reallocation of MA27 working space failed
[#671, by Daniel Oliveira].
- Added option `file_append` to specify whether to append to `output_file` instead of
truncating it. Added default argument `file_append` to `Journalist::AddFileJournal()`,
added default argument `fappend` to `FileJournal::Open()`, and added default argument
`file_append` to `IpoptApplication::OpenOutputFile()`. [#720]

### 3.14.12 (2023-04-05)

Expand Down
12 changes: 8 additions & 4 deletions src/Common/IpJournalist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,15 @@ bool Journalist::AddJournal(
SmartPtr<Journal> Journalist::AddFileJournal(
const std::string& journal_name,
const std::string& fname,
EJournalLevel default_level
EJournalLevel default_level,
bool file_append
)
{
SmartPtr<FileJournal> temp = new FileJournal(journal_name, default_level);

// Open the file (Note:, a fname of "stdout" is handled by the
// Journal class to mean stdout, etc.
if( temp->Open(fname.c_str()) && AddJournal(GetRawPtr(temp)) )
if( temp->Open(fname.c_str(), file_append) && AddJournal(GetRawPtr(temp)) )
{
return GetRawPtr(temp);
}
Expand Down Expand Up @@ -392,7 +393,10 @@ FileJournal::~FileJournal()
file_ = NULL;
}

bool FileJournal::Open(const char* fname)
bool FileJournal::Open(
const char* fname,
bool fappend
)
{
if( file_ && file_ != stdout && file_ != stderr )
{
Expand All @@ -414,7 +418,7 @@ bool FileJournal::Open(const char* fname)
else
{
// open the file on disk
file_ = fopen(fname, "w+");
file_ = fopen(fname, fappend ? "a+" : "w+");
if( file_ )
{
return true;
Expand Down
6 changes: 4 additions & 2 deletions src/Common/IpJournalist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ class IPOPTLIB_EXPORT Journalist: public ReferencedObject
virtual SmartPtr<Journal> AddFileJournal(
const std::string& location_name, /**< string identifier, which can be used to obtain the pointer to the new Journal at a later point using the GetJournal method */
const std::string& fname, /**< name of the file to which this Journal corresponds; use "stdout" for stdout and use "stderr" for stderr */
EJournalLevel default_level = J_WARNING /**< default journal level used to initialize the printing level for all categories */
EJournalLevel default_level = J_WARNING,/**< default journal level used to initialize the printing level for all categories */
bool file_append = false /**< whether to append to file or truncate it (since 3.14.13) */
);

/** Get an existing journal.
Expand Down Expand Up @@ -432,7 +433,8 @@ class IPOPTLIB_EXPORT FileJournal: public Journal
* @return false only if the file with the given name could not be opened
*/
virtual bool Open(
const char* fname
const char* fname, /**< name of file to open */
bool fappend = false /**< whether to append or truncate file (since 3.14.13) */
);

protected:
Expand Down
14 changes: 11 additions & 3 deletions src/Interfaces/IpIpoptApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ ApplicationReturnStatus IpoptApplication::Initialize(
{
file_print_level = print_level;
}
bool openend = OpenOutputFile(output_filename, file_print_level);
bool file_append;
options_->GetBoolValue("file_append", file_append, "");
bool openend = OpenOutputFile(output_filename, file_print_level, file_append);
if( !openend )
{
jnlst_->Printf(J_ERROR, J_INITIALIZATION, "Error opening output file \"%s\"\n", output_filename.c_str());
Expand Down Expand Up @@ -386,6 +388,11 @@ void IpoptApplication::RegisterOptions(
"NOTE: This option only works when read from the ipopt.opt options file! "
"Determines the verbosity level for the file specified by \"output_file\". "
"By default it is the same as \"print_level\".");
roptions->AddBoolOption(
"file_append",
"Whether to append to output file, if set, instead of truncating.",
false,
"NOTE: This option only works when read from the ipopt.opt options file!");
roptions->AddBoolOption(
"print_user_options",
"Print all options set by the user.",
Expand Down Expand Up @@ -943,14 +950,15 @@ ApplicationReturnStatus IpoptApplication::call_optimize()

bool IpoptApplication::OpenOutputFile(
std::string file_name,
EJournalLevel print_level
EJournalLevel print_level,
bool file_append
)
{
SmartPtr<Journal> file_jrnl = jnlst_->GetJournal("OutputFile:" + file_name);

if( IsNull(file_jrnl) )
{
file_jrnl = jnlst_->AddFileJournal("OutputFile:" + file_name, file_name.c_str(), print_level);
file_jrnl = jnlst_->AddFileJournal("OutputFile:" + file_name, file_name.c_str(), print_level, file_append);
}

// Check, if the output file could be created properly
Expand Down
5 changes: 3 additions & 2 deletions src/Interfaces/IpIpoptApplication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ class IPOPTLIB_EXPORT IpoptApplication: public ReferencedObject
* @return false if there was a problem
*/
virtual bool OpenOutputFile(
std::string file_name,
EJournalLevel print_level
std::string file_name, /**< name of file to open */
EJournalLevel print_level, /**< print level to be used */
bool file_append = false /**< whether to append to file or truncate (since 3.14.13) */
);

/**@name Accessor methods */
Expand Down

0 comments on commit 924f14f

Please sign in to comment.