1 | // ----------------------------------------------------------------------- |
---|
2 | // duchamp.cc: Warning and Error messages. |
---|
3 | // ----------------------------------------------------------------------- |
---|
4 | // Copyright (C) 2006, Matthew Whiting, ATNF |
---|
5 | // |
---|
6 | // This program is free software; you can redistribute it and/or modify it |
---|
7 | // under the terms of the GNU General Public License as published by the |
---|
8 | // Free Software Foundation; either version 2 of the License, or (at your |
---|
9 | // option) any later version. |
---|
10 | // |
---|
11 | // Duchamp is distributed in the hope that it will be useful, but WITHOUT |
---|
12 | // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
13 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
---|
14 | // for more details. |
---|
15 | // |
---|
16 | // You should have received a copy of the GNU General Public License |
---|
17 | // along with Duchamp; if not, write to the Free Software Foundation, |
---|
18 | // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA |
---|
19 | // |
---|
20 | // Correspondence concerning Duchamp may be directed to: |
---|
21 | // Internet email: Matthew.Whiting [at] atnf.csiro.au |
---|
22 | // Postal address: Dr. Matthew Whiting |
---|
23 | // Australia Telescope National Facility, CSIRO |
---|
24 | // PO Box 76 |
---|
25 | // Epping NSW 1710 |
---|
26 | // AUSTRALIA |
---|
27 | // ----------------------------------------------------------------------- |
---|
28 | #include <iostream> |
---|
29 | #include <stdexcept> |
---|
30 | #include <iomanip> |
---|
31 | #include <string> |
---|
32 | #include <duchamp/duchamp.hh> |
---|
33 | |
---|
34 | namespace duchamp |
---|
35 | { |
---|
36 | |
---|
37 | void duchampWarning(std::string subroutine, std::string warning) |
---|
38 | { |
---|
39 | /** |
---|
40 | * Prints a WARNING message to the standard error stream, quoting |
---|
41 | * the subroutine that the problem occurred in, and a descriptive |
---|
42 | * warning text. |
---|
43 | * |
---|
44 | * Format: WARNING \<function\> : Something happened that you should be aware of. |
---|
45 | */ |
---|
46 | std::string preamble = "WARNING <" + subroutine + "> : "; |
---|
47 | std::cerr << preamble; |
---|
48 | for(size_t i=0;i<warning.size();i++){ |
---|
49 | std::cerr << warning[i]; |
---|
50 | if((i!=warning.size()-1)&&(warning[i]=='\n')) |
---|
51 | std::cerr << std::setw(preamble.size()) <<": "; |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | DuchampError::DuchampError(const std::string& message) |
---|
56 | : std::runtime_error(message) |
---|
57 | {} |
---|
58 | |
---|
59 | DuchampError::~DuchampError() throw() |
---|
60 | {} |
---|
61 | |
---|
62 | void duchampError(std::string subroutine, std::string error) |
---|
63 | { |
---|
64 | /** |
---|
65 | * Prints an ERROR message to the standard error stream, quoting |
---|
66 | * the subroutine that the problem occurred in, a descriptive |
---|
67 | * warning text, and sounding the bell. |
---|
68 | * |
---|
69 | * Format: ERROR \<function\> : Something bad happened. |
---|
70 | */ |
---|
71 | std::string preamble = "\aERROR <" + subroutine + "> : "; |
---|
72 | std::cerr << preamble; |
---|
73 | for(size_t i=0;i<error.size();i++){ |
---|
74 | std::cerr << error[i]; |
---|
75 | if((i!=error.size()-1)&&(error[i]=='\n')) |
---|
76 | std::cerr << std::setw(preamble.size()-1) <<": "; |
---|
77 | } |
---|
78 | |
---|
79 | size_t loc=0; |
---|
80 | // std::cout << "|"<<error<<"|\n"; |
---|
81 | while (loc=error.find('\n',loc+1), |
---|
82 | // std::cout << loc << " " << error.size() << " " << std::string::npos << "\n", |
---|
83 | loc!=std::string::npos){ |
---|
84 | if(loc!=error.size()-1) error.replace(loc,1," -- "); |
---|
85 | else error[loc]=' '; |
---|
86 | // std::cout << "|"<<error<<"|\n"; |
---|
87 | } |
---|
88 | |
---|
89 | throw DuchampError(error); |
---|
90 | } |
---|
91 | |
---|
92 | } |
---|