Coverage for pyquickhelper/loghelper/buffered_flog.py: 100%

10 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 02:21 +0200

1""" 

2@file 

3@brief Buffer as a logging function. 

4""" 

5from io import StringIO 

6 

7 

8class BufferedPrint: 

9 """ 

10 Buffered display. Relies on :epkg:`*py:io:StringIO`. 

11 Use it as follows: 

12 

13 .. runpython:: 

14 :showcode: 

15 

16 def do_something(fLOG=None): 

17 if fLOG: 

18 fLOG("Did something.") 

19 return 3 

20 

21 from pyquickhelper.loghelper import BufferedPrint 

22 buf = BufferedPrint() 

23 do_something(fLOG=buf.fprint) 

24 print(buf) 

25 """ 

26 

27 def __init__(self): 

28 "constructor" 

29 self.buffer = StringIO() 

30 

31 def fprint(self, *args, **kwargs): 

32 "print function" 

33 mes = " ".join(str(_) for _ in args) 

34 self.buffer.write(mes) 

35 self.buffer.write("\n") 

36 

37 def __str__(self): 

38 "Returns the content." 

39 return self.buffer.getvalue()