Aluode/PerceptionLabPortable
0
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 Varoquaux9# License: BSD Style, 3 clauses.10 11from __future__ import print_function12 13import logging14import os15import pprint16import shutil17import sys18import time19 20from .disk import mkdirp21 22 23def _squeeze_time(t):24 """Remove .1s to the time under Windows: this is the time it take to25 stat files. This is needed to make results similar to timings under26 Unix, for tests27 """28 if sys.platform.startswith("win"):29 return max(0, t - 0.1)30 else:31 return t32 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 np50 51 print_options = np.get_printoptions()52 np.set_printoptions(precision=6, threshold=64, edgeitems=1)53 else:54 print_options = None55 out = pprint.pformat(obj, depth=depth, indent=indent)56 if print_options:57 np.set_printoptions(**print_options)58 return out59 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 Parameters70 ----------71 depth: int, optional72 The depth of objects printed.73 name: str, optional74 The namespace to log to. If None, defaults to joblib.75 """76 self.depth = depth77 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 class87 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 docstring104 self.last_time = time.time()105 self.start_time = self.last_time106 if logdir is not None:107 logfile = os.path.join(logdir, "joblib.log")108 self.logfile = logfile109 if logfile is not None:110 mkdirp(os.path.dirname(logfile))111 if os.path.exists(logfile):112 # Rotate the logs113 for i in range(1, 9):114 try:115 shutil.move(logfile + ".%i" % i, logfile + ".%i" % (i + 1))116 except: # noqa: E722117 "No reason failing here"118 # Use a copy rather than a move, so that a process119 # monitoring this file does not get lost.120 try:121 shutil.copy(logfile, logfile + ".1")122 except: # noqa: E722123 "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: E722129 """ Multiprocessing writing to files can create race130 conditions. Rather fail silently than crash the131 computation.132 """133 # XXX: We actually need a debug flag to disable this134 # silent failure.135 136 def __call__(self, msg="", total=False):137 """Print the time elapsed between the last call and the current138 call, with an optional message.139 """140 if not total:141 time_lapse = time.time() - self.last_time142 full_msg = "%s: %s" % (msg, format_time(time_lapse))143 else:144 # FIXME: Too much logic duplicated145 time_lapse = time.time() - self.start_time146 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: E722153 """ Multiprocessing writing to files can create race154 conditions. Rather fail silently than crash the155 calculation.156 """157 # XXX: We actually need a debug flag to disable this158 # silent failure.159 self.last_time = time.time()160 