Skip to content

Commit

Permalink
ENH: Add "-loglevel" command-line option to elastix and transformix exe
Browse files Browse the repository at this point in the history
  • Loading branch information
N-Dekker committed Feb 20, 2023
1 parent 2822234 commit c90f1c4
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 38 deletions.
59 changes: 35 additions & 24 deletions Core/Main/elastix.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ constexpr const char * elastixHelpText =
" -fp point set for fixed image\n"
" -mp point set for moving image\n"
" -t0 parameter file for initial transform\n"
" -loglevel set the log level to \"off\", \"error\", \"warning\", or \"info\" (default),\n"
" -priority set the process priority to high, abovenormal, normal (default),\n"
" belownormal, or idle (Windows only option)\n"
" -threads set the maximum number of threads of elastix\n\n"
Expand Down Expand Up @@ -135,6 +136,7 @@ main(int argc, char ** argv)
ArgumentMapType argMap;
std::queue<std::string> parameterFileList;
std::string outFolder;
auto level = elx::log::level::info;

/** Put command line parameters into parameterFileList. */
for (unsigned int i = 1; static_cast<long>(i) < (argc - 1); i += 2)
Expand All @@ -155,37 +157,46 @@ main(int argc, char ** argv)
}
else
{
if (key == "-out")
if (key == "-loglevel")
{
/** Make sure that last character of the output folder equals a '/' or '\'. */
const char last = value.back();
if (last != '/' && last != '\\')
if (!ToLogLevel(value, level))
{
value.append("/");
// Unsupported log level value.
return EXIT_FAILURE;
}
value = elx::Conversion::ToNativePathNameSeparators(value);

/** Save this information. */
outFolder = value;

} // end if key == "-out"

/** Attempt to save the arguments in the ArgumentMap. */
if (argMap.count(key) == 0)
{
argMap.insert(ArgumentMapEntryType(key, value));
}
else
{
/** Duplicate arguments. */
std::cerr << "WARNING!\n"
<< "Argument " << key << "is only required once.\n"
<< "Arguments " << key << " " << value << "are ignored" << std::endl;
}
if (key == "-out")
{
/** Make sure that last character of the output folder equals a '/' or '\'. */
const char last = value.back();
if (last != '/' && last != '\\')
{
value.append("/");
}
value = elx::Conversion::ToNativePathNameSeparators(value);

} // end else (so, if key does not equal "-p")
/** Save this information. */
outFolder = value;

} // end for loop
} // end if key == "-out"

/** Attempt to save the arguments in the ArgumentMap. */
if (argMap.count(key) == 0)
{
argMap.insert(ArgumentMapEntryType(key, value));
}
else
{
/** Duplicate arguments. */
std::cerr << "WARNING!\n"
<< "Argument " << key << "is only required once.\n"
<< "Arguments " << key << " " << value << "are ignored" << std::endl;
}
}
} // end else (so, if key does not equal "-p")
} // end for loop

/** The argv0 argument, required for finding the component.dll/so's. */
argMap.insert(ArgumentMapEntryType("-argv0", argv[0]));
Expand Down Expand Up @@ -213,7 +224,7 @@ main(int argc, char ** argv)
{
/** Setup the log system. */
const std::string logFileName = outFolder + "elastix.log";
const int returndummy2 = elx::log::setup(logFileName, true, true) ? 0 : 1;
const int returndummy2 = elx::log::setup(logFileName, true, true, level) ? 0 : 1;

if (returndummy2 != 0)
{
Expand Down
34 changes: 34 additions & 0 deletions Core/Main/elxMainExeUtilities.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,37 @@ elastix::MakeStringOfCommandLineArguments(const char * const * const arguments)
}
return outputStringStream.str();
}

bool
elastix::ToLogLevel(const std::string & str, log::level & logLevel)
{
constexpr std::pair<log::level, const char *> levels[] = { { log::level::info, "info" },
{ log::level::warn, "warning" },
{ log::level::err, "error" },
{ log::level::off, "off" } };
for (const auto & pair : levels)
{
if (str == pair.second)
{
logLevel = pair.first;
return true;
}
}

std::string supportedLevels;

for (const auto & pair : levels)
{
if (!supportedLevels.empty())
{
// Use comma as separator.
supportedLevels += ", ";
}
supportedLevels += '"';
supportedLevels += pair.second;
supportedLevels += '"';
}
std::cerr << "ERROR: The specified log level (\"" << str
<< "\") is not supported. Supported levels: " << supportedLevels << ".\n";
return false;
}
5 changes: 5 additions & 0 deletions Core/Main/elxMainExeUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#ifndef elxMainExeUtilities_h
#define elxMainExeUtilities_h

#include "elxlog.h"

#include <exception>
#include <string>

Expand All @@ -36,6 +38,9 @@ GetExtendedVersionInformation(const char * const executableName, const char * co
std::string
MakeStringOfCommandLineArguments(const char * const * const arguments);

bool
ToLogLevel(const std::string & str, log::level & logLevel);

} // namespace elastix

#endif
40 changes: 26 additions & 14 deletions Core/Main/transformix.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ constexpr const char * transformixHelpText =
" spatial Jacobian\n"
" -jacmat use \"-jacmat all\" to generate an image with the spatial Jacobian\n"
" matrix at each voxel\n"
" -loglevel set the log level to \"off\", \"error\", \"warning\", or \"info\" (default),\n"
" -priority set the process priority to high, abovenormal, normal (default),\n"
" belownormal, or idle (Windows only option)\n"
" -threads set the maximum number of threads of transformix\n"
Expand Down Expand Up @@ -132,29 +133,40 @@ main(int argc, char ** argv)
bool outFolderPresent = false;
std::string outFolder = "";
std::string logFileName = "";
auto level = elx::log::level::info;

/** Put command line parameters into parameterFileList. */
for (unsigned int i = 1; static_cast<long>(i) < argc - 1; i += 2)
{
std::string key(argv[i]);
std::string value(argv[i + 1]);

if (key == "-out")
if (key == "-loglevel")
{
/** Make sure that last character of the output folder equals a '/' or '\'. */
const char last = value.back();
if (last != '/' && last != '\\')
if (!ToLogLevel(value, level))
{
value.append("/");
// Unsupported log level value.
return EXIT_FAILURE;
}
value = elx::Conversion::ToNativePathNameSeparators(value);

/** Save this information. */
outFolderPresent = true;
outFolder = value;

} // end if key == "-out"

}
else
{
if (key == "-out")
{
/** Make sure that last character of the output folder equals a '/' or '\'. */
const char last = value.back();
if (last != '/' && last != '\\')
{
value.append("/");
}
value = elx::Conversion::ToNativePathNameSeparators(value);

/** Save this information. */
outFolderPresent = true;
outFolder = value;

} // end if key == "-out"
}
/** Attempt to save the arguments in the ArgumentMap. */
if (argMap.count(key) == 0)
{
Expand Down Expand Up @@ -205,7 +217,7 @@ main(int argc, char ** argv)
{
/** Setup the log system. */
logFileName = argMap["-out"] + "transformix.log";
int returndummy2 = elx::log::setup(logFileName, true, true) ? 0 : 1;
int returndummy2 = elx::log::setup(logFileName, true, true, level) ? 0 : 1;
if (returndummy2)
{
std::cerr << "ERROR while setting up the log system." << std::endl;
Expand Down

0 comments on commit c90f1c4

Please sign in to comment.