Merge branch 'release-v4.6.0' of github.com:wrf-model/WRF
[WRF.git] / frame / clog.c
blob0b3b8d05db1d954b72ab75bc89a845568a8a6c5d
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
5 #ifdef __cplusplus
6 extern "C" {
7 #endif
9 static void clog_init();
10 static void clog_finalize();
12 void clog_set_buffer_len(int *wantlenp);
13 void clog_write(int*len,char*c);
14 void clog_flush(int*flag);
16 static const size_t default_len=100;
18 static FILE *stream=NULL;
19 static char *buffer=NULL;
20 static size_t len=0,used=0;
21 static int inited=0;
23 static void clog_init(void) {
24 if(!inited) {
25 atexit(clog_finalize);
26 inited=1;
30 static void clog_finalize(void) {
31 int i=1;
32 if(buffer)
33 clog_flush(&i);
34 free(buffer);
37 void clog_set_buffer_len(int *wantlenp) {
38 /* Initialize or re-initialize the output buffer with size *wantlenp */
39 int dummy;
40 size_t wantlen=*wantlenp;
41 if(buffer) {
42 /* Already had a buffer */
43 if(len!=wantlen) {
44 /* User is requesting a change in the buffer size, so flush first */
45 clog_flush(NULL);
46 free(buffer);
47 buffer=NULL;
48 len=used=0;
49 } else {
50 /* Buffer size is not changing, so nothing to do. */
51 return;
55 /* We now need to allocate a new buffer and initialize the state */
56 if(!(buffer=(char*)malloc(sizeof(char)*len)))
57 len=0;
58 len=wantlen;
59 used=0;
61 clog_init();
63 if(!stream)
64 stream=stdout;
65 if(buffer)
66 fprintf(stream,"Clog: buffering rsl.out with a %llu byte buffer...\n",
67 (unsigned long long)len);
70 void clog_write(int *clen,char *c) {
71 size_t slen=(size_t)*clen;
72 size_t nlen=slen;
74 if(c[slen-1]!='\n' && c[slen-1]!='\r')
75 /* String is missing an end-of-line, so we need to add one. */
76 nlen++;
78 if(!stream)
79 stream=stdout;
80 if(!buffer) {
81 /* Buffer is not allocated yet, so allocate it. */
82 if(!(buffer=(char*)malloc(sizeof(char)*default_len))) {
83 /* Failed to allocate the buffer, so write the string directly. */
84 fwrite(c,slen,1,stream);
85 if(nlen>slen) fputc('\n',stream);
86 return;
88 len=default_len;
89 used=0;
90 clog_init();
91 if(buffer)
92 fprintf(stream,"Clog: buffering rsl.out with a %llu byte buffer...\n",
93 (unsigned long long)len);
96 if(nlen>len) {
97 /* Cannot fit the string in the buffer, so we'll have to write the
98 string directly. */
99 if(used>0)
100 clog_flush(NULL);
101 fwrite(c,slen,1,stream);
102 if(nlen>slen) fputc('\n',stream);
103 return;
104 } else if(nlen+used>len) {
105 /* String is smaller than the buffer, but the buffer is filled enough
106 so that the string cannot fit */
107 clog_flush(NULL);
110 if(slen>0)
111 memcpy(buffer+used,c,slen);
113 if(nlen>slen)
114 buffer[used+slen]='\n';
115 used+=nlen;
118 void clog_flush(int *flag) {
119 if(!stream)
120 stream=stdout;
121 if(buffer) {
122 fwrite(buffer,used,1,stream);
123 used=0;
125 if(flag && *flag) {
126 fflush(stream);
130 /* Lookalike functions for all common fortran name mangling schemes */
132 void clog_init_(void) { clog_init(); }
133 void clog_init__(void) { clog_init(); }
134 void CLOG_INIT(void) { clog_init(); }
135 void CLOG_INIT_(void) { clog_init(); }
136 void CLOG_INIT__(void) { clog_init(); }
138 void clog_set_buffer_len_(int *w) { clog_set_buffer_len(w); }
139 void clog_set_buffer_len__(int *w) { clog_set_buffer_len(w); }
140 void CLOG_SET_BUFFER_LEN(int *w) { clog_set_buffer_len(w); }
141 void CLOG_SET_BUFFER_LEN_(int *w) { clog_set_buffer_len(w); }
142 void CLOG_SET_BUFFER_LEN__(int *w) { clog_set_buffer_len(w); }
144 void clog_write_(int *clen,char *c) { clog_write(clen,c); }
145 void clog_write__(int *clen,char *c) { clog_write(clen,c); }
146 void CLOG_WRITE(int *clen,char *c) { clog_write(clen,c); }
147 void CLOG_WRITE_(int *clen,char *c) { clog_write(clen,c); }
148 void CLOG_WRITE__(int *clen,char *c) { clog_write(clen,c); }
150 void clog_flush_(int *flag) { clog_flush(flag); }
151 void clog_flush__(int *flag) { clog_flush(flag); }
152 void CLOG_FLUSH(int *flag) { clog_flush(flag); }
153 void CLOG_FLUSH_(int *flag) { clog_flush(flag); }
154 void CLOG_FLUSH__(int *flag) { clog_flush(flag); }
156 #ifdef __cplusplus
158 #endif