-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprintf.d
150 lines (127 loc) · 2.9 KB
/
printf.d
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
/*
* Empire, the Wargame of the Century (tm)
* Copyright (C) 1978-2004 by Walter Bright
* All Rights Reserved
*
* You may use this source for personal use only. To use it commercially
* or to distribute source or binaries of Empire, please contact
* www.digitalmars.com.
*
* Written by Walter Bright.
* This source is written in the D Programming Language.
* See www.digitalmars.com/d/ for the D specification and compiler.
*
* Use entirely at your own risk. There is no warranty, expressed or implied.
*/
/* This file implements printf for GUI apps that sends the output
* to logfile rather than stdout.
* The file is closed after each printf, so that it is complete even
* if the program subsequently crashes.
*/
import std.c.stdio;
import std.c.stdlib;
import std.string;
import std.file;
alias void* va_list;
const int LOG = 1; // disable logging by setting this to 0
version (Windows)
{
char logfile[] = r"\empire.log";
}
version (linux)
{
char logfile[] = "/var/log/empire.log";
}
/*********************************************
* Route printf() to a log file rather than stdio.
*/
enum Plog
{
TOBITBUCKET,
TOLOGFILE,
TOSTDOUT,
};
Plog printf_logging = Plog.TOLOGFILE;
void printf_tologfile()
{
printf_logging = Plog.TOLOGFILE;
}
void printf_tostdout()
{
printf_logging = Plog.TOSTDOUT;
}
void printf_tobitbucket()
{
printf_logging = Plog.TOBITBUCKET;
}
void LogfileAppend(char* buffer)
{
if (printf_logging == Plog.TOLOGFILE)
std.file.append(logfile, buffer[0 .. strlen(buffer)]);
else
{
fputs(buffer, stdout);
fflush(stdout);
}
}
extern (C)
{
int VPRINTF(char* format, va_list args)
{
if (printf_logging != Plog.TOBITBUCKET)
{
char buffer[128];
char* p;
uint psize;
int count;
p = buffer;
psize = buffer.length;
for (;;)
{
version (linux)
{
count = vsnprintf(p,psize,format,args);
if (count == -1)
psize *= 2;
else if (count >= cast(int)psize)
psize = count + 1;
else
break;
}
else version (Windows)
{
count = _vsnprintf(p,psize,format,args);
if (count != -1)
break;
psize *= 2;
}
else
{
static assert(0); // unsupported system
}
p = cast(char *) alloca(psize * buffer[0].sizeof); // buffer too small, try again with larger size
}
LogfileAppend(p);
}
return 0;
}
int PRINTF(char* format, ...)
{
int result = 0;
if (printf_logging != Plog.TOBITBUCKET)
{
va_list ap;
//va_start(ap, format);
ap = cast(va_list)(cast(void*)format + format.sizeof);
result = VPRINTF(format,ap);
//va_end(ap);
}
return result;
}
}
void _printf_assert(char* file, uint line)
{
PRINTF("assert fail: %s(%d)\n", file, line);
*cast(char *)0 = 0; // seg fault to ensure it isn't overlooked
exit(0);
}