Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/joblib/logger.py: 27%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

79 statements  

1""" 

2Helpers for logging. 

3 

4This module needs much love to become useful. 

5""" 

6 

7# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org> 

8# Copyright (c) 2008 Gael Varoquaux 

9# License: BSD Style, 3 clauses. 

10 

11from __future__ import print_function 

12 

13import time 

14import sys 

15import os 

16import shutil 

17import logging 

18import pprint 

19 

20from .disk import mkdirp 

21 

22 

23def _squeeze_time(t): 

24 """Remove .1s to the time under Windows: this is the time it take to 

25 stat files. This is needed to make results similar to timings under 

26 Unix, for tests 

27 """ 

28 if sys.platform.startswith('win'): 

29 return max(0, t - .1) 

30 else: 

31 return t 

32 

33 

34def format_time(t): 

35 t = _squeeze_time(t) 

36 return "%.1fs, %.1fmin" % (t, t / 60.) 

37 

38 

39def short_format_time(t): 

40 t = _squeeze_time(t) 

41 if t > 60: 

42 return "%4.1fmin" % (t / 60.) 

43 else: 

44 return " %5.1fs" % (t) 

45 

46 

47def pformat(obj, indent=0, depth=3): 

48 if 'numpy' in sys.modules: 

49 import numpy as np 

50 print_options = np.get_printoptions() 

51 np.set_printoptions(precision=6, threshold=64, edgeitems=1) 

52 else: 

53 print_options = None 

54 out = pprint.pformat(obj, depth=depth, indent=indent) 

55 if print_options: 

56 np.set_printoptions(**print_options) 

57 return out 

58 

59 

60############################################################################### 

61# class `Logger` 

62############################################################################### 

63class Logger(object): 

64 """ Base class for logging messages. 

65 """ 

66 

67 def __init__(self, depth=3, name=None): 

68 """ 

69 Parameters 

70 ---------- 

71 depth: int, optional 

72 The depth of objects printed. 

73 name: str, optional 

74 The namespace to log to. If None, defaults to joblib. 

75 """ 

76 self.depth = depth 

77 self._name = name if name else 'joblib' 

78 

79 def warn(self, msg): 

80 logging.getLogger(self._name).warning("[%s]: %s" % (self, msg)) 

81 

82 def info(self, msg): 

83 logging.info("[%s]: %s" % (self, msg)) 

84 

85 def debug(self, msg): 

86 # XXX: This conflicts with the debug flag used in children class 

87 logging.getLogger(self._name).debug("[%s]: %s" % (self, msg)) 

88 

89 def format(self, obj, indent=0): 

90 """Return the formatted representation of the object.""" 

91 return pformat(obj, indent=indent, depth=self.depth) 

92 

93 

94############################################################################### 

95# class `PrintTime` 

96############################################################################### 

97class PrintTime(object): 

98 """ Print and log messages while keeping track of time. 

99 """ 

100 

101 def __init__(self, logfile=None, logdir=None): 

102 if logfile is not None and logdir is not None: 

103 raise ValueError('Cannot specify both logfile and logdir') 

104 # XXX: Need argument docstring 

105 self.last_time = time.time() 

106 self.start_time = self.last_time 

107 if logdir is not None: 

108 logfile = os.path.join(logdir, 'joblib.log') 

109 self.logfile = logfile 

110 if logfile is not None: 

111 mkdirp(os.path.dirname(logfile)) 

112 if os.path.exists(logfile): 

113 # Rotate the logs 

114 for i in range(1, 9): 

115 try: 

116 shutil.move(logfile + '.%i' % i, 

117 logfile + '.%i' % (i + 1)) 

118 except: # noqa: E722 

119 "No reason failing here" 

120 # Use a copy rather than a move, so that a process 

121 # monitoring this file does not get lost. 

122 try: 

123 shutil.copy(logfile, logfile + '.1') 

124 except: # noqa: E722 

125 "No reason failing here" 

126 try: 

127 with open(logfile, 'w') as logfile: 

128 logfile.write('\nLogging joblib python script\n') 

129 logfile.write('\n---%s---\n' % time.ctime(self.last_time)) 

130 except: # noqa: E722 

131 """ Multiprocessing writing to files can create race 

132 conditions. Rather fail silently than crash the 

133 computation. 

134 """ 

135 # XXX: We actually need a debug flag to disable this 

136 # silent failure. 

137 

138 def __call__(self, msg='', total=False): 

139 """ Print the time elapsed between the last call and the current 

140 call, with an optional message. 

141 """ 

142 if not total: 

143 time_lapse = time.time() - self.last_time 

144 full_msg = "%s: %s" % (msg, format_time(time_lapse)) 

145 else: 

146 # FIXME: Too much logic duplicated 

147 time_lapse = time.time() - self.start_time 

148 full_msg = "%s: %.2fs, %.1f min" % (msg, time_lapse, 

149 time_lapse / 60) 

150 print(full_msg, file=sys.stderr) 

151 if self.logfile is not None: 

152 try: 

153 with open(self.logfile, 'a') as f: 

154 print(full_msg, file=f) 

155 except: # noqa: E722 

156 """ Multiprocessing writing to files can create race 

157 conditions. Rather fail silently than crash the 

158 calculation. 

159 """ 

160 # XXX: We actually need a debug flag to disable this 

161 # silent failure. 

162 self.last_time = time.time()