Debian Codegraph @

ext/GOTCHA/src/example/autotee/autotee.c

raw symbols SCIP
1 /*
2 This file is part of GOTCHA. For copyright information see the COPYRIGHT
3 file in the top level directory, or at
4 https://github.com/LLNL/gotcha/blob/master/COPYRIGHT
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU Lesser General Public License (as published by the Free
7 Software Foundation) version 2.1 dated February 1999. This program is
8 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 without even the IMPLIED WARRANTY OF MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 PURPOSE. See the terms and conditions of the GNU Lesser General Public License
11 for more details. You should have received a copy of the GNU Lesser General
12 Public License along with this program; if not, write to the Free Software
13 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
14 */
15
16 /**
17 * autotee -
18 * Using gotcha, wrap the major IO writing routines with functions that
19 * "tee" any stdout output to another file. Init by calling
20 * init_autotee(filename)
21 * finish by calling:
22 * close_autotee()
23 *
24 * Note, this is a demonstration program for gotcha and does not handle
25 * cases like stdout's file descriptor being dup'd or more esoteric
26 * IO routines.
27 **/
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33
34 #include "gotcha/gotcha.h"
35 #include "gotcha/gotcha_types.h"
36
37 static int tee_fd = -1;
38 static FILE *tee_FILE = NULL;
39
40 static int printf_wrapper(const char *format, ...);
41 static int fprintf_wrapper(FILE *stream, const char *format, ...);
42 static int vfprintf_wrapper(FILE *stream, const char *str, va_list args);
43 static int vprintf_wrapper(const char *str, va_list args);
44 static ssize_t write_wrapper(int fd, const void *buffer, size_t size);
45 static int puts_wrapper(const char *str);
46 static int fputs_wrapper(const char *str, FILE *f);
47 static int fwrite_wrapper(const void *ptr, size_t size, size_t nmemb,
48 FILE *stream);
49
50 static gotcha_wrappee_handle_t orig_printf_handle;
51 static gotcha_wrappee_handle_t orig_fprintf_handle;
52 static gotcha_wrappee_handle_t orig_vfprintf_handle;
53 static gotcha_wrappee_handle_t orig_vprintf_handle;
54 static gotcha_wrappee_handle_t orig_write_handle;
55 static gotcha_wrappee_handle_t orig_puts_handle;
56 static gotcha_wrappee_handle_t orig_fputs_handle;
57 static gotcha_wrappee_handle_t orig_fwrite_handle;
58
59 #define NUM_IOFUNCS 8
60 struct gotcha_binding_t iofuncs[] = {
61 {"printf", printf_wrapper, &orig_printf_handle},
62 {"fprintf", fprintf_wrapper, &orig_fprintf_handle},
63 {"vfprintf", vfprintf_wrapper, &orig_vfprintf_handle},
64 {"vprintf", vprintf_wrapper, &orig_vprintf_handle},
65 {"write", write_wrapper, &orig_write_handle},
66 {"puts", puts_wrapper, &orig_puts_handle},
67 {"fputs", fputs_wrapper, &orig_fputs_handle},
68 {"fwrite", fwrite_wrapper, &orig_fwrite_handle}};
69
70 int init_autotee(const char *teefile) {
71 enum gotcha_error_t result;
72 gotcha_set_priority("testing/whether/this/works", 1);
73 tee_FILE = fopen(teefile, "w");
74 if (!tee_FILE) {
75 perror("Failed to open tee file");
76 return -1;
77 }
78 tee_fd = fileno(tee_FILE);
79
80 result = gotcha_wrap(iofuncs, NUM_IOFUNCS, "testing/whether");
81 if (result != GOTCHA_SUCCESS) {
82 fprintf(stderr, "gotcha_wrap returned %d\n", (int)result);
83 return -1;
84 }
85
86 return 0;
87 }
88
89 int close_autotee() {
90 if (tee_FILE) {
91 fclose(tee_FILE);
92 tee_fd = -1;
93 }
94 return 0;
95 }
96
97 static int printf_wrapper(const char *format, ...) {
98 typeof(&vfprintf) orig_vfprintf = gotcha_get_wrappee(orig_vfprintf_handle);
99 typeof(&vprintf) orig_vprintf = gotcha_get_wrappee(orig_vprintf_handle);
100 int result;
101 va_list args, args2;
102 va_start(args, format);
103
104 if (tee_FILE) {
105 va_copy(args2, args);
106 orig_vfprintf(tee_FILE, format, args2);
107 va_end(args2);
108 }
109
110 result = orig_vprintf(format, args);
111 va_end(args);
112
113 return result;
114 }
115
116 static int fprintf_wrapper(FILE *stream, const char *format, ...) {
117 typeof(&vfprintf) orig_vfprintf = gotcha_get_wrappee(orig_vfprintf_handle);
118 int result;
119 va_list args, args2;
120 va_start(args, format);
121
122 if (stream != stdout) {
123 result = orig_vfprintf(stream, format, args);
124 } else {
125 if (tee_FILE) {
126 va_copy(args2, args);
127 orig_vfprintf(tee_FILE, format, args2);
128 va_end(args2);
129 }
130 result = orig_vfprintf(stdout, format, args);
131 }
132
133 va_end(args);
134 return result;
135 }
136
137 static int vfprintf_wrapper(FILE *stream, const char *str, va_list args) {
138 typeof(&vfprintf) orig_vfprintf = gotcha_get_wrappee(orig_vfprintf_handle);
139 va_list args2;
140 if (stream != stdout) {
141 return orig_vfprintf(stream, str, args);
142 }
143 if (tee_FILE) {
144 va_copy(args2, args);
145 orig_vfprintf(tee_FILE, str, args2);
146 va_end(args2);
147 }
148 return orig_vfprintf(stream, str, args);
149 }
150
151 static int vprintf_wrapper(const char *str, va_list args) {
152 typeof(&vfprintf) orig_vfprintf = gotcha_get_wrappee(orig_vfprintf_handle);
153 typeof(&vprintf) orig_vprintf = gotcha_get_wrappee(orig_vprintf_handle);
154 va_list args2;
155 if (tee_FILE) {
156 va_copy(args2, args);
157 orig_vfprintf(tee_FILE, str, args2);
158 va_end(args2);
159 }
160 return orig_vprintf(str, args);
161 }
162
163 static ssize_t write_wrapper(int fd, const void *buffer, size_t size) {
164 typeof(&write) orig_write = gotcha_get_wrappee(orig_write_handle);
165 if (fd != 1) return orig_write(fd, buffer, size);
166
167 if (tee_fd != -1) orig_write(tee_fd, buffer, size);
168 return orig_write(fd, buffer, size);
169 }
170
171 static int puts_wrapper(const char *str) {
172 typeof(&fputs) orig_fputs = gotcha_get_wrappee(orig_fputs_handle);
173 typeof(&puts) orig_puts = gotcha_get_wrappee(orig_puts_handle);
174 if (tee_FILE) {
175 orig_fputs(str, tee_FILE);
176 orig_fputs("\n", tee_FILE);
177 }
178 return orig_puts(str);
179 }
180
181 static int fputs_wrapper(const char *str, FILE *f) {
182 typeof(&fputs) orig_fputs = gotcha_get_wrappee(orig_fputs_handle);
183 if (f != stdout) return orig_fputs(str, f);
184 if (tee_FILE) orig_fputs(str, tee_FILE);
185 return orig_fputs(str, f);
186 }
187
188 static int fwrite_wrapper(const void *ptr, size_t size, size_t nmemb,
189 FILE *stream) {
190 typeof(&fwrite) orig_fwrite = gotcha_get_wrappee(orig_fwrite_handle);
191 if (stream != stdout) return orig_fwrite(ptr, size, nmemb, stream);
192 if (tee_FILE) orig_fwrite(ptr, size, nmemb, tee_FILE);
193 return orig_fwrite(ptr, size, nmemb, stream);
194 }