Merge release-5-0 into master
[alexxy/gromacs.git] / src / gromacs / commandline / cmdlinemodulemanager.cpp
index 01d9fa7401cd23045722dbaede233a6edf66251c..a32903f51650a1313a61db897bfd3dc19ad69618 100644 (file)
@@ -47,7 +47,6 @@
 #include <utility>
 
 #include "gromacs/legacyheaders/copyrite.h"
-#include "gromacs/legacyheaders/network.h"
 
 #include "gromacs/commandline/cmdlinehelpcontext.h"
 #include "gromacs/commandline/cmdlinehelpmodule.h"
@@ -58,7 +57,9 @@
 #include "gromacs/commandline/cmdlineprogramcontext.h"
 #include "gromacs/options/basicoptions.h"
 #include "gromacs/options/options.h"
+#include "gromacs/utility/basenetwork.h"
 #include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/stringutil.h"
 
@@ -146,7 +147,7 @@ class CMainCommandLineModule : public CommandLineModuleInterface
 
 CommandLineCommonOptionsHolder::CommandLineCommonOptionsHolder()
     : options_(NULL, NULL), bHelp_(false), bHidden_(false),
-      bQuiet_(false), bVersion_(false), bCopyright_(true)
+      bQuiet_(false), bVersion_(false), bCopyright_(true), debugLevel_(0)
 {
     binaryInfoSettings_.copyright(true);
 }
@@ -168,6 +169,10 @@ void CommandLineCommonOptionsHolder::initOptions()
                            .description("Print extended version information and quit"));
     options_.addOption(BooleanOption("copyright").store(&bCopyright_)
                            .description("Print copyright information on startup"));
+    options_.addOption(IntegerOption("debug").store(&debugLevel_)
+                           .hidden().defaultValueIfSet(1)
+                           .description("Write file with debug information, "
+                                        "1: short (default), 2: also x and f"));
 }
 
 bool CommandLineCommonOptionsHolder::finishOptions()
@@ -512,7 +517,7 @@ void CommandLineModuleManager::addHelpTopic(HelpTopicPointer topic)
 int CommandLineModuleManager::run(int argc, char *argv[])
 {
     CommandLineModuleInterface    *module;
-    const bool                     bMaster = (!gmx_mpi_initialized() || gmx_node_rank() == 0);
+    const bool                     bMaster = (gmx_node_rank() == 0);
     bool                           bQuiet  = impl_->bQuiet_ || !bMaster;
     CommandLineCommonOptionsHolder optionsHolder;
     try
@@ -542,7 +547,24 @@ int CommandLineModuleManager::run(int argc, char *argv[])
     {
         return 0;
     }
-    int rc = module->run(argc, argv);
+    /* Open the debug file */
+    if (optionsHolder.debugLevel() > 0)
+    {
+        std::string filename(impl_->programContext_.programName());
+        if (gmx_node_num() > 1)
+        {
+            filename.append(formatString("%d", gmx_node_rank()));
+        }
+        filename.append(".debug");
+
+        fprintf(stderr, "Will write debug log file: %s\n", filename.c_str());
+        gmx_init_debug(optionsHolder.debugLevel(), filename.c_str());
+    }
+    int rc = 0;
+    if (!(module == impl_->helpModule_ && !bMaster))
+    {
+        rc = module->run(argc, argv);
+    }
     if (!bQuiet)
     {
         gmx_thanx(stderr);