6
6
"errors"
7
7
"fmt"
8
8
"log"
9
+ "os"
9
10
"os/exec"
10
11
"strings"
11
12
"time"
25
26
ErrCommandNotReady = errors .New ("inkscape not ready" )
26
27
)
27
28
28
- var (
29
- bufferPool = NewSizedBufferPool (5 , 1024 * 1024 )
30
- verbose bool
31
- )
32
-
33
- func debug (v ... interface {}) {
34
- if ! verbose {
35
- return
36
- }
37
-
38
- log .Print (append ([]interface {}{"proxy:" }, v ... )... )
39
- }
29
+ // bytes.Buffer pool
30
+ var bufferPool = NewSizedBufferPool (5 , 1024 * 1024 )
40
31
41
32
type chanWriter struct {
42
33
out chan []byte
@@ -60,6 +51,7 @@ func (w *chanWriter) Write(data []byte) (int, error) {
60
51
// instance via stdin
61
52
type Proxy struct {
62
53
options Options
54
+ logger * log.Logger
63
55
64
56
// context and cancellation
65
57
ctx context.Context
@@ -76,6 +68,12 @@ type Proxy struct {
76
68
stderr chan []byte
77
69
}
78
70
71
+ func (p * Proxy ) debug (args ... interface {}) {
72
+ if p .options .verbose {
73
+ p .logger .Println (args ... )
74
+ }
75
+ }
76
+
79
77
// runBackground run inkscape instance in background
80
78
func (p * Proxy ) runBackground (ctx context.Context , commandPath string , vars ... string ) error {
81
79
args := []string {
@@ -108,7 +106,7 @@ func (p *Proxy) runBackground(ctx context.Context, commandPath string, vars ...s
108
106
defer stdin .Close ()
109
107
110
108
// start command and wait it close
111
- debug ("run in background" )
109
+ p . debug ("run in background" )
112
110
if err := cmd .Start (); err != nil {
113
111
return err
114
112
}
@@ -140,7 +138,7 @@ wait:
140
138
return cmd .Wait ()
141
139
142
140
case command := <- p .requestQueue :
143
- debug ("write command " , string (command ))
141
+ p . debug ("write command " , string (command ))
144
142
if _ , err := stdin .Write (command ); err != nil {
145
143
p .stderr <- []byte (err .Error ())
146
144
}
@@ -173,7 +171,7 @@ func (p *Proxy) Run(args ...string) error {
173
171
return ErrCommandNotAvailable
174
172
}
175
173
176
- debug (commandPath )
174
+ p . debug (commandPath )
177
175
178
176
p .ctx , p .cancel = context .WithCancel (context .Background ())
179
177
@@ -215,14 +213,14 @@ func (p *Proxy) sendCommand(b []byte, waitPrompt ...bool) ([]byte, error) {
215
213
}
216
214
217
215
// wait available
218
- debug ("wait prompt available" )
216
+ p . debug ("wait prompt available" )
219
217
<- p .requestLimiter
220
218
defer func () {
221
219
// make it available again
222
220
p .requestLimiter <- struct {}{}
223
221
}()
224
222
225
- debug ("send command to stdin " , string (b ))
223
+ p . debug ("send command to stdin " , string (b ))
226
224
227
225
// drain old err and out
228
226
drain (p .stderr )
@@ -250,7 +248,7 @@ waitLoop:
250
248
for {
251
249
// wait until received prompt
252
250
bytesOut := <- p .stdout
253
- debug (string (bytesOut ))
251
+ p . debug (string (bytesOut ))
254
252
parts := bytes .Split (bytesOut , []byte ("\n " ))
255
253
for _ , part := range parts {
256
254
if isPrompt (part ) {
@@ -267,7 +265,7 @@ errLoop:
267
265
select {
268
266
case bytesErr := <- p .stderr :
269
267
if len (bytesErr ) > 0 {
270
- debug (string (bytesErr ))
268
+ p . debug (string (bytesErr ))
271
269
err = fmt .Errorf ("%s" , string (bytesErr ))
272
270
}
273
271
default :
@@ -303,7 +301,7 @@ func (p *Proxy) Svg2Pdf(svgIn, pdfOut string) error {
303
301
return err
304
302
}
305
303
306
- debug ("result" , string (res ))
304
+ p . debug ("result" , string (res ))
307
305
308
306
return nil
309
307
}
@@ -320,13 +318,11 @@ func NewProxy(opts ...Option) *Proxy {
320
318
// merge options
321
319
options = mergeOptions (options , opts ... )
322
320
323
- // check verbosity
324
- verbose = options .verbose
325
-
326
321
return & Proxy {
327
322
options : options ,
328
323
stdout : make (chan []byte , 100 ),
329
324
stderr : make (chan []byte , 100 ),
325
+ logger : log .New (os .Stdout , "[debug]" , log .Lshortfile ),
330
326
331
327
// limit request to one request at time
332
328
requestLimiter : make (chan struct {}, 1 ),
0 commit comments