Skip to content

Commit ac6b885

Browse files
author
Louis Fréneau
committed
Improve intermediate files exportation
- Thread-safe directory creation - By default a time stamp is appended to dir path - Previous behaviour (with file removing) is enable by intermediateFilesDirTimeStamp = false
1 parent 632958a commit ac6b885

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

src/lib/utils/fileExport.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,16 @@ void createDirs(const std::string& filePath) {
8686
return;
8787
}
8888

89-
// Create all missing directories
90-
if(!std::filesystem::create_directories(dir)) {
91-
throw std::runtime_error("Error : failed to create directories : " + dir.string() + " for the intermediate file : " + filePath);
89+
// Thread-safe missing directory creation
90+
static std::mutex fs_mutex;
91+
{
92+
std::lock_guard<std::mutex> lock(fs_mutex);
93+
std::error_code ec;
94+
if (!std::filesystem::create_directories(dir,ec)) {
95+
if(ec) {
96+
throw std::runtime_error("Error: failed to create directories: " + dir.string() + " for the intermediate file: " + filePath + ". " + ec.message());
97+
}
98+
}
9299
}
93100
}
94101

@@ -199,7 +206,7 @@ namespace FileExport {
199206

200207
void cleanIntermediateFiles() {
201208
// Remove all files within the intermediate files directory if it exists but keep the subdirectory structure intact.
202-
Logger::log<LogLevel::TRACE>("EXPORT FILE","Clean intermediate files directory.\n");
209+
Logger::log<LogLevel::INFO>("EXPORT FILE","Clean intermediate files directory.\n");
203210

204211
const std::filesystem::path root(p_->intermediateFilesDir);
205212

src/lib/utils/parameters.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ void initializeParameterMap(Parameters& param) {
162162

163163
// ___ Debug parameters ___ //
164164
{"exportIntermediateFiles", {BOOL, "", &param.exportIntermediateFiles}},
165+
{"intermediateFilesDirTimeStamp", {BOOL, "", &param.intermediateFilesDirTimeStamp}},
165166
{"timerLog", {BOOL, "", &param.timerLog}},
166167

167168
// ___ Activate or not some features ___ //

src/lib/utils/parameters.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ struct Parameters {
5656
bool doubleLayer = true;
5757
std::string logLevel = "INFO";
5858
bool errorsAreFatal = true;
59-
std::string intermediateFilesDir;
60-
59+
6160
// ___ Debug parameters ___ //
6261
bool exportIntermediateFiles = false;
62+
bool intermediateFilesDirTimeStamp = true;
63+
std::string intermediateFilesDir;
6364
bool timerLog = false; // TODO(lf): remove and activate it if loglevel is profiling
6465

6566
// ___ Activate or not some features ___ //

src/lib/uvgvpcc.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,31 @@ void parseUvgvpccParameters() {
389389
if(p_->attributeEncodingNbThread == 0) {
390390
uvgvpcc_enc::Logger::log<uvgvpcc_enc::LogLevel::DEBUG>("API","'attributeEncodingNbThread' is set to 0. The number of thread used for the attribute video 2D encoding is then the detected number of threads: "+detectedThreadNumber + "\n");
391391
setParameterValue("attributeEncodingNbThread",detectedThreadNumber,false);
392-
}
392+
}
393+
394+
if(p_->exportIntermediateFiles && p_->intermediateFilesDirTimeStamp) {
395+
uvgvpcc_enc::Logger::log<uvgvpcc_enc::LogLevel::DEBUG>("API","'intermediateFilesDirTimeStamp' is true, so a time stamp is added to the 'intermediateFilesDir' path.\n");
396+
397+
std::time_t now = std::time(nullptr);
398+
std::tm* localTime = std::localtime(&now);
399+
400+
std::ostringstream oss;
401+
oss << std::setfill('0') << std::setw(2) << (localTime->tm_year % 100)
402+
<< std::setw(2) << (localTime->tm_mon + 1)
403+
<< std::setw(2) << localTime->tm_mday
404+
<< std::setw(2) << localTime->tm_hour
405+
<< std::setw(2) << localTime->tm_min
406+
<< std::setw(2) << localTime->tm_sec;
407+
408+
std::string dir = p_->intermediateFilesDir;
409+
if (!dir.empty() && dir.back() == '/') {
410+
dir.pop_back(); // Remove trailing slash
411+
}
412+
413+
setParameterValue("intermediateFilesDir",dir + oss.str(),false);
414+
}
415+
416+
393417
}
394418

395419
static void initializeContext() {
@@ -427,7 +451,7 @@ void API::initializeEncoder() {
427451
initializeStaticParameters();
428452
initializeStaticFunctionPointers();
429453
initializeContext();
430-
if(p_->exportIntermediateFiles) FileExport::cleanIntermediateFiles();
454+
if(p_->exportIntermediateFiles && !p_->intermediateFilesDirTimeStamp) FileExport::cleanIntermediateFiles();
431455
initializationDone = true;
432456
}
433457

0 commit comments

Comments
 (0)