Removed some direct output to stderr.
authorTeemu Murtola <teemu.murtola@cbr.su.se>
Sun, 25 Sep 2011 18:11:14 +0000 (21:11 +0300)
committerTeemu Murtola <teemu.murtola@gmail.com>
Sat, 3 Dec 2011 19:05:20 +0000 (21:05 +0200)
Functions that used to print information directly to stderr now take a
FILE object such that the caller can decide where the information goes.
The caller for now still always prints to stderr, but this reduces the
number of direct references to stderr. Remaining references to stderr in
the selection module are either for 1) part of interactive input, 2)
debug output, or 3) rare error conditions that require some thought on
how to handle.

Related to issue #655.

Change-Id: If0d7b6c5ba36ad64f97ff2cfac8f7ab47b4d5a27

src/gromacs/selection/indexutil.cpp
src/gromacs/selection/indexutil.h
src/gromacs/selection/parsetree.cpp
src/gromacs/selection/selection.cpp
src/gromacs/selection/selection.h
src/gromacs/selection/selhelp.cpp
src/gromacs/selection/selhelp.h
src/gromacs/trajectoryanalysis/tests/test_selection.cpp

index bb1bc048e927b68291297d9b8b83d4b14c0cfa77..02f284804f40b5ec052d5f65e7b8e5aa2f9f321d 100644 (file)
@@ -331,19 +331,20 @@ gmx_ana_indexgrps_find(gmx_ana_index_t *dest, gmx_ana_indexgrps_t *src, char *na
 }
 
 /*!
+ * \param[in]  fp     Where to print the output.
  * \param[in]  g      Index groups to print.
  * \param[in]  maxn   Maximum number of indices to print
  *      (-1 = print all, 0 = print only names).
  */
 void
-gmx_ana_indexgrps_print(gmx_ana_indexgrps_t *g, int maxn)
+gmx_ana_indexgrps_print(FILE *fp, gmx_ana_indexgrps_t *g, int maxn)
 {
     int  i;
 
     for (i = 0; i < g->nr; ++i)
     {
-        fprintf(stderr, " %2d: ", i);
-        gmx_ana_index_dump(&g->g[i], i, maxn);
+        fprintf(fp, " %2d: ", i);
+        gmx_ana_index_dump(fp, &g->g[i], i, maxn);
     }
 }
 
@@ -480,27 +481,28 @@ gmx_ana_index_copy(gmx_ana_index_t *dest, gmx_ana_index_t *src, gmx_bool bAlloc)
 }
 
 /*!
+ * \param[in]  fp     Where to print the output.
  * \param[in]  g      Index group to print.
  * \param[in]  i      Group number to use if the name is NULL.
  * \param[in]  maxn   Maximum number of indices to print (-1 = print all).
  */
 void
-gmx_ana_index_dump(gmx_ana_index_t *g, int i, int maxn)
+gmx_ana_index_dump(FILE *fp, gmx_ana_index_t *g, int i, int maxn)
 {
     int  j, n;
 
     if (g->name)
     {
-        fprintf(stderr, "\"%s\"", g->name);
+        fprintf(fp, "\"%s\"", g->name);
     }
     else
     {
-        fprintf(stderr, "Group %d", i+1);
+        fprintf(fp, "Group %d", i+1);
     }
-    fprintf(stderr, " (%d atoms)", g->isize);
+    fprintf(fp, " (%d atoms)", g->isize);
     if (maxn != 0)
     {
-        fprintf(stderr, ":");
+        fprintf(fp, ":");
         n = g->isize;
         if (maxn >= 0 && n > maxn)
         {
@@ -508,14 +510,14 @@ gmx_ana_index_dump(gmx_ana_index_t *g, int i, int maxn)
         }
         for (j = 0; j < n; ++j)
         {
-            fprintf(stderr, " %d", g->index[j]+1);
+            fprintf(fp, " %d", g->index[j]+1);
         }
         if (n < g->isize)
         {
-            fprintf(stderr, " ...");
+            fprintf(fp, " ...");
         }
     }
-    fprintf(stderr, "\n");
+    fprintf(fp, "\n");
 }
 
 /*!
index 90d50f9c058bdd6b189ba7f00088551237a0c271..b7b666835632eaff879f5cde49f5c912253875c5 100644 (file)
@@ -233,7 +233,7 @@ gmx_ana_indexgrps_find(gmx_ana_index_t *dest, gmx_ana_indexgrps_t *src, char *na
 
 /** Writes out a list of index groups. */
 void
-gmx_ana_indexgrps_print(gmx_ana_indexgrps_t *g, int maxn);
+gmx_ana_indexgrps_print(FILE *fp, gmx_ana_indexgrps_t *g, int maxn);
 /*@}*/
 
 /*! \name Functions for handling gmx_ana_index_t
@@ -264,7 +264,7 @@ gmx_ana_index_copy(gmx_ana_index_t *dest, gmx_ana_index_t *src, gmx_bool bAlloc)
 
 /** Writes out the contents of a index group. */
 void
-gmx_ana_index_dump(gmx_ana_index_t *g, int i, int maxn);
+gmx_ana_index_dump(FILE *fp, gmx_ana_index_t *g, int i, int maxn);
 
 /** Checks whether all indices are between 0 and \p natoms. */
 void
index f7570818ce196293dbe37e7c08659920d6a84c12..f2929a4cfa5956b251343e857ca43a92dd957a17 100644 (file)
@@ -1335,7 +1335,7 @@ _gmx_sel_handle_empty_cmd(yyscan_t scanner)
     if (grps)
     {
         fprintf(stderr, "Available index groups:\n");
-        gmx_ana_indexgrps_print(_gmx_sel_lexer_indexgrps(scanner), 0);
+        gmx_ana_indexgrps_print(stderr, _gmx_sel_lexer_indexgrps(scanner), 0);
     }
     if (sc->nvars > 0 || !sc->sel.empty())
     {
@@ -1363,7 +1363,7 @@ _gmx_sel_handle_help_cmd(char *topic, yyscan_t scanner)
 {
     gmx_ana_selcollection_t *sc = _gmx_sel_lexer_selcollection(scanner);
 
-    _gmx_sel_print_help(sc->symtab, topic);
+    _gmx_sel_print_help(stderr, sc->symtab, topic);
     if (topic)
     {
         sfree(topic);
index db66901bb735649f9e3959de0d9a3480ad23facc..d09f88685d29c05c98dd4d6de8111f6065b76fac 100644 (file)
@@ -128,13 +128,13 @@ Selection::~Selection()
 
 
 void
-Selection::printInfo() const
+Selection::printInfo(FILE *fp) const
 {
-    fprintf(stderr, "\"%s\" (%d position%s, %d atom%s%s)", _sel.name,
+    fprintf(fp, "\"%s\" (%d position%s, %d atom%s%s)", _sel.name,
             _sel.p.nr,     _sel.p.nr     == 1 ? "" : "s",
             _sel.g->isize, _sel.g->isize == 1 ? "" : "s",
             _sel.bDynamic ? ", dynamic" : "");
-    fprintf(stderr, "\n");
+    fprintf(fp, "\n");
 }
 
 
@@ -164,17 +164,17 @@ Selection::initCoveredFraction(e_coverfrac_t type)
 
 
 void
-Selection::printDebugInfo(int nmaxind) const
+Selection::printDebugInfo(FILE *fp, int nmaxind) const
 {
-    fprintf(stderr, "  ");
-    printInfo();
-    fprintf(stderr, "    ");
-    gmx_ana_index_dump(_sel.g, -1, nmaxind);
+    fprintf(fp, "  ");
+    printInfo(fp);
+    fprintf(fp, "    ");
+    gmx_ana_index_dump(fp, _sel.g, -1, nmaxind);
 
-    fprintf(stderr, "    Block (size=%d):", _sel.p.m.mapb.nr);
+    fprintf(fp, "    Block (size=%d):", _sel.p.m.mapb.nr);
     if (!_sel.p.m.mapb.index)
     {
-        fprintf(stderr, " (null)");
+        fprintf(fp, " (null)");
     }
     else
     {
@@ -182,42 +182,42 @@ Selection::printDebugInfo(int nmaxind) const
         if (nmaxind >= 0 && n > nmaxind)
             n = nmaxind;
         for (int i = 0; i <= n; ++i)
-            fprintf(stderr, " %d", _sel.p.m.mapb.index[i]);
+            fprintf(fp, " %d", _sel.p.m.mapb.index[i]);
         if (n < _sel.p.m.mapb.nr)
-            fprintf(stderr, " ...");
+            fprintf(fp, " ...");
     }
-    fprintf(stderr, "\n");
+    fprintf(fp, "\n");
 
     int n = _sel.p.m.nr;
     if (nmaxind >= 0 && n > nmaxind)
         n = nmaxind;
-    fprintf(stderr, "    RefId:");
+    fprintf(fp, "    RefId:");
     if (!_sel.p.m.refid)
     {
-        fprintf(stderr, " (null)");
+        fprintf(fp, " (null)");
     }
     else
     {
         for (int i = 0; i < n; ++i)
-            fprintf(stderr, " %d", _sel.p.m.refid[i]);
+            fprintf(fp, " %d", _sel.p.m.refid[i]);
         if (n < _sel.p.m.nr)
-            fprintf(stderr, " ...");
+            fprintf(fp, " ...");
     }
-    fprintf(stderr, "\n");
+    fprintf(fp, "\n");
 
-    fprintf(stderr, "    MapId:");
+    fprintf(fp, "    MapId:");
     if (!_sel.p.m.mapid)
     {
-        fprintf(stderr, " (null)");
+        fprintf(fp, " (null)");
     }
     else
     {
         for (int i = 0; i < n; ++i)
-            fprintf(stderr, " %d", _sel.p.m.mapid[i]);
+            fprintf(fp, " %d", _sel.p.m.mapid[i]);
         if (n < _sel.p.m.nr)
-            fprintf(stderr, " ...");
+            fprintf(fp, " ...");
     }
-    fprintf(stderr, "\n");
+    fprintf(fp, "\n");
 }
 
 } // namespace gmx
index c2d756c950d81bbeacdede0b6f29d270e6271aa3..f8cfdb389118fa9093a31fba0a322903c3392d2d 100644 (file)
@@ -171,15 +171,20 @@ class Selection
          */
         bool initCoveredFraction(e_coverfrac_t type);
 
-        //! Prints out one-line description of the selection.
-        void printInfo() const;
+        /*! \brief
+         * Prints out one-line description of the selection.
+         *
+         * \param[in] fp      Where to print the information.
+         */
+        void printInfo(FILE *fp) const;
         /*! \brief
          * Prints out extended information about the selection for debugging.
          *
+         * \param[in] fp      Where to print the information.
          * \param[in] nmaxind Maximum number of values to print in lists
          *      (-1 = print all).
          */
-        void printDebugInfo(int nmaxind) const;
+        void printDebugInfo(FILE *fp, int nmaxind) const;
 
         gmx_ana_selection_t     _sel;
 
index d7e7a699357cfaf09a6ca8210991644a6dd12e4c..a30465d56f43f8dc98e39200acb479d5c8278d97 100644 (file)
@@ -327,13 +327,14 @@ static const t_selection_help_item helpitems[] = {
 /*! \brief
  * Prints a brief list of keywords (selection methods) available.
  *
+ * \param[in] fp    Where to write the list.
  * \param[in] symtab  Symbol table to use to find available keywords.
  * \param[in] type  Only methods that return this type are printed.
  * \param[in] bMod  If FALSE, \ref SMETH_MODIFIER methods are excluded, otherwise
  *     only them are printed.
  */
 static void
-print_keyword_list(gmx_sel_symtab_t *symtab, e_selvalue_t type,
+print_keyword_list(FILE *fp, gmx_sel_symtab_t *symtab, e_selvalue_t type,
                    gmx_bool bMod)
 {
     gmx_sel_symrec_t *symbol;
@@ -348,22 +349,22 @@ print_keyword_list(gmx_sel_symtab_t *symtab, e_selvalue_t type,
                 || (!bMod && !(method->flags & SMETH_MODIFIER)));
         if (bShow)
         {
-            fprintf(stderr, " %c ",
+            fprintf(fp, " %c ",
                     (method->help.nlhelp > 0 && method->help.help) ? '*' : ' ');
             if (method->help.syntax)
             {
-                fprintf(stderr, "%s\n", method->help.syntax);
+                fprintf(fp, "%s\n", method->help.syntax);
             }
             else
             {
                 const char *symname = _gmx_sel_sym_name(symbol);
 
-                fprintf(stderr, "%s", symname);
+                fprintf(fp, "%s", symname);
                 if (strcmp(symname, method->name) != 0)
                 {
-                    fprintf(stderr, " (synonym for %s)", method->name);
+                    fprintf(fp, " (synonym for %s)", method->name);
                 }
-                fprintf(stderr, "\n");
+                fprintf(fp, "\n");
             }
         }
         symbol = _gmx_sel_next_symbol(symbol, SYMBOL_METHOD);
@@ -371,14 +372,15 @@ print_keyword_list(gmx_sel_symtab_t *symtab, e_selvalue_t type,
 }
 
 /*!
- * \param[in]   symtab  Symbol table to use to find available keywords.
+ * \param[in]  fp      Where to write the help.
+ * \param[in]  symtab  Symbol table to use to find available keywords.
  * \param[in]  topic Topic to print help on, or NULL for general help.
  *
- * \p sc is used to get information on which keywords are available in the
+ * \p symtab is used to get information on which keywords are available in the
  * present context.
  */
 void
-_gmx_sel_print_help(gmx_sel_symtab_t *symtab, const char *topic)
+_gmx_sel_print_help(FILE *fp, gmx_sel_symtab_t *symtab, const char *topic)
 {
     const t_selection_help_item *item = NULL;
     size_t i;
@@ -393,10 +395,10 @@ _gmx_sel_print_help(gmx_sel_symtab_t *symtab, const char *topic)
         for (i = 0; i < asize(helpitems); ++i)
         {
             item = &helpitems[i];
-            _gmx_sel_print_help(symtab, item->topic);
+            _gmx_sel_print_help(fp, symtab, item->topic);
             if (i != asize(helpitems) - 1)
             {
-                fprintf(stderr, "\n\n");
+                fprintf(fp, "\n\n");
             }
         }
         return;
@@ -425,25 +427,25 @@ _gmx_sel_print_help(gmx_sel_symtab_t *symtab, const char *topic)
             if (method->help.nlhelp > 0 && method->help.help
                 && strncmp(method->name, topic, strlen(topic)) == 0)
             {
-                print_tty_formatted(stderr, method->help.nlhelp,
+                print_tty_formatted(fp, method->help.nlhelp,
                         method->help.help, 0, NULL, NULL, FALSE);
                 return;
             }
             symbol = _gmx_sel_next_symbol(symbol, SYMBOL_METHOD);
         }
 
-        fprintf(stderr, "No help available for '%s'.\n", topic);
+        fprintf(fp, "No help available for '%s'.\n", topic);
         return;
     }
     /* Print the help */
-    print_tty_formatted(stderr, item->nl, item->text, 0, NULL, NULL, FALSE);
+    print_tty_formatted(fp, item->nl, item->text, 0, NULL, NULL, FALSE);
     /* Special handling of certain pages */
     if (!topic)
     {
         int len = 0;
 
         /* Print the subtopics on the main page */
-        fprintf(stderr, "\nAvailable subtopics:\n");
+        fprintf(fp, "\nAvailable subtopics:\n");
         for (i = 1; i < asize(helpitems); ++i)
         {
             int len1 = strlen(helpitems[i].topic) + 2;
@@ -451,37 +453,37 @@ _gmx_sel_print_help(gmx_sel_symtab_t *symtab, const char *topic)
             len += len1;
             if (len > 79)
             {
-                fprintf(stderr, "\n");
+                fprintf(fp, "\n");
                 len = len1;
             }
-            fprintf(stderr, "  %s", helpitems[i].topic);
+            fprintf(fp, "  %s", helpitems[i].topic);
         }
-        fprintf(stderr, "\n");
+        fprintf(fp, "\n");
     }
     else if (strcmp(item->topic, "keywords") == 0)
     {
         /* Print the list of keywords */
-        fprintf(stderr, "\nKeywords that select atoms by an integer property:\n");
-        fprintf(stderr, "(use in expressions or like \"atomnr 1 to 5 7 9\")\n");
-        print_keyword_list(symtab, INT_VALUE, FALSE);
+        fprintf(fp, "\nKeywords that select atoms by an integer property:\n");
+        fprintf(fp, "(use in expressions or like \"atomnr 1 to 5 7 9\")\n");
+        print_keyword_list(fp, symtab, INT_VALUE, FALSE);
 
-        fprintf(stderr, "\nKeywords that select atoms by a numeric property:\n");
-        fprintf(stderr, "(use in expressions or like \"occupancy 0.5 to 1\")\n");
-        print_keyword_list(symtab, REAL_VALUE, FALSE);
+        fprintf(fp, "\nKeywords that select atoms by a numeric property:\n");
+        fprintf(fp, "(use in expressions or like \"occupancy 0.5 to 1\")\n");
+        print_keyword_list(fp, symtab, REAL_VALUE, FALSE);
 
-        fprintf(stderr, "\nKeywords that select atoms by a string property:\n");
-        fprintf(stderr, "(use like \"name PATTERN [PATTERN] ...\")\n");
-        print_keyword_list(symtab, STR_VALUE, FALSE);
+        fprintf(fp, "\nKeywords that select atoms by a string property:\n");
+        fprintf(fp, "(use like \"name PATTERN [PATTERN] ...\")\n");
+        print_keyword_list(fp, symtab, STR_VALUE, FALSE);
 
-        fprintf(stderr, "\nAdditional keywords that directly select atoms:\n");
-        print_keyword_list(symtab, GROUP_VALUE, FALSE);
+        fprintf(fp, "\nAdditional keywords that directly select atoms:\n");
+        print_keyword_list(fp, symtab, GROUP_VALUE, FALSE);
 
-        fprintf(stderr, "\nKeywords that directly evaluate to positions:\n");
-        fprintf(stderr, "(see also \"help positions\")\n");
-        print_keyword_list(symtab, POS_VALUE, FALSE);
+        fprintf(fp, "\nKeywords that directly evaluate to positions:\n");
+        fprintf(fp, "(see also \"help positions\")\n");
+        print_keyword_list(fp, symtab, POS_VALUE, FALSE);
 
-        fprintf(stderr, "\nAdditional keywords:\n");
-        print_keyword_list(symtab, POS_VALUE, TRUE);
-        print_keyword_list(symtab, NO_VALUE, TRUE);
+        fprintf(fp, "\nAdditional keywords:\n");
+        print_keyword_list(fp, symtab, POS_VALUE, TRUE);
+        print_keyword_list(fp, symtab, NO_VALUE, TRUE);
     }
 }
index 1072d0b27751f76fcc95f27474b186d97a1f146f..a032c836ba3078841ea79aa0cd540a97021e9ed7 100644 (file)
 #ifndef GMX_SELECTION_HELP_H
 #define GMX_SELECTION_HELP_H
 
+#include <stdio.h>
+
 struct gmx_sel_symtab_t;
 
 /** Prints help for writing selections. */
 void
-_gmx_sel_print_help(struct gmx_sel_symtab_t *symtab, const char *topic);
+_gmx_sel_print_help(FILE *fp, struct gmx_sel_symtab_t *symtab, const char *topic);
 
 #endif
index f73aa283636aa873e5ee3edcf23ae618c2c7110c..df07271d1f2e877399f819ffb7b4ce5bb4eb2375 100644 (file)
@@ -88,7 +88,7 @@ SelectionTester::printSelections()
     fprintf(stderr, "\nSelections:\n");
     for (size_t g = 0; g < _selections.size(); ++g)
     {
-        _selections[g]->printDebugInfo(_nmaxind);
+        _selections[g]->printDebugInfo(stderr, _nmaxind);
     }
     fprintf(stderr, "\n");
 }
@@ -129,7 +129,7 @@ SelectionTester::analyzeFrame(int /*frnr*/, const t_trxframe &/*fr*/, t_pbc * /*
     {
         const Selection *sel = _selections[g];
 
-        gmx_ana_index_dump(sel->indexGroup(), g, _nmaxind);
+        gmx_ana_index_dump(stderr, sel->indexGroup(), g, _nmaxind);
         fprintf(stderr, "  Positions (%d pcs):\n", sel->posCount());
         n = sel->posCount();
         if (_nmaxind >= 0 && n > _nmaxind)