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