Debian Codegraph @

libsrc/st/midlog.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2007 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++++++++++++++ Module MIDLOG +++++++++++++++++++++++++++++++++++++++
29 .LANGUAGE C
30 .IDENTIFICATION Module MIDLOG.C
31 .AUTHOR Klaus Banse
32 .KEYWORDS Midas logging utility
33 .ENVIRONMENT VMS and UNIX
34 .COMMENTS
35 holds MID_LOG
36
37 .VERSION [1.30] 861110: (K. Banse)
38
39 070730 last modif
40 ------------------------------------------------------------------------*/
41
42 #include <stdio.h>
43
44 #include <fileexts.h>
45 #include <fsydef.h>
46
47
48 /*
49
50 */
51
52 #ifdef __STDC__
53 int MID_LOG(char flag , char * line, int linlen)
54 #else
55 int MID_LOG(flag,line,linlen)
56 /*++++++++++++++++++++++++++++++++++++++++++++++++++
57 .KEYWORDS
58 logfile
59 .PURPOSE
60 manage the logfile
61 .ALGORITHM
62 The logfile is a sequential "stream" file with fixed records of 80 bytes,
63 it is a shared file (between the MIDAS monitor + application program)
64 .RETURNS
65 status: I*4 return status
66 .REMARKS
67 values for flag are:
68 S - for starting up (initializing) of the logfile;
69 I - for opening; O - for closing;
70 G (L) - for writing a new record;
71 P - for starting a new page
72
73 --------------------------------------------------*/
74
75 char flag; /* IN: action = S, I, O, G, L, P */
76 char *line; /* IN: record to be written to logfile \
77 if 'flag' = I, it's the unit */
78 int linlen; /* IN: length of line */
79
80 #endif
81
82 {
83 register int nr;
84 int mm, fstat;
85 int lbase, baseindx, pageindx;
86
87 char strbuf[40], work[88];
88
89
90 static int lp = -1; /* logfile file id */
91 static int lppr = -1; /* print file id */
92 static char blank_line[] = " ";
93 static char new_page[] = " \f";
94 static char lpframe[200]; /* logfile in MID_WORK */
95
96
97
98
99 if (KEYALL.ORIGIN > 9) /* outside MIDAS */
100 return (ERR_NORMAL);
101
102 /* // revive if needed... //
103 if (KIWORDS[OFF_MODE+7] == 1)
104 (void) printf("MID_LOG: flag = %c\n",flag);
105 */
106
107 if (KIWORDS[OFF_LOG] == 0)
108 return ERR_NORMAL; /* test log flag */
109
110
111 /* -------------------------------------------------------------------
112 flag = G : enter line into logfile
113 ------------------------------------------------------------------- */
114
115 if (flag == 'G')
116 {
117 if (lppr >= 0)
118 {
119 fstat = osawrite(lppr,line,linlen);
120 if (fstat == 0)
121 {
122 lppr = -1;
123 KIWORDS[OFF_LOG+8] = 0;
124 (void)printf("MID_LOG: could not write into print file\n");
125 }
126 return ERR_NORMAL; /* job already done */
127 }
128
129 if (lp < 0)
130 {
131 (void)strcpy(strbuf,"sync");
132 goto error;
133 }
134
135 baseindx = OFF_LOG + 5;
136 lbase = KIWORDS[baseindx]; /* get base reg. */
137
138 if (lbase >= KIWORDS[OFF_LOG+6]) /* see, if at page limit */
139 {
140 lbase = 2; /* update base reg. */
141 pageindx = OFF_LOG + 4;
142 KIWORDS[pageindx] ++;
143 OSY_ASCTIM(strbuf); /* get current time */
144
145 (void) osawrite(lp,new_page,2);
146 nr = OFF_SESS + 15;
147 mm = snprintf(work,(size_t) 84,
148 "*** MIDAS logfile (%10.10s) *** %s Page %d\n",
149 &KCWORDS[nr],strbuf,KIWORDS[pageindx]);
150 (void) osawrite(lp,work,mm);
151 (void) osawrite(lp,blank_line,1);
152 }
153
154 fstat = osawrite(lp,line,linlen); /* only check last osawrite */
155 if (fstat == 0)
156 {
157 (void)strcpy(strbuf,"write");
158 goto error;
159 }
160
161 KIWORDS[baseindx] = ++lbase; /* save incremented base counter */
162 }
163
164
165 /* -------------------------------------------------------------------
166 flag = I : open 'logfile' inside SCSPRO
167 ------------------------------------------------------------------- */
168
169
170 else if (flag == 'I')
171 {
172 register int mr;
173
174 lppr = -1; /* just to make sure */
175
176 if (KIWORDS[OFF_LOG+8] > 0) /* we want to print */
177 {
178 nr = OFF_PRINT;
179 if (KCWORDS[nr] != 'F')
180 {
181 (void)strncpy(work,"MID_WORK:",9);
182 mm = 9;
183 }
184 else
185 mm = 0;
186
187 (void)memcpy(&work[mm],&(KCWORDS[nr+2]),(size_t)40);
188 work[mm+40] = '\0';
189 for (mr=mm+1; mr<(mm+40); mr++)
190 {
191 if (work[mr] == ' ')
192 {
193 work[mr] = '\0';
194 break;
195 }
196 }
197
198 lppr = CGN_OPEN(work,WRITE);
199 if (lppr < 0)
200 (void)printf("MID_LOG: could not open print file %s\n",work);
201 }
202
203 if (lp < 0) /* only needed, if not already opened */
204 {
205 (void)memcpy(lpframe,"MID_WORK:FORGR .LOG",(size_t)21);
206 lpframe[14] = *line;
207 lpframe[15] = *(line+1);
208 lp = CGN_OPEN(lpframe,APPEND);
209 if (lp < 0)
210 {
211 (void)strcpy(strbuf,"open");
212 goto error;
213 }
214 }
215 }
216
217
218 /* -------------------------------------------------------------------
219 flag = O : close logfile
220 ------------------------------------------------------------------- */
221
222
223 else if (flag == 'O')
224 {
225 if (lppr >= 0)
226 {
227 osaclose(lppr);
228 lppr = -1;
229 }
230
231 if (lp >= 0)
232 {
233 osaclose(lp);
234 lp = -1;
235 }
236 }
237
238 /* -------------------------------------------------------------------
239 flag = S : open logfile for the first time + write first lines
240 ------------------------------------------------------------------- */
241
242 else if (flag == 'S')
243 {
244 (void)memcpy(lpframe,"MID_WORK:FORGR .LOG",(size_t)21);
245 lpframe[14] = *line;
246 lpframe[15] = *(line+1);
247 lp = CGN_OPEN(lpframe,WRITE);
248 if (lp < 0) /* we could not get started... */
249 {
250 (void)strcpy(strbuf,"open");
251 goto error;
252 }
253
254
255 /* now init everything */
256
257 baseindx = OFF_LOG + 5;
258 pageindx = OFF_LOG + 4;
259 OSY_ASCTIM(strbuf);
260 KIWORDS[pageindx] = 1; /* init page counter + base page reg. */
261 KIWORDS[baseindx] = 2; /* and base reg... */
262
263 nr = OFF_SESS + 15;
264 mm = snprintf(work,(size_t) 84,
265 "*** MIDAS logfile (%10.10s) *** %s Page 1\n",
266 &KCWORDS[nr],strbuf);
267
268 (void) osawrite(lp,work,mm);
269 fstat = osawrite(lp,blank_line,1);
270 if (fstat == 0) /* only check last osawrite */
271 {
272 (void)strcpy(strbuf,"write");
273 goto error;
274 }
275
276 osaclose(lp);
277 lp = -1;
278 }
279
280 /* -------------------------------------------------------------------
281 only left is flag = P : start new page in logfile
282 ------------------------------------------------------------------- */
283
284 else
285 {
286 if (lp < 0)
287 {
288 (void)strcpy(strbuf,"sync");
289 goto error;
290 }
291
292 fflush(stdout); /* first, flush out buffer */
293
294 baseindx = OFF_LOG + 5;
295 pageindx = OFF_LOG + 4;
296
297 KIWORDS[pageindx] ++;
298 OSY_ASCTIM(strbuf); /* get current time */
299
300 (void) osawrite(lp,new_page,2);
301 nr = OFF_SESS + 15;
302 mm = snprintf(work,(size_t) 84,
303 "*** MIDAS logfile (vers. %10.10s) *** %s Page %d\n",
304 &KCWORDS[nr],strbuf,KIWORDS[pageindx]);
305 (void) osawrite(lp,work,mm);
306 fstat = osawrite(lp,blank_line,1);
307 if (fstat == 0) /* only check last osawrite */
308 {
309 (void)strcpy(strbuf,"write");
310 goto error;
311 }
312
313 KIWORDS[baseindx] = 2; /* reset base counter */
314 }
315
316 return ERR_NORMAL; /* get out of here */
317
318
319 /* -------------------------------------------------------------------
320 ... here for errors ...
321 -----------------------------------------------------------------------*/
322
323 error:
324 (void)printf("MID_LOG: %s error with logfile: %s\n",strbuf,lpframe);
325 osaclose(lp);
326
327
328 /* clear actual + permanent log flag */
329
330 KIWORDS[OFF_LOG] = KIWORDS[OFF_LOG+7] = 0;
331 (void)printf("Logging switched off now ...\n");
332
333 return ERR_INPINV;
334 }