Changeset 16902
- Timestamp:
- Mar 9, 2008, 11:05:03 AM (18 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/lib.shell/gprint.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/lib.shell/gprint.c
r16901 r16902 91 91 } 92 92 93 // close if necessary, set to file (may be NULL) 94 void gprintCloseFile (gpStream *stream, FILE *file) { 95 96 int i, Nmatch; 97 98 // do not close the file if the new file is the same 99 if (stream[0].file == file) return; 100 101 // do not close the file if the old one is NULL 102 if (stream[0].file == NULL) { 103 stream[0].file = file; 104 return; 105 } 106 107 // check the special cases 108 if (stream[0].file == stdout) { 109 stream[0].file = file; 110 return; 111 } 112 if (stream[0].file == stderr) { 113 stream[0].file = file; 114 return; 115 } 116 117 // must we close the existing file? if still being used, then no 118 Nmatch = 0; 119 for (i = 0; i < Nstreams; i++) { 120 if (stream == &streams[i]) continue; 121 if (streams[i].file == stream[0].file) Nmatch ++; 122 } 123 if (Nmatch == 0) { 124 fflush (stream[0].file); 125 fclose (stream[0].file); 126 } 127 128 stream[0].file = file; 129 return; 130 } 131 93 132 void gprintSetBuffer (gpDest dest) { 94 133 … … 97 136 stream = gprintGetStream (dest); 98 137 99 /* if we have an open file: flush it, close it, null it */ 100 if (stream[0].file != NULL) { 101 fflush (stream[0].file); 102 if (stream[0].file == stdout) goto skip_close; 103 if (stream[0].file == stderr) goto skip_close; 104 fclose (stream[0].file); 105 106 skip_close: 107 stream[0].file = NULL; 108 } 138 // close the existing file (if needed), set to NULL 139 gprintCloseFile (stream, NULL); 140 141 assert (stream[0].buffer); 142 FlushIOBuffer (stream[0].buffer); 109 143 110 if (stream[0].buffer == NULL) {111 ALLOCATE (stream[0].buffer, IOBuffer, 1);112 InitIOBuffer (stream[0].buffer, 64);113 } else {114 FlushIOBuffer (stream[0].buffer);115 }116 117 /* this element may be redundant with the NULL state of file and buffer */118 144 stream[0].mode = GP_BUFF; 119 145 } … … 152 178 FILE *file; 153 179 180 assert (stream[0].buffer); 181 154 182 /* if we have an open buffer, free it and null it */ 155 if (stream[0].buffer != NULL) { 156 FreeIOBuffer (stream[0].buffer); 157 free (stream[0].buffer); 158 stream[0].buffer = NULL; 183 if (stream[0].buffer[0].Nbuffer) { 184 // XXX we drop what was on the buffer, send it to the old or the new file? 185 FlushIOBuffer (stream[0].buffer); 159 186 } 160 187 … … 167 194 } 168 195 169 / * we allow the user to set stdout to ERR and stderr to LOG if they want */196 // we allow the user to set stdout to ERR and stderr to LOG if they want 170 197 if (!strcmp (stream[0].name, "stdout")) { 171 198 stream[0].file = stdout; … … 177 204 } 178 205 179 // open the file : use a local FILE pointer. only close old pointer if no one else is 180 // using it 206 // open the file. only close old pointer if no one else is using it 181 207 file = fopen (stream[0].name, "a"); 182 208 if (file == NULL) { … … 188 214 } 189 215 190 /* check the special cases */ 191 if (stream[0].file == stdout) { 192 stream[0].file = file; 193 return; 194 } 195 if (stream[0].file == stderr) { 196 stream[0].file = file; 197 return; 198 } 199 200 /* must we close the existing file? */ 201 if (stream[0].file != NULL) { 202 Nmatch = 0; 203 for (i = 0; i < Nstreams; i++) { 204 if (streams[i].file == stream[0].file) Nmatch ++; 205 } 206 if (file == stream[0].file) Nmatch ++; 207 if (Nmatch == 1) { 208 fflush (stream[0].file); 209 fclose (stream[0].file); 210 } 211 stream[0].file = file; 212 } 216 // close the existing file (if needed), set to new file 217 gprintCloseFile (stream, file); 213 218 return; 214 219 } … … 237 242 238 243 va_start (argp, format); 239 240 // gprint (GP_ERR, "GP_FILE: %d\n", GP_FILE);241 // gprint (GP_ERR, "GP_BUFF: %d\n", GP_BUFF);242 // gprint (GP_ERR, "mode: %d\n", stream[0].mode);243 244 244 245 if (stream[0].mode == GP_FILE) { 245 // gprint (GP_ERR, "printing to FILE\n");246 246 status = vfprintf (stream[0].file, format, argp); 247 247 if (status < 0) { … … 249 249 } 250 250 } else { 251 // gprint (GP_ERR, "printing to BUFFER\n");252 251 vPrintIOBuffer (stream[0].buffer, format, argp); 253 252 } … … 267 266 fwrite (buffer, size, N, stream[0].file); 268 267 } else { 268 // XXX can we not use exising IOBuffer APIs here? 269 269 outbuff = stream[0].buffer; 270 270 Nbyte = size * N;
Note:
See TracChangeset
for help on using the changeset viewer.
