113113import inspect
114114import itertools
115115import locale
116+ import logging
116117import os
117118import re
118119import sys
137138__version__ = str (get_versions ()['version' ])
138139del get_versions
139140
141+ _log = logging .getLogger (__name__ )
142+
140143__version__numpy__ = str ('1.7.1' ) # minimum required numpy version
141144
142145__bibtex__ = r"""@Article{Hunter:2007,
160163if not (_python27 or _python34 ):
161164 raise ImportError ("Matplotlib requires Python 2.7 or 3.4 or later" )
162165
166+ if _python27 :
167+ _log .addHandler (logging .NullHandler ())
168+
163169
164170def compare_versions (a , b ):
165171 "return True if a is greater than or equal to b"
@@ -215,7 +221,75 @@ def _is_writable_dir(p):
215221 """
216222 return os .access (p , os .W_OK ) and os .path .isdir (p )
217223
224+ _verbose_msg = """\
225+ Command line argument --verbose-LEVEL is deprecated.
226+ This functionality is now provided by the standard
227+ python logging library. To get more (or less) logging output:
228+ import logging
229+ logger = logging.getLogger('matplotlib')
230+ logger.set_level(logging.INFO)"""
231+
232+
233+ def _set_logger_verbose_level (level_str = 'silent' , file_str = 'sys.stdout' ):
234+ """
235+ Use a --verbose-LEVEL level to set the logging level:
236+
237+ """
238+ levelmap = {'silent' : logging .WARNING , 'helpful' : logging .INFO ,
239+ 'debug' : logging .DEBUG , 'debug-annoying' : logging .DEBUG ,
240+ 'info' : logging .INFO , 'warning' : logging .WARNING }
241+ # Check that current state of logger isn't already more verbose
242+ # than the requested level. If it is more verbose, then leave more
243+ # verbose.
244+ newlev = levelmap [level_str ]
245+ oldlev = _log .getEffectiveLevel ()
246+ if newlev < oldlev :
247+ _log .setLevel (newlev )
248+ std = {
249+ 'sys.stdout' : sys .stdout ,
250+ 'sys.stderr' : sys .stderr ,
251+ }
252+ if file_str in std :
253+ fileo = std [file_str ]
254+ else :
255+ fileo = sys .stdout
256+ try :
257+ fileo = open (file_str , 'w' )
258+ # if this fails, we will just write to stdout
259+ except IOError :
260+ warnings .warn ('could not open log file "{0}"'
261+ 'for writing. Check your '
262+ 'matplotlibrc' .format (file_str ))
263+ console = logging .StreamHandler (fileo )
264+ console .setLevel (newlev )
265+ _log .addHandler (console )
266+
267+
268+ def _parse_commandline ():
269+ """
270+ Check for --verbose-LEVEL type command line arguments and
271+ set logging level appropriately.
272+ """
218273
274+ levels = ('silent' , 'helpful' , 'debug' , 'debug-annoying' ,
275+ 'info' , 'warning' )
276+
277+ for arg in sys .argv [1 :]:
278+ # cast to str because we are using unicode_literals,
279+ # and argv is always str
280+
281+ if arg .startswith (str ('--verbose-' )):
282+ level_str = arg [10 :]
283+ # If it doesn't match one of ours, then don't even
284+ # bother noting it, we are just a 3rd-party library
285+ # to somebody else's script.
286+ if level_str in levels :
287+ _set_logger_verbose_level (level_str )
288+
289+ _parse_commandline ()
290+
291+
292+ @cbook .deprecated ("2.2" , message = _verbose_msg )
219293class Verbose (object ):
220294 """
221295 A class to handle reporting. Set the fileo attribute to any file
@@ -311,7 +385,29 @@ def ge(self, level):
311385 return self .vald [self .level ] >= self .vald [level ]
312386
313387
314- verbose = Verbose ()
388+ def _wrap (fmt , func , level = 'INFO' , always = True ):
389+ """
390+ return a callable function that wraps func and reports its
391+ output through logger
392+
393+ if always is True, the report will occur on every function
394+ call; otherwise only on the first time the function is called
395+ """
396+ assert callable (func )
397+
398+ def wrapper (* args , ** kwargs ):
399+ ret = func (* args , ** kwargs )
400+
401+ if (always or not wrapper ._spoke ):
402+ lvl = logging .getLevelName (level .upper ())
403+ _log .log (lvl , fmt % ret )
404+ spoke = True
405+ if not wrapper ._spoke :
406+ wrapper ._spoke = spoke
407+ return ret
408+ wrapper ._spoke = False
409+ wrapper .__doc__ = func .__doc__
410+ return wrapper
315411
316412
317413def checkdep_dvipng ():
@@ -512,7 +608,7 @@ def _create_tmp_config_dir():
512608 return configdir
513609
514610
515- get_home = verbose . wrap ('$HOME=%s' , _get_home , always = False )
611+ get_home = _wrap ('$HOME=%s' , _get_home , always = False )
516612
517613
518614def _get_xdg_config_dir ():
@@ -601,7 +697,7 @@ def _get_configdir():
601697 """
602698 return _get_config_or_cache_dir (_get_xdg_config_dir ())
603699
604- get_configdir = verbose . wrap ('CONFIGDIR=%s' , _get_configdir , always = False )
700+ get_configdir = _wrap ('CONFIGDIR=%s' , _get_configdir , always = False )
605701
606702
607703def _get_cachedir ():
@@ -613,7 +709,7 @@ def _get_cachedir():
613709 """
614710 return _get_config_or_cache_dir (_get_xdg_cache_dir ())
615711
616- get_cachedir = verbose . wrap ('CACHEDIR=%s' , _get_cachedir , always = False )
712+ get_cachedir = _wrap ('CACHEDIR=%s' , _get_cachedir , always = False )
617713
618714
619715def _decode_filesystem_path (path ):
@@ -671,8 +767,8 @@ def _get_data_path_cached():
671767 defaultParams ['datapath' ][0 ] = _get_data_path ()
672768 return defaultParams ['datapath' ][0 ]
673769
674- get_data_path = verbose . wrap ('matplotlib data path %s' , _get_data_path_cached ,
675- always = False )
770+ get_data_path = _wrap ('matplotlib data path %s' , _get_data_path_cached ,
771+ always = False )
676772
677773
678774def get_py2exe_datafiles ():
@@ -1035,22 +1131,18 @@ def rc_params_from_file(fname, fail_on_error=False, use_default_template=True):
10351131 if key not in _all_deprecated ])
10361132 config .update (config_from_file )
10371133
1038- verbose .set_level (config ['verbose.level' ])
1039- verbose .set_fileo (config ['verbose.fileo' ])
1040-
10411134 if config ['datapath' ] is None :
10421135 config ['datapath' ] = get_data_path ()
10431136
10441137 if "" .join (config ['text.latex.preamble' ]):
1045- verbose . report ("""
1138+ _log . info ("""
10461139*****************************************************************
10471140You have the following UNSUPPORTED LaTeX preamble customizations:
10481141%s
10491142Please do not ask for support with these customizations active.
10501143*****************************************************************
1051- """ % '\n ' .join (config ['text.latex.preamble' ]), 'helpful' )
1052-
1053- verbose .report ('loaded rc file %s' % fname )
1144+ """ , '\n ' .join (config ['text.latex.preamble' ]))
1145+ _log .info ('loaded rc file %s' , fname )
10541146
10551147 return config
10561148
@@ -1736,9 +1828,7 @@ def inner(ax, *args, **kwargs):
17361828 return inner
17371829 return param
17381830
1739-
1740- verbose .report ('matplotlib version %s' % __version__ )
1741- verbose .report ('verbose.level %s' % verbose .level )
1742- verbose .report ('interactive is %s' % is_interactive ())
1743- verbose .report ('platform is %s' % sys .platform )
1744- verbose .report ('loaded modules: %s' % list (sys .modules ), 'debug' )
1831+ _log .info ('matplotlib version %s' , __version__ )
1832+ _log .info ('interactive is %s' , is_interactive ())
1833+ _log .info ('platform is %s' , sys .platform )
1834+ _log .debug ('loaded modules: %s' , list (sys .modules ))
0 commit comments