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 |
|
---|
42 | //------------------------------------------------------------- PKSmsg::PKSmsg
|
---|
43 |
|
---|
44 | // Default constructor.
|
---|
45 |
|
---|
46 | PKSmsg::PKSmsg()
|
---|
47 | {
|
---|
48 | initMsg();
|
---|
49 | }
|
---|
50 |
|
---|
51 | void PKSmsg::initMsg()
|
---|
52 | {
|
---|
53 | cMsgFD = stderr;
|
---|
54 | cMsgLen = 0;
|
---|
55 | cMsgBuff = 0x0;
|
---|
56 | cMsgIdx = cMsgBuff;
|
---|
57 | cNMsg = 0;
|
---|
58 | }
|
---|
59 |
|
---|
60 | //------------------------------------------------------------ PKSmsg::~PKSmsg
|
---|
61 |
|
---|
62 | // Destructor.
|
---|
63 |
|
---|
64 | PKSmsg::~PKSmsg()
|
---|
65 | {
|
---|
66 | delete [] cMsgBuff;
|
---|
67 | cMsgBuff = 0x0;
|
---|
68 | cMsgIdx = cMsgBuff;
|
---|
69 | }
|
---|
70 |
|
---|
71 | //------------------------------------------------------------- PKSmsg::setMsg
|
---|
72 |
|
---|
73 | // Set message disposition. If fd is non-zero messages will be written
|
---|
74 | // to that file descriptor, else stored for retrieval by getMsg().
|
---|
75 |
|
---|
76 | int PKSmsg::setMsg(FILE *fd)
|
---|
77 | {
|
---|
78 | cMsgFD = fd;
|
---|
79 |
|
---|
80 | if (cMsgFD == 0x0) {
|
---|
81 | clearMsg();
|
---|
82 |
|
---|
83 | } else {
|
---|
84 | delete [] cMsgBuff;
|
---|
85 | cMsgLen = 0;
|
---|
86 | cMsgBuff = 0x0;
|
---|
87 | cMsgIdx = cMsgBuff;
|
---|
88 | }
|
---|
89 |
|
---|
90 | return 0;
|
---|
91 | }
|
---|
92 |
|
---|
93 | //------------------------------------------------------------- PKSmsg::logMsg
|
---|
94 |
|
---|
95 | // Log a message.
|
---|
96 |
|
---|
97 | void PKSmsg::logMsg(const char *msg)
|
---|
98 | {
|
---|
99 | if (msg) {
|
---|
100 | if (cMsgFD) {
|
---|
101 | fprintf(cMsgFD, "%s\n", msg);
|
---|
102 |
|
---|
103 | } else {
|
---|
104 | // Expand the message buffer if necessary.
|
---|
105 | if (!cMsgBuff) {
|
---|
106 | cMsgLen = 128;
|
---|
107 | cMsgBuff = new char[cMsgLen];
|
---|
108 | cMsgIdx = cMsgBuff;
|
---|
109 | clearMsg();
|
---|
110 | }
|
---|
111 |
|
---|
112 | int used = strlen(cMsgBuff);
|
---|
113 | int free = cMsgLen - (used + 1);
|
---|
114 | int xtra = 1 + strlen(msg);
|
---|
115 | if (free < xtra) {
|
---|
116 | if (xtra < 128) xtra = 128;
|
---|
117 | cMsgLen += xtra;
|
---|
118 | char *newBuff = new char[cMsgLen];
|
---|
119 | strcpy(newBuff, cMsgBuff);
|
---|
120 | delete [] cMsgBuff;
|
---|
121 | cMsgBuff = newBuff;
|
---|
122 | cMsgIdx = cMsgBuff;
|
---|
123 | }
|
---|
124 |
|
---|
125 | sprintf(cMsgBuff+used, "%s%s", ((cNMsg++)?"\n":""), msg);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | //------------------------------------------------------------- PKSmsg::getMsg
|
---|
131 |
|
---|
132 | // Get a message string, or 0x0 if there is none.
|
---|
133 |
|
---|
134 | const char *PKSmsg::getMsg()
|
---|
135 | {
|
---|
136 | if (cMsgBuff && *cMsgBuff) {
|
---|
137 | cMsgIdx = cMsgBuff;
|
---|
138 | return cMsgBuff;
|
---|
139 | }
|
---|
140 |
|
---|
141 | return 0x0;
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | // Get the next group of messages by type: ERROR, WARNING, or otherwise.
|
---|
146 |
|
---|
147 | const char *PKSmsg::getMsg(msgType &type)
|
---|
148 | {
|
---|
149 | if (cMsgIdx && *cMsgIdx) {
|
---|
150 | if (strncmp(cMsgIdx, "ERROR", 5) == 0) {
|
---|
151 | type = ERROR;
|
---|
152 | } else if (strncmp(cMsgIdx, "WARNING", 7) == 0) {
|
---|
153 | type = WARNING;
|
---|
154 | } else {
|
---|
155 | type = NORMAL;
|
---|
156 | }
|
---|
157 |
|
---|
158 | // Gather multi-line messages.
|
---|
159 | char *cp = cMsgIdx;
|
---|
160 | while ((cMsgIdx = strchr(cMsgIdx, '\n'))) {
|
---|
161 | cMsgIdx++;
|
---|
162 |
|
---|
163 | if (type == ERROR) {
|
---|
164 | if (strncmp(cMsgIdx, "ERROR", 5) == 0 ||
|
---|
165 | strncmp(cMsgIdx, " ", 5) == 0) {
|
---|
166 | continue;
|
---|
167 | }
|
---|
168 |
|
---|
169 | } else if (type == WARNING) {
|
---|
170 | if (strncmp(cMsgIdx, "WARNING", 7) == 0 ||
|
---|
171 | strncmp(cMsgIdx, " ", 7) == 0) {
|
---|
172 | continue;
|
---|
173 | }
|
---|
174 |
|
---|
175 | } else {
|
---|
176 | if (strncmp(cMsgIdx, "ERROR", 5) ||
|
---|
177 | strncmp(cMsgIdx, "WARNING", 7)) {
|
---|
178 | continue;
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | *(cMsgIdx-1) = '\0';
|
---|
183 | break;
|
---|
184 | }
|
---|
185 |
|
---|
186 | return cp;
|
---|
187 | }
|
---|
188 |
|
---|
189 | return 0x0;
|
---|
190 | }
|
---|
191 |
|
---|
192 | //----------------------------------------------------------- PKSmsg::clearMsg
|
---|
193 |
|
---|
194 | // Clear the message buffer.
|
---|
195 |
|
---|
196 | void PKSmsg::clearMsg(void)
|
---|
197 | {
|
---|
198 | if (cMsgBuff) *cMsgBuff = '\0';
|
---|
199 | cMsgIdx = cMsgBuff;
|
---|
200 | cNMsg = 0;
|
---|
201 | }
|
---|