1 | #include <stdlib.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <string.h> |
---|
4 | |
---|
5 | #ifdef _UNDERSCORE |
---|
6 | #define cio_set_log_filename cio_set_log_filename_ |
---|
7 | #define cio_prints cio_prints_ |
---|
8 | #define cio_printf cio_printf_ |
---|
9 | #define cio_printi cio_printi_ |
---|
10 | #endif |
---|
11 | #ifdef _DOUBLEUNDERSCORE |
---|
12 | #define cio_set_log_filename cio_set_log_filename__ |
---|
13 | #define cio_prints cio_prints__ |
---|
14 | #define cio_printf cio_printf__ |
---|
15 | #define cio_printi cio_printi__ |
---|
16 | #endif |
---|
17 | |
---|
18 | char * logfilename = 0; |
---|
19 | FILE * cio_out = 0; |
---|
20 | |
---|
21 | void cio_set_log_filename(char * s, int * n) |
---|
22 | { |
---|
23 | /* Allow changes to the log filename so long as |
---|
24 | * we haven't actually opened the file (and written |
---|
25 | * to it). |
---|
26 | */ |
---|
27 | if (!cio_out && logfilename) |
---|
28 | { |
---|
29 | free(logfilename); |
---|
30 | logfilename = 0; |
---|
31 | } |
---|
32 | |
---|
33 | if (!logfilename) |
---|
34 | { |
---|
35 | logfilename = (char *)malloc(*n+1); |
---|
36 | strncpy(logfilename, s, *n); |
---|
37 | logfilename[*n] = '\0'; |
---|
38 | } |
---|
39 | } |
---|
40 | |
---|
41 | void cio_printf(int * fd, float * f) |
---|
42 | { |
---|
43 | if (!logfilename) return; |
---|
44 | |
---|
45 | if (*fd != 0) |
---|
46 | { |
---|
47 | if (!cio_out) cio_out = fopen(logfilename,"w"); |
---|
48 | fprintf(cio_out, "%f", *f); |
---|
49 | fflush(cio_out); |
---|
50 | } |
---|
51 | else |
---|
52 | { |
---|
53 | fprintf(stdout, "%f", *f); |
---|
54 | fflush(stdout); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | void cio_printi(int * fd, int * i) |
---|
59 | { |
---|
60 | if (!logfilename) return; |
---|
61 | |
---|
62 | if (*fd != 0) |
---|
63 | { |
---|
64 | if (!cio_out) cio_out = fopen(logfilename,"w"); |
---|
65 | fprintf(cio_out, "%i", *i); |
---|
66 | fflush(cio_out); |
---|
67 | } |
---|
68 | else |
---|
69 | { |
---|
70 | fprintf(stdout, "%i", *i); |
---|
71 | fflush(stdout); |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | void cio_prints(int * fd, char * s, int * n) |
---|
76 | { |
---|
77 | if (!logfilename) return; |
---|
78 | |
---|
79 | if (*fd != 0) |
---|
80 | { |
---|
81 | s[*n] = '\0'; |
---|
82 | if (!cio_out) cio_out = fopen(logfilename,"w"); |
---|
83 | fprintf(cio_out, "%s", s); |
---|
84 | fflush(cio_out); |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | s[*n] = '\0'; |
---|
89 | fprintf(stdout, "%s", s); |
---|
90 | fflush(stdout); |
---|
91 | } |
---|
92 | } |
---|