-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprint.cc
53 lines (43 loc) · 1.02 KB
/
print.cc
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
// Print formatted content with a newline
#include "print.h"
#include "debug.h"
#include "noncopyable.h"
#include <fcntl.h>
#include <memory>
#include <unistd.h>
namespace mandelbrot {
using std::unique_ptr;
namespace {
struct Tee : public Noncopyable {
FILE* f = 0;
~Tee() { if (f) fclose(f); }
};
}
static Tee& tee_fd() {
static Tee t;
return t;
}
void tee(const string& path) {
auto& t = tee_fd();
if (t.f) die("can't tee to two files");
const int fd = open(path.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd < 0) die("can't tee output to '%s': %s", path, strerror(errno));
t.f = fdopen(fd, "w");
if (!t.f) die("fdopen failed: %s", strerror(errno));
}
static inline void tee_print(const string& s) {
if (FILE* f = tee_fd().f) {
fprintf(f, "%s\n", s.c_str());
fflush(f);
}
}
void print() { print(""); }
void print(const string& s) {
printf("%s\n", s.c_str());
tee_print(s);
}
void print_error(const string& s) {
fprintf(stderr, "%s\n", s.c_str());
tee_print(s);
}
} // namespace mandelbrot