chiark / gitweb /
more debugging
[moebius3.git] / moedebug.py
1
2 from __future__ import print_function
3
4 import moenp
5
6 _files = []
7
8 def dbg_file(f):
9   _files.append(f)
10
11 def dbg_enabled():
12   return not not _files
13
14 def dbg(*args):
15   for f in _files:
16     s = ' '.join(map(str,args))
17     for l in s.split('\n'):
18       print('// D ', l, file=f)
19     f.flush()
20
21 class BaseVisdebug():
22   def line(vd, p0, p1):
23     vd.curve(lambda t: p0 + t * (p1-p0))
24   def arrow(vd, p, d):
25     vd.line(p, p+d)
26
27 class NullVisdebug():
28   def curve(*a, **kw): pass
29   def line(*a, **kw): pass
30   def arrow(*a, **kw): pass
31   def crashing(*a, **kw): pass
32
33 class MatrixVisdebug(BaseVisdebug):
34   def __init__(m, base, matrix):
35     m._b = base
36     m._matrix = matrix
37   def curve(m, fn):
38     m._b.curve(lambda t: moenp.augmatmultiply(m._matrix, fn(t)))
39   def crashing(m, *a, **kw): m._b.crashing(*a, **kw)
40
41 _nullvis = NullVisdebug()
42 _vis = [ _nullvis ]
43
44 def vdbg():
45   return _vis[0]
46
47 def vdbg_enabled():
48   return _vis[0] != _nullvis
49
50 def vdbg_enable(vis):
51   _vis[0] = vis