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;
23 static void clog_init(void) {
25 atexit(clog_finalize
);
30 static void clog_finalize(void) {
37 void clog_set_buffer_len(int *wantlenp
) {
38 /* Initialize or re-initialize the output buffer with size *wantlenp */
40 size_t wantlen
=*wantlenp
;
42 /* Already had a buffer */
44 /* User is requesting a change in the buffer size, so flush first */
50 /* Buffer size is not changing, so nothing to do. */
55 /* We now need to allocate a new buffer and initialize the state */
56 if(!(buffer
=(char*)malloc(sizeof(char)*len
)))
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
;
74 if(c
[slen
-1]!='\n' && c
[slen
-1]!='\r')
75 /* String is missing an end-of-line, so we need to add one. */
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
);
92 fprintf(stream
,"Clog: buffering rsl.out with a %llu byte buffer...\n",
93 (unsigned long long)len
);
97 /* Cannot fit the string in the buffer, so we'll have to write the
101 fwrite(c
,slen
,1,stream
);
102 if(nlen
>slen
) fputc('\n',stream
);
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 */
111 memcpy(buffer
+used
,c
,slen
);
114 buffer
[used
+slen
]='\n';
118 void clog_flush(int *flag
) {
122 fwrite(buffer
,used
,1,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
); }