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

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

80 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 logging 

14import os 

15import pprint 

16import shutil 

17import sys 

18import time 

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 - 0.1) 

30 else: 

31 return t 

32 

33 

34def format_time(t): 

35 t = _squeeze_time(t) 

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

37 

38 

39def short_format_time(t): 

40 t = _squeeze_time(t) 

41 if t > 60: 

42 return "%4.1fmin" % (t / 60.0) 

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 

51 print_options = np.get_printoptions() 

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

53 else: 

54 print_options = None 

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

56 if print_options: 

57 np.set_printoptions(**print_options) 

58 return out 

59 

60 

61############################################################################### 

62# class `Logger` 

63############################################################################### 

64class Logger(object): 

65 """Base class for logging messages.""" 

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 def __init__(self, logfile=None, logdir=None): 

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

102 raise ValueError("Cannot specify both logfile and logdir") 

103 # XXX: Need argument docstring 

104 self.last_time = time.time() 

105 self.start_time = self.last_time 

106 if logdir is not None: 

107 logfile = os.path.join(logdir, "joblib.log") 

108 self.logfile = logfile 

109 if logfile is not None: 

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

111 if os.path.exists(logfile): 

112 # Rotate the logs 

113 for i in range(1, 9): 

114 try: 

115 shutil.move(logfile + ".%i" % i, logfile + ".%i" % (i + 1)) 

116 except: # noqa: E722 

117 "No reason failing here" 

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

119 # monitoring this file does not get lost. 

120 try: 

121 shutil.copy(logfile, logfile + ".1") 

122 except: # noqa: E722 

123 "No reason failing here" 

124 try: 

125 with open(logfile, "w") as logfile: 

126 logfile.write("\nLogging joblib python script\n") 

127 logfile.write("\n---%s---\n" % time.ctime(self.last_time)) 

128 except: # noqa: E722 

129 """ Multiprocessing writing to files can create race 

130 conditions. Rather fail silently than crash the 

131 computation. 

132 """ 

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

134 # silent failure. 

135 

136 def __call__(self, msg="", total=False): 

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

138 call, with an optional message. 

139 """ 

140 if not total: 

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

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

143 else: 

144 # FIXME: Too much logic duplicated 

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

146 full_msg = "%s: %.2fs, %.1f min" % (msg, time_lapse, time_lapse / 60) 

147 print(full_msg, file=sys.stderr) 

148 if self.logfile is not None: 

149 try: 

150 with open(self.logfile, "a") as f: 

151 print(full_msg, file=f) 

152 except: # noqa: E722 

153 """ Multiprocessing writing to files can create race 

154 conditions. Rather fail silently than crash the 

155 calculation. 

156 """ 

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

158 # silent failure. 

159 self.last_time = time.time()