Refactor SD update
[gromacs.git] / src / gromacs / fileio / warninp.cpp
blob0f18418de7c7ec42f466d2711a5fd12edad1d270
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "gmxpre.h"
39 #include "warninp.h"
41 #include <cstring>
43 #include <string>
45 #include "gromacs/utility/cstringutil.h"
46 #include "gromacs/utility/fatalerror.h"
47 #include "gromacs/utility/smalloc.h"
49 typedef struct warninp {
50 gmx_bool bAllowWarnings;
51 int nwarn_note;
52 int nwarn_warn;
53 int nwarn_error;
54 int maxwarn;
55 int lineno;
56 std::string filenm;
57 } t_warninp;
59 warninp_t init_warning(gmx_bool bAllowWarnings, int maxwarning)
61 warninp_t wi = new warninp;
63 wi->bAllowWarnings = bAllowWarnings;
64 wi->maxwarn = maxwarning;
65 warning_reset(wi);
66 wi->filenm = "unknown";
67 wi->lineno = 0;
69 return wi;
72 void warning_reset(warninp_t wi)
74 wi->nwarn_note = 0;
75 wi->nwarn_warn = 0;
76 wi->nwarn_error = 0;
79 void set_warning_line(warninp_t wi, const char *s, int line)
81 if (s != nullptr)
83 wi->filenm = s;
85 wi->lineno = line;
88 int get_warning_line(warninp_t wi)
90 return wi->lineno;
93 const char *get_warning_file(warninp_t wi)
95 return wi->filenm.c_str();
98 static void low_warning(warninp_t wi, const char *wtype, int n, const char *s)
100 #define indent 2
101 char *temp, *temp2;
102 int i;
104 if (s == nullptr)
106 s = "Empty error message.";
108 snew(temp, std::strlen(s)+indent+1);
109 for (i = 0; i < indent; i++)
111 temp[i] = ' ';
113 temp[indent] = '\0';
114 std::strcat(temp, s);
115 temp2 = wrap_lines(temp, 78-indent, indent, FALSE);
116 if (!wi->filenm.empty())
118 if (wi->lineno != -1)
120 fprintf(stderr, "\n%s %d [file %s, line %d]:\n%s\n\n",
121 wtype, n, wi->filenm.c_str(), wi->lineno, temp2);
123 else
125 fprintf(stderr, "\n%s %d [file %s]:\n%s\n\n",
126 wtype, n, wi->filenm.c_str(), temp2);
129 else
131 fprintf(stderr, "\n%s %d:\n%s\n\n", wtype, n, temp2);
133 sfree(temp);
134 sfree(temp2);
137 void warning(warninp_t wi, const char *s)
139 if (wi->bAllowWarnings)
141 wi->nwarn_warn++;
142 low_warning(wi, "WARNING", wi->nwarn_warn, s);
144 else
146 warning_error(wi, s);
150 void warning(warninp_t wi, const std::string &s)
152 warning(wi, s.c_str());
155 void warning_note(warninp_t wi, const char *s)
157 wi->nwarn_note++;
158 low_warning(wi, "NOTE", wi->nwarn_note, s);
161 void warning_note(warninp_t wi, const std::string &s)
163 warning_note(wi, s.c_str());
166 void warning_error(warninp_t wi, const char *s)
168 wi->nwarn_error++;
169 low_warning(wi, "ERROR", wi->nwarn_error, s);
172 void warning_error(warninp_t wi, const std::string &s)
174 warning_error(wi, s.c_str());
177 static void print_warn_count(const char *type, int n)
179 if (n > 0)
181 fprintf(stderr, "\nThere %s %d %s%s\n",
182 (n == 1) ? "was" : "were", n, type, (n == 1) ? "" : "s");
186 // Note it is the caller's responsibility to ensure that exiting is correct behaviour
187 static gmx_noreturn void check_warning_error_impl(warninp_t wi, int f_errno, const char *file, int line)
189 print_warn_count("note", wi->nwarn_note);
190 print_warn_count("warning", wi->nwarn_warn);
192 gmx_fatal(f_errno, file, line, "There %s %d error%s in input file(s)",
193 (wi->nwarn_error == 1) ? "was" : "were", wi->nwarn_error,
194 (wi->nwarn_error == 1) ? "" : "s");
197 void check_warning_error(warninp_t wi, int f_errno, const char *file, int line)
199 if (wi->nwarn_error > 0)
201 check_warning_error_impl(wi, f_errno, file, line);
205 void warning_error_and_exit(warninp_t wi, const char *s, int f_errno, const char *file, int line)
207 warning_error(wi, s);
208 check_warning_error_impl(wi, f_errno, file, line);
211 gmx_bool warning_errors_exist(warninp_t wi)
213 return (wi->nwarn_error > 0);
216 void done_warning(warninp_t wi, int f_errno, const char *file, int line)
218 // If we've had an error, then this will report the number of
219 // notes and warnings, and then exit.
220 check_warning_error(wi, f_errno, file, line);
222 // Otherwise, we report the number of notes and warnings.
223 print_warn_count("note", wi->nwarn_note);
224 print_warn_count("warning", wi->nwarn_warn);
226 if (wi->maxwarn >= 0 && wi->nwarn_warn > wi->maxwarn)
228 gmx_fatal(f_errno, file, line,
229 "Too many warnings (%d).\n"
230 "If you are sure all warnings are harmless, use the -maxwarn option.",
231 wi->nwarn_warn);
234 free_warning(wi);
237 void free_warning(warninp_t wi)
239 delete wi;
242 void _too_few(warninp_t wi, const char *fn, int line)
244 char buf[STRLEN];
246 sprintf(buf,
247 "Too few parameters on line (source file %s, line %d)",
248 fn, line);
249 warning(wi, buf);
252 void _incorrect_n_param(warninp_t wi, const char *fn, int line)
254 char buf[STRLEN];
256 sprintf(buf,
257 "Incorrect number of parameters on line (source file %s, line %d)",
258 fn, line);
259 warning(wi, buf);