1 | //#--------------------------------------------------------------------------- |
---|
2 | //# PKSmsg.cc: Message handling for the PKSIO classes. |
---|
3 | //#--------------------------------------------------------------------------- |
---|
4 | //# livedata - processing pipeline for single-dish, multibeam spectral data. |
---|
5 | //# Copyright (C) 2008-2009, Australia Telescope National Facility, CSIRO |
---|
6 | //# |
---|
7 | //# This file is part of livedata. |
---|
8 | //# |
---|
9 | //# livedata is free software: you can redistribute it and/or modify it under |
---|
10 | //# the terms of the GNU General Public License as published by the Free |
---|
11 | //# Software Foundation, either version 3 of the License, or (at your option) |
---|
12 | //# any later version. |
---|
13 | //# |
---|
14 | //# livedata is distributed in the hope that it will be useful, but WITHOUT |
---|
15 | //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
16 | //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
17 | //# more details. |
---|
18 | //# |
---|
19 | //# You should have received a copy of the GNU General Public License along |
---|
20 | //# with livedata. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | //# |
---|
22 | //# Correspondence concerning livedata may be directed to: |
---|
23 | //# Internet email: mcalabre@atnf.csiro.au |
---|
24 | //# Postal address: Dr. Mark Calabretta |
---|
25 | //# Australia Telescope National Facility, CSIRO |
---|
26 | //# PO Box 76 |
---|
27 | //# Epping NSW 1710 |
---|
28 | //# AUSTRALIA |
---|
29 | //# |
---|
30 | //# http://www.atnf.csiro.au/computing/software/livedata.html |
---|
31 | //# $Id: PKSmsg.cc,v 1.3 2009-09-29 07:33:38 cal103 Exp $ |
---|
32 | //#--------------------------------------------------------------------------- |
---|
33 | //# Original: 2008/09/18, Mark Calabretta, ATNF |
---|
34 | //#--------------------------------------------------------------------------- |
---|
35 | |
---|
36 | #include <atnf/PKSIO/PKSmsg.h> |
---|
37 | |
---|
38 | #include <casa/stdio.h> |
---|
39 | |
---|
40 | #include <string> |
---|
41 | #include <cstring> |
---|
42 | |
---|
43 | //------------------------------------------------------------- PKSmsg::PKSmsg |
---|
44 | |
---|
45 | // Default constructor. |
---|
46 | |
---|
47 | PKSmsg::PKSmsg() |
---|
48 | { |
---|
49 | initMsg(); |
---|
50 | } |
---|
51 | |
---|
52 | void PKSmsg::initMsg() |
---|
53 | { |
---|
54 | cMsgFD = stderr; |
---|
55 | cMsgLen = 0; |
---|
56 | cMsgBuff = 0x0; |
---|
57 | cMsgIdx = cMsgBuff; |
---|
58 | cNMsg = 0; |
---|
59 | } |
---|
60 | |
---|
61 | //------------------------------------------------------------ PKSmsg::~PKSmsg |
---|
62 | |
---|
63 | // Destructor. |
---|
64 | |
---|
65 | PKSmsg::~PKSmsg() |
---|
66 | { |
---|
67 | delete [] cMsgBuff; |
---|
68 | cMsgBuff = 0x0; |
---|
69 | cMsgIdx = cMsgBuff; |
---|
70 | } |
---|
71 | |
---|
72 | //------------------------------------------------------------- PKSmsg::setMsg |
---|
73 | |
---|
74 | // Set message disposition. If fd is non-zero messages will be written |
---|
75 | // to that file descriptor, else stored for retrieval by getMsg(). |
---|
76 | |
---|
77 | int PKSmsg::setMsg(FILE *fd) |
---|
78 | { |
---|
79 | cMsgFD = fd; |
---|
80 | |
---|
81 | if (cMsgFD == 0x0) { |
---|
82 | clearMsg(); |
---|
83 | |
---|
84 | } else { |
---|
85 | delete [] cMsgBuff; |
---|
86 | cMsgLen = 0; |
---|
87 | cMsgBuff = 0x0; |
---|
88 | cMsgIdx = cMsgBuff; |
---|
89 | } |
---|
90 | |
---|
91 | return 0; |
---|
92 | } |
---|
93 | |
---|
94 | //------------------------------------------------------------- PKSmsg::logMsg |
---|
95 | |
---|
96 | // Log a message. |
---|
97 | |
---|
98 | void PKSmsg::logMsg(const char *msg) |
---|
99 | { |
---|
100 | if (msg) { |
---|
101 | if (cMsgFD) { |
---|
102 | fprintf(cMsgFD, "%s\n", msg); |
---|
103 | |
---|
104 | } else { |
---|
105 | // Expand the message buffer if necessary. |
---|
106 | if (!cMsgBuff) { |
---|
107 | cMsgLen = 128; |
---|
108 | cMsgBuff = new char[cMsgLen]; |
---|
109 | cMsgIdx = cMsgBuff; |
---|
110 | clearMsg(); |
---|
111 | } |
---|
112 | |
---|
113 | int used = strlen(cMsgBuff); |
---|
114 | int free = cMsgLen - (used + 1); |
---|
115 | int xtra = 1 + strlen(msg); |
---|
116 | if (free < xtra) { |
---|
117 | if (xtra < 128) xtra = 128; |
---|
118 | cMsgLen += xtra; |
---|
119 | char *newBuff = new char[cMsgLen]; |
---|
120 | strcpy(newBuff, cMsgBuff); |
---|
121 | delete [] cMsgBuff; |
---|
122 | cMsgBuff = newBuff; |
---|
123 | cMsgIdx = cMsgBuff; |
---|
124 | } |
---|
125 | |
---|
126 | sprintf(cMsgBuff+used, "%s%s", ((cNMsg++)?"\n":""), msg); |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | //------------------------------------------------------------- PKSmsg::getMsg |
---|
132 | |
---|
133 | // Get a message string, or 0x0 if there is none. |
---|
134 | |
---|
135 | const char *PKSmsg::getMsg() |
---|
136 | { |
---|
137 | if (cMsgBuff && *cMsgBuff) { |
---|
138 | cMsgIdx = cMsgBuff; |
---|
139 | return cMsgBuff; |
---|
140 | } |
---|
141 | |
---|
142 | return 0x0; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | // Get the next group of messages by type: ERROR, WARNING, or otherwise. |
---|
147 | |
---|
148 | const char *PKSmsg::getMsg(msgType &type) |
---|
149 | { |
---|
150 | if (cMsgIdx && *cMsgIdx) { |
---|
151 | if (strncmp(cMsgIdx, "ERROR", 5) == 0) { |
---|
152 | type = ERROR; |
---|
153 | } else if (strncmp(cMsgIdx, "WARNING", 7) == 0) { |
---|
154 | type = WARNING; |
---|
155 | } else { |
---|
156 | type = NORMAL; |
---|
157 | } |
---|
158 | |
---|
159 | // Gather multi-line messages. |
---|
160 | char *cp = cMsgIdx; |
---|
161 | while ((cMsgIdx = strchr(cMsgIdx, '\n'))) { |
---|
162 | cMsgIdx++; |
---|
163 | |
---|
164 | if (type == ERROR) { |
---|
165 | if (strncmp(cMsgIdx, "ERROR", 5) == 0 || |
---|
166 | strncmp(cMsgIdx, " ", 5) == 0) { |
---|
167 | continue; |
---|
168 | } |
---|
169 | |
---|
170 | } else if (type == WARNING) { |
---|
171 | if (strncmp(cMsgIdx, "WARNING", 7) == 0 || |
---|
172 | strncmp(cMsgIdx, " ", 7) == 0) { |
---|
173 | continue; |
---|
174 | } |
---|
175 | |
---|
176 | } else { |
---|
177 | if (strncmp(cMsgIdx, "ERROR", 5) || |
---|
178 | strncmp(cMsgIdx, "WARNING", 7)) { |
---|
179 | continue; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | *(cMsgIdx-1) = '\0'; |
---|
184 | break; |
---|
185 | } |
---|
186 | |
---|
187 | return cp; |
---|
188 | } |
---|
189 | |
---|
190 | return 0x0; |
---|
191 | } |
---|
192 | |
---|
193 | //----------------------------------------------------------- PKSmsg::clearMsg |
---|
194 | |
---|
195 | // Clear the message buffer. |
---|
196 | |
---|
197 | void PKSmsg::clearMsg(void) |
---|
198 | { |
---|
199 | if (cMsgBuff) *cMsgBuff = '\0'; |
---|
200 | cMsgIdx = cMsgBuff; |
---|
201 | cNMsg = 0; |
---|
202 | } |
---|