Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 129 lines (111 sloc) 4.475 kb
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
1 """
d405a16 @GaelVaroquaux Cleanup, increase code coverage, rename logging utilities to what they
GaelVaroquaux authored
2 Helpers for logging.
3
4 This module needs much love to become useful.
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
5 """
4f67d00 @GaelVaroquaux Added docs and doctest for memoize
GaelVaroquaux authored
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
11
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
12 import time
13 import sys
14 import os
55cb296 @GaelVaroquaux merge upstream
GaelVaroquaux authored
15 import shutil
198b67f @GaelVaroquaux ENH: Separate the memory context from the wrapped function to enable
GaelVaroquaux authored
16 import logging
b4360a6 @GaelVaroquaux ENH: Better logging.
GaelVaroquaux authored
17 import pprint
198b67f @GaelVaroquaux ENH: Separate the memory context from the wrapped function to enable
GaelVaroquaux authored
18
19 ################################################################################
20 # class `Logger`
21 ################################################################################
22 class Logger(object):
47d9c53 @GaelVaroquaux DOC: Improve documentation.
GaelVaroquaux authored
23 """ Base class for logging messages.
24 """
198b67f @GaelVaroquaux ENH: Separate the memory context from the wrapped function to enable
GaelVaroquaux authored
25
b4360a6 @GaelVaroquaux ENH: Better logging.
GaelVaroquaux authored
26 def __init__(self, depth=3):
27 """
28 Parameters
29 ----------
30 depth: int, optional
31 The depth of objects printed.
32 """
33 self.depth = depth
34
198b67f @GaelVaroquaux ENH: Separate the memory context from the wrapped function to enable
GaelVaroquaux authored
35 def warn(self, msg):
36 logging.warn("[%s]: %s" % (self, msg))
37
db52826 @GaelVaroquaux Improve debug feature.
GaelVaroquaux authored
38 def debug(self, msg):
39 logging.debug("[%s]: %s" % (self, msg))
40
b4360a6 @GaelVaroquaux ENH: Better logging.
GaelVaroquaux authored
41 def format(self, obj, indent=0):
42 """ Return the formated representation of the object.
43 """
44 if 'numpy' in sys.modules:
45 import numpy as np
46 print_options = np.get_printoptions()
47 np.set_printoptions(precision=6, threshold=64, edgeitems=2)
48 else:
49 print_options = None
50 out = pprint.pformat(obj, depth=self.depth, indent=indent)
51 if print_options:
52 np.set_printoptions(**print_options)
53 return out
198b67f @GaelVaroquaux ENH: Separate the memory context from the wrapped function to enable
GaelVaroquaux authored
54
55
56 ################################################################################
57 # class `PrintTime`
58 ################################################################################
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
59 class PrintTime(object):
47d9c53 @GaelVaroquaux DOC: Improve documentation.
GaelVaroquaux authored
60 """ Print and log messages while keeping track of time.
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
61 """
62
7829c90 @GaelVaroquaux ENH: Misc enhancements to PrintTime
GaelVaroquaux authored
63 def __init__(self, logfile=None, logdir=None):
64 if logfile is not None and logdir is not None:
65 raise ValueError('Cannot specify both logfile and logdir')
66 # XXX: Need argument docstring
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
67 self.last_time = time.time()
b4360a6 @GaelVaroquaux ENH: Better logging.
GaelVaroquaux authored
68 self.start_time = self.last_time
7829c90 @GaelVaroquaux ENH: Misc enhancements to PrintTime
GaelVaroquaux authored
69 if logdir is not None:
70 logfile = os.path.join(logdir, 'joblib.log')
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
71 self.logfile = logfile
55cb296 @GaelVaroquaux merge upstream
GaelVaroquaux authored
72 if logfile is not None:
fc170a2 @GaelVaroquaux Make joblib logging more robust to multiprocessing.
GaelVaroquaux authored
73 if not os.path.exists(os.path.dirname(logfile)):
74 os.makedirs(os.path.dirname(logfile))
55cb296 @GaelVaroquaux merge upstream
GaelVaroquaux authored
75 if os.path.exists(logfile):
76 # Rotate the logs
77 for i in range(1, 9):
78 if os.path.exists(logfile+'.%i' % i):
fc170a2 @GaelVaroquaux Make joblib logging more robust to multiprocessing.
GaelVaroquaux authored
79 try:
80 shutil.move(logfile+'.%i' % i,
81 logfile+'.%i' % (i+1))
82 except:
83 "No reason failing here"
55cb296 @GaelVaroquaux merge upstream
GaelVaroquaux authored
84 # Use a copy rather than a move, so that a process
85 # monitoring this file does not get lost.
fc170a2 @GaelVaroquaux Make joblib logging more robust to multiprocessing.
GaelVaroquaux authored
86 try:
87 shutil.copy(logfile, logfile+'.1')
88 except:
89 "No reason failing here"
90 try:
91 logfile = file(logfile, 'w')
92 logfile.write('\nLogging joblib python script\n')
93 logfile.write('\n---%s---\n' % time.ctime(self.last_time))
94 except:
95 """ Multiprocessing writing to files can create race
96 conditions. Rather fail silently than crash the
97 caculation.
98 """
99 # XXX: We actually need a debug flag to disable this
100 # silent failure.
101
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
102
7829c90 @GaelVaroquaux ENH: Misc enhancements to PrintTime
GaelVaroquaux authored
103 def __call__(self, msg='', total=False):
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
104 """ Print the time elapsed between the last call and the current
105 call, with an optional message.
106 """
7829c90 @GaelVaroquaux ENH: Misc enhancements to PrintTime
GaelVaroquaux authored
107 if not total:
108 time_lapse = time.time() - self.last_time
109 full_msg = "%s: %.2fs, %.1f min" % (msg, time_lapse, time_lapse/60)
110 else:
111 # FIXME: Too much logic duplicated
112 time_lapse = time.time() - self.start_time
113 full_msg = "%s: %.2fs, %.1f min" % (msg, time_lapse, time_lapse/60)
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
114 print >> sys.stderr, full_msg
115 if self.logfile is not None:
fc170a2 @GaelVaroquaux Make joblib logging more robust to multiprocessing.
GaelVaroquaux authored
116 try:
117 print >> file(self.logfile, 'a'), full_msg
118 except:
119 """ Multiprocessing writing to files can create race
120 conditions. Rather fail silently than crash the
121 caculation.
122 """
123 # XXX: We actually need a debug flag to disable this
124 # silent failure.
481e3f2 Initial commit of the split off tree
Gael Varoquaux authored
125 self.last_time = time.time()
126
127
128
Something went wrong with that request. Please try again.