Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions src/picongpu/include/plugins/common/txtFileHandling.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2015 Axel Huebl
* Copyright 2015 Axel Huebl, Richard Pausch
*
* This file is part of PIConGPU.
*
Expand Down Expand Up @@ -34,7 +34,8 @@ using namespace boost::filesystem;
/** Restore a txt file from the checkpoint dir
*
* Restores a txt file from the checkpoint dir and starts appending to it.
* Opened files in \see outFile are closed and a valid handle is opened again.
* Opened files in \see outFile are closed and a valid handle is opened again
* if a restart file is found. Otherwise new output file stays untouched.
*
* \param outFile std::ofstream file handle to regular file that shall be restored
* \param filename the file's name
Expand All @@ -46,28 +47,46 @@ using namespace boost::filesystem;
bool restoreTxtFile( std::ofstream& outFile, std::string filename,
uint32_t restartStep, const std::string restartDirectory )
{
if( outFile.is_open() )
outFile.close();

/* get restart time step as string */
std::stringstream sStep;
sStep << restartStep;

/* set location of restart file and output file */
path src( restartDirectory + std::string("/") + filename +
std::string(".") + sStep.str() );
path dst( filename );

copy_file( src,
dst,
copy_option::overwrite_if_exists );

outFile.open( filename.c_str(), std::ofstream::out | std::ostream::app );
if( !outFile )
/* check whether restart file exists */
std::ifstream restartFile(src.c_str());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an interesting way of checking that.
We usually use if( !boost::filesystem::exists( src ) ) for that - maybe that's more suited here, too.

also saves you the restartFile.close(); lines below.

boost:filesystem is already in the scope, so you can use a slick if( ! exists( src ) ) :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ax3l Thanks, that's way better. I will change it to boost::filesystem::....

if(! restartFile.good())
{
std::cerr << "[Plugin] Can't open file '" << filename
<< "', output disabled" << std::endl;
return false;
/* restart file does not exists */
restartFile.close();
std::cerr << "Plugin restart file: \n \t" << src << "\n was not found." << std::endl;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is not ideal for "cerr" but could be maybe better in a log level such as INPUT_OUTPUT. (Can be a totally normal state as the bug shows us.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change this to a log warning.

std::cerr << "Starting plugin from current time step." << std::endl;
return true;
}
else
{
/* restart file found - fix output file created at restart */
restartFile.close();

if( outFile.is_open() )
outFile.close();

copy_file( src,
dst,
copy_option::overwrite_if_exists );

outFile.open( filename.c_str(), std::ofstream::out | std::ostream::app );
if( !outFile )
{
std::cerr << "[Plugin] Can't open file '" << filename
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line on the other hand is an unexpected state. even if log INPUT_OUTPUT would apply somehow, I think cerr is appropriate.

(Nevertheless, it would be clean if we introduce a specific, default-enabled log state that writes to cerr but that is an other story.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will keep this a std::cerr.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

<< "', output disabled" << std::endl;
return false;
}
return true;
}
return true;
}

/** Checkpoints a txt file
Expand Down