Uniform "generated by" information in output files.
authorTeemu Murtola <teemu.murtola@gmail.com>
Fri, 5 Jul 2013 02:58:36 +0000 (05:58 +0300)
committerGerrit Code Review <gerrit@gerrit.gromacs.org>
Sun, 21 Jul 2013 12:21:06 +0000 (14:21 +0200)
Extend the printBinaryInformation() function to make it possible to
write the lines in a particular comment format.  Use the function to
print out "Generated by" information in several output formats.  Removed
command_line() from statutil.h as it is no longer used.  Replaced most
uses of Program() with ShortProgram() where the actual path was not
relevant.

Also use printBinaryInformation() to print out information into the
mdrun log file.  Again print the copyright header to the beginning of
the log file.

Related to #1209.

Change-Id: Ibf32de318421a10d39f217517005fea408d19fbf

17 files changed:
src/contrib/do_multiprot.c
src/gromacs/commandline/cmdlinemodulemanager.cpp
src/gromacs/gmxana/gmx_densorder.cpp
src/gromacs/gmxana/gmx_dipoles.cpp
src/gromacs/gmxana/gmx_trjconv.c
src/gromacs/gmxlib/copyrite.cpp
src/gromacs/gmxlib/main.cpp
src/gromacs/gmxlib/matio.cpp
src/gromacs/gmxlib/statutil.cpp
src/gromacs/gmxlib/warninp.c
src/gromacs/gmxlib/xvgr.cpp
src/gromacs/gmxpreprocess/pdb2top.cpp
src/gromacs/legacyheaders/copyrite.h
src/gromacs/legacyheaders/pdb2top.h
src/gromacs/legacyheaders/statutil.h
src/programs/gmx/g_x2top.c
src/programs/gmx/pdb2gmx.c

index 58e7e5db35f4771b4c837f8fa3da36b53d1cb892..3477e9e5a8c79d3a903787fcda4f861336bfaaff 100644 (file)
@@ -354,7 +354,7 @@ int main(int argc,char *argv[])
        }
        if (outftp == efG87)
            fprintf(gmx_fio_getfp(trx_get_fileio(trxout)),"Generated by %s. #atoms=%d, a BOX is"
-                   " stored in this file.\n",Program(),nout);   
+                   " stored in this file.\n",ShortProgram(),nout);
     }
     
     if (natoms > atoms->nr) {
index 0f4276de45ec9b959a8b121b551dc5a55cd8018e..c9dd7aa773ec32b7b89a37f47be4c02fa835cf8e 100644 (file)
@@ -544,8 +544,10 @@ int CommandLineModuleManager::run(int argc, char *argv[])
     }
     if (!impl_->bQuiet_)
     {
-        printBinaryInformation(impl_->bStdOutInfo_ ? stdout : stderr,
-                               impl_->programInfo_, impl_->binaryInfoSettings_);
+        FILE *out = (impl_->bStdOutInfo_ ? stdout : stderr);
+        printBinaryInformation(out, impl_->programInfo_,
+                               impl_->binaryInfoSettings_);
+        fprintf(out, "\n");
     }
     if (module == NULL)
     {
index 841b464c20716d3f5a0c18c67390c1054fafe833..bb2da466511575fa167cd1b9c3a14524fcf175b8 100644 (file)
 #include "binsearch.h"
 #include "powerspect.h"
 #include "gmx_ana.h"
+#include "copyrite.h"
+
+#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/programinfo.h"
 
 #ifdef GMX_DOUBLE
 #define FLOOR(x) ((int) floor(x))
@@ -615,10 +619,19 @@ static void writeraw(t_interf ***int1, t_interf ***int2, int tblocks, int xbins,
 
     raw1 = ffopen(fnms[0], "w");
     raw2 = ffopen(fnms[1], "w");
-    fprintf(raw1, "#Produced by: %s #\n", command_line());
-    fprintf(raw2, "#Produced by: %s #\n", command_line());
-    fprintf(raw1, "#Legend: nt nx ny\n#Xbin Ybin Z t\n");
-    fprintf(raw2, "#Legend: nt nx ny\n#Xbin Ybin Z t\n");
+    try
+    {
+        gmx::BinaryInformationSettings settings;
+        settings.generatedByHeader(true);
+        settings.linePrefix("# ");
+        gmx::printBinaryInformation(raw1, gmx::ProgramInfo::getInstance(),
+                                    settings);
+        gmx::printBinaryInformation(raw2, gmx::ProgramInfo::getInstance(),
+                                    settings);
+    }
+    GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
+    fprintf(raw1, "# Legend: nt nx ny\n# Xbin Ybin Z t\n");
+    fprintf(raw2, "# Legend: nt nx ny\n# Xbin Ybin Z t\n");
     fprintf(raw1, "%i %i %i\n", tblocks, xbins, ybins);
     fprintf(raw2, "%i %i %i\n", tblocks, xbins, ybins);
     for (n = 0; n < tblocks; n++)
index 1f0c2edf0693afdad603d9b71670da198e9490a8..733ed899fe176b4dead2f3fd36e0766c9169ba7b 100644 (file)
 #include "nrjac.h"
 #include "matio.h"
 #include "gmx_ana.h"
+#include "copyrite.h"
 
+#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/programinfo.h"
 
 #define e2d(x) ENM2DEBYE*(x)
 #define EANG2CM  E_CHARGE*1.0e-10       /* e Angstrom to Coulomb meter */
@@ -942,9 +945,15 @@ static void do_dip(t_topology *top, int ePBC, real volume,
         ffclose(dip3d);
 
         dip3d = (FILE *)ffopen(fndip3d, "w");
-        fprintf(dip3d, "# This file was created by %s\n", Program());
-        fprintf(dip3d, "# which is part of G R O M A C S:\n");
-        fprintf(dip3d, "#\n");
+        try
+        {
+            gmx::BinaryInformationSettings settings;
+            settings.generatedByHeader(true);
+            settings.linePrefix("# ");
+            gmx::printBinaryInformation(dip3d, gmx::ProgramInfo::getInstance(),
+                                        settings);
+        }
+        GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
     }
 
     /* Write legends to all the files */
index ab64f46b1a1b8abdfce56982db3c8dc27afa0294..2505a937dce61ea0a319cd5254136d6336c948dc 100644 (file)
@@ -1327,7 +1327,7 @@ int gmx_trjconv(int argc, char *argv[])
             {
                 fprintf(gmx_fio_getfp(trx_get_fileio(trxout)),
                         "Generated by %s. #atoms=%d, a BOX is"
-                        " stored in this file.\n", Program(), nout);
+                        " stored in this file.\n", ShortProgram(), nout);
             }
 
             /* Start the big loop over frames */
index 96137542ba09ca4647200709b13dcab30206ccb9..f91355b2215f369de06aef51c2a1b3da2b6dd20a 100644 (file)
@@ -59,6 +59,7 @@
 #include "gromacs/legacyheaders/vec.h"
 
 #include "gromacs/fft/fft.h"
+#include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/programinfo.h"
 
 #include "buildinfo.h"
@@ -598,9 +599,9 @@ const char *GromacsVersion()
     return _gmx_ver_string;
 }
 
-void gmx_print_version_info_gpu(FILE *fp);
+extern void gmx_print_version_info_gpu(FILE *fp);
 
-void gmx_print_version_info(FILE *fp)
+static void gmx_print_version_info(FILE *fp)
 {
     fprintf(fp, "Gromacs version:    %s\n", _gmx_ver_string);
 #ifdef GMX_GIT_VERSION_INFO
@@ -686,7 +687,8 @@ namespace gmx
 {
 
 BinaryInformationSettings::BinaryInformationSettings()
-    : bExtendedInfo_(false), bCopyright_(false)
+    : bExtendedInfo_(false), bCopyright_(false),
+      bGeneratedByHeader_(false), prefix_(""), suffix_("")
 {
 }
 
@@ -698,26 +700,37 @@ void printBinaryInformation(FILE *fp, const ProgramInfo &programInfo)
 void printBinaryInformation(FILE *fp, const ProgramInfo &programInfo,
                             const BinaryInformationSettings &settings)
 {
+    const char *prefix          = settings.prefix_;
+    const char *suffix          = settings.suffix_;
     const char *precisionString = "";
 #ifdef GMX_DOUBLE
     precisionString = " (double precision)";
 #endif
     const std::string &name = programInfo.programName();
-    fprintf(fp, "GROMACS:    %s, %s%s\n", name.c_str(),
-            GromacsVersion(), precisionString);
-    fprintf(fp, "Executable: %s\n", programInfo.programNameWithPath().c_str());
-    fprintf(fp, "Command line:\n  %s\n", programInfo.commandLine().c_str());
+    if (settings.bGeneratedByHeader_)
+    {
+        fprintf(fp, "%sCreated by:%s\n", prefix, suffix);
+    }
+    fprintf(fp, "%sGROMACS:    %s, %s%s%s\n", prefix, name.c_str(),
+            GromacsVersion(), precisionString, suffix);
+    fprintf(fp, "%sExecutable: %s%s\n", prefix,
+            programInfo.programNameWithPath().c_str(), suffix);
+    fprintf(fp, "%sCommand line:%s\n%s  %s%s\n",
+            prefix, suffix, prefix, programInfo.commandLine().c_str(), suffix);
     if (settings.bCopyright_)
     {
+        GMX_RELEASE_ASSERT(prefix[0] == '\0' && suffix[0] == '\0',
+                           "Prefix/suffix not supported with copyright");
         fprintf(fp, "\n");
         CopyRight(fp);
     }
     if (settings.bExtendedInfo_)
     {
+        GMX_RELEASE_ASSERT(prefix[0] == '\0' && suffix[0] == '\0',
+                           "Prefix/suffix not supported with extended info");
         fprintf(fp, "\n");
         gmx_print_version_info(fp);
     }
-    fprintf(fp, "\n");
 }
 
 } // namespace gmx
index 1784c29ab1fea2a4dbea7041fc7aff5a45d706d3..bde720f9e2e25272fac877a895afde26afeb8904 100644 (file)
@@ -63,6 +63,9 @@
 #include "thread_mpi.h"
 #endif
 
+#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/programinfo.h"
+
 /* The source code in this file should be thread-safe.
          Please keep it that way. */
 
@@ -332,7 +335,14 @@ void gmx_log_open(const char *lognm, const t_commrec *cr, gmx_bool bMasterOnly,
             "Log file opened on %s"
             "Host: %s  pid: %d  nodeid: %d  nnodes:  %d\n",
             timebuf, host, pid, cr->nodeid, cr->nnodes);
-    gmx_print_version_info(fp);
+    try
+    {
+        gmx::BinaryInformationSettings settings;
+        settings.extendedInfo(true);
+        settings.copyright(!bAppendFiles);
+        gmx::printBinaryInformation(fp, gmx::ProgramInfo::getInstance(), settings);
+    }
+    GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
     fprintf(fp, "\n\n");
 
     fflush(fp);
index 7ab142ba7bfde8974b542e98e50b80b7c51b99b2..ef8b1e79686a85a19ee249bf5ab2785d0dcf0b25 100644 (file)
 #include "statutil.h"
 #include "gmxfio.h"
 #include "maths.h"
+#include "copyrite.h"
+
+#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/programinfo.h"
 
 #define round(a) (int)(a+0.5)
 
@@ -645,7 +649,16 @@ void write_xpm_header(FILE *out,
                       gmx_bool bDiscrete)
 {
     fprintf(out,  "/* XPM */\n");
-    fprintf(out,  "/* Generated by %s */\n", Program());
+    try
+    {
+        gmx::BinaryInformationSettings settings;
+        settings.generatedByHeader(true);
+        settings.linePrefix("/* ");
+        settings.lineSuffix(" */");
+        gmx::printBinaryInformation(out, gmx::ProgramInfo::getInstance(),
+                                    settings);
+    }
+    GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
     fprintf(out,  "/* This file can be converted to EPS by the GROMACS program xpm2ps */\n");
     fprintf(out,  "/* title:   \"%s\" */\n", title);
     fprintf(out,  "/* legend:  \"%s\" */\n", legend);
index 28d5590a40f45df7eb97f47003a69132538847e5..5e554d1885f82bd39f736c7519f51363c634a38d 100644 (file)
@@ -105,15 +105,6 @@ const char *Program(void)
     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
 }
 
-const char *command_line(void)
-{
-    try
-    {
-        return gmx::ProgramInfo::getInstance().commandLine().c_str();
-    }
-    GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
-}
-
 /* utility functions */
 
 gmx_bool bRmod_fd(double a, double b, double c, gmx_bool bDouble)
index 3c0750a449007805c021fa2ef5217ccc9cc6ae0c..7e32d8272370f6bde1d299f48ed62d36e5e45b3f 100644 (file)
@@ -191,7 +191,7 @@ void done_warning(warninp_t wi, int f_errno, const char *file, int line)
         gmx_fatal(f_errno, file, line,
                   "Too many warnings (%d), %s terminated.\n"
                   "If you are sure all warnings are harmless, use the -maxwarn option.",
-                  wi->nwarn_warn, Program());
+                  wi->nwarn_warn, ShortProgram());
     }
 
     sfree(wi);
index 4dff38347927bf53304fcf59d89bce61c3eebd32..39d08cafb95922ec4a34b98376af8b7636fde3d5 100644 (file)
@@ -56,6 +56,9 @@
 #include "vec.h"
 #include "gmxfio.h"
 
+#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/programinfo.h"
+
 gmx_bool output_env_get_print_xvgr_codes(const output_env_t oenv)
 {
     int xvg_format;
@@ -243,7 +246,15 @@ void xvgr_header(FILE *fp, const char *title, const char *xaxis,
         time(&t);
         gmx_ctime_r(&t, buf, STRLEN);
         fprintf(fp, "# This file was created %s", buf);
-        fprintf(fp, "# by the following command:\n# %s\n#\n", command_line());
+        try
+        {
+            gmx::BinaryInformationSettings settings;
+            settings.generatedByHeader(true);
+            settings.linePrefix("# ");
+            gmx::printBinaryInformation(fp, gmx::ProgramInfo::getInstance(),
+                                        settings);
+        }
+        GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
         fprintf(fp, "# %s is part of G R O M A C S:\n#\n", ShortProgram());
         bromacs(pukestr, 99);
         fprintf(fp, "# %s\n#\n", pukestr);
index b4815c43f5de13c3ea396f38fddc79ed71df510e..971755755e40bd80d5804f9bfcef60356c6a9c4b 100644 (file)
 #include "toputil.h"
 #include "fflibutil.h"
 #include "strdb.h"
+#include "copyrite.h"
+
+#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/programinfo.h"
 
 /* this must correspond to enum in pdb2top.h */
 const char *hh[ehisNR]   = { "HISD", "HISE", "HISH", "HIS1" };
@@ -543,7 +547,6 @@ static void print_top_heavy_H(FILE *out, real mHmult)
 
 void print_top_comment(FILE       *out,
                        const char *filename,
-                       const char *generator,
                        const char *ffdir,
                        gmx_bool    bITP)
 {
@@ -552,9 +555,15 @@ void print_top_comment(FILE       *out,
 
     nice_header(out, filename);
     fprintf(out, ";\tThis is a %s topology file\n;\n", bITP ? "include" : "standalone");
-    fprintf(out, ";\tIt was generated using program:\n;\t%s\n;\n",
-            (NULL == generator) ? "unknown" : generator);
-    fprintf(out, ";\tCommand line was:\n;\t%s\n;\n", command_line());
+    try
+    {
+        gmx::BinaryInformationSettings settings;
+        settings.generatedByHeader(true);
+        settings.linePrefix(";\t");
+        gmx::printBinaryInformation(out, gmx::ProgramInfo::getInstance(),
+                                    settings);
+    }
+    GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
 
     if (strchr(ffdir, '/') == NULL)
     {
@@ -585,11 +594,11 @@ void print_top_comment(FILE       *out,
 }
 
 void print_top_header(FILE *out, const char *filename,
-                      const char *title, gmx_bool bITP, const char *ffdir, real mHmult)
+                      gmx_bool bITP, const char *ffdir, real mHmult)
 {
     const char *p;
 
-    print_top_comment(out, filename, title, ffdir, bITP);
+    print_top_comment(out, filename, ffdir, bITP);
 
     print_top_heavy_H(out, mHmult);
     fprintf(out, "; Include forcefield parameters\n");
index a7075520c80023c0c3520b419cde5c150b75cc54..daf13eb8e9cab44eae9d63a93869445a0c14d839 100644 (file)
@@ -44,9 +44,6 @@ extern "C" {
 /* Has to be a function, so we can get version number from autoconf */
 const char *GromacsVersion(void);
 
-void
-gmx_print_version_info(FILE *fp);
-
 /* For both bromacs() and cool_quote() you have to provide a pointer to
  * a string of reasonable length (say 256) and the string length. This
  * is necessary to make the routines threadsafe and avoid allocating
@@ -98,10 +95,28 @@ class BinaryInformationSettings
             bCopyright_ = bEnabled;
             return *this;
         }
+        BinaryInformationSettings &generatedByHeader(bool bEnabled)
+        {
+            bGeneratedByHeader_ = bEnabled;
+            return *this;
+        }
+        BinaryInformationSettings &linePrefix(const char *prefix)
+        {
+            prefix_ = prefix;
+            return *this;
+        }
+        BinaryInformationSettings &lineSuffix(const char *suffix)
+        {
+            suffix_ = suffix;
+            return *this;
+        }
 
     private:
         bool        bExtendedInfo_;
         bool        bCopyright_;
+        bool        bGeneratedByHeader_;
+        const char *prefix_;
+        const char *suffix_;
 
         //! Needed to read the members without otherwise unnecessary accessors.
         friend void printBinaryInformation(FILE *fp, const ProgramInfo &programInfo,
index 6f6fc9c14a0ea2a1016bfbb052a9708d2c443d88..5947db7441eda4a15df1fce0a15ef147d51be143 100644 (file)
@@ -91,9 +91,9 @@ void match_atomnames_with_rtp(t_restp restp[], t_hackblock hb[],
  * add these atoms to restp.
  */
 
-void print_top_comment(FILE *out, const char *filename, const char *generator, const char *ffdir, gmx_bool bITP);
+void print_top_comment(FILE *out, const char *filename, const char *ffdir, gmx_bool bITP);
 
-void print_top_header(FILE *out, const char *filename, const char *title, gmx_bool bITP,
+void print_top_header(FILE *out, const char *filename, gmx_bool bITP,
                       const char *ffdir, real mHmult);
 
 void print_top_mols(FILE *out,
index 1cb66a27909f1b69d5eb6004bf8f66d00f7b302e..0751fec668f67185787c3046c1af838c04069710 100644 (file)
@@ -81,9 +81,6 @@ typedef struct t_trxstatus t_trxstatus;
    global informaion, their existence in a multi-threaded environment is not
    a real problem. */
 
-/* Return the name of the program */
-const char *command_line(void);
-
 /* set the program name to the provided string, but note
  * that it must be a real file - we determine the library
  * directory from its location!
index e9db6949b3d54c8202bccef2dc494b9c728b6785..eb3f8438939f572dd39236d97cf577b5e6198b8a 100644 (file)
@@ -606,8 +606,7 @@ int gmx_x2top(int argc, char *argv[])
     if (bTOP)
     {
         fp = ftp2FILE(efTOP, NFILE, fnm, "w");
-        print_top_header(fp, ftp2fn(efTOP, NFILE, fnm),
-                         "Generated by x2top", TRUE, ffdir, 1.0);
+        print_top_header(fp, ftp2fn(efTOP, NFILE, fnm), TRUE, ffdir, 1.0);
 
         write_top(fp, NULL, mymol.name, atoms, FALSE, bts, plist, excls, atype,
                   cgnr, rtp_header_settings.nrexcl);
@@ -628,7 +627,8 @@ int gmx_x2top(int argc, char *argv[])
     close_symtab(&symtab);
     free(mymol.name);
 
-    printf("\nWARNING: topologies generated by %s can not be trusted at face value.\n", Program());
+    printf("\nWARNING: topologies generated by %s can not be trusted at face value.\n",
+           ShortProgram());
     printf("         Please verify atomtypes and charges by comparison to other\n");
     printf("         topologies.\n");
 
index c3fa5e7a8fc4eaee9a7dab704e23d4574af391b9..59b1549c46ca04926339922dbed64fe1974736b6 100644 (file)
@@ -1229,7 +1229,7 @@ int gmx_pdb2gmx(int argc, char *argv[])
     gmx_residuetype_t rt;
     const char       *top_fn;
     char              fn[256], itp_fn[STRLEN], posre_fn[STRLEN], buf_fn[STRLEN];
-    char              molname[STRLEN], title[STRLEN], quote[STRLEN], generator[STRLEN];
+    char              molname[STRLEN], title[STRLEN], quote[STRLEN];
     char             *c, forcefield[STRLEN], ffdir[STRLEN];
     char              ffname[STRLEN], suffix[STRLEN], buf[STRLEN];
     char             *watermodel;
@@ -1751,9 +1751,7 @@ int gmx_pdb2gmx(int argc, char *argv[])
     top_fn   = ftp2fn(efTOP, NFILE, fnm);
     top_file = gmx_fio_fopen(top_fn, "w");
 
-    sprintf(generator, "%s - %s", ShortProgram(), GromacsVersion() );
-
-    print_top_header(top_file, top_fn, generator, FALSE, ffdir, mHmult);
+    print_top_header(top_file, top_fn, FALSE, ffdir, mHmult);
 
     nincl = 0;
     nmol  = 0;
@@ -2074,7 +2072,7 @@ int gmx_pdb2gmx(int argc, char *argv[])
 
         if (bITP)
         {
-            print_top_comment(itp_file, itp_fn, generator, ffdir, TRUE);
+            print_top_comment(itp_file, itp_fn, ffdir, TRUE);
         }
 
         if (cc->bAllWat)