Scheaven
2021-09-18 291deeb1fcf45dbf39a24aa72a213ff3fd6b3405
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
U
©r_ã@sÖddlZddlZddlZddlZddlZddlmZddlmZddl    m
Z
Gdd„dej ƒZ e  ¡ddd    dd
œd d „ƒZej dd dd„ƒZdd„ZeƒZiZddddœdd„Zdddœdd„Zdddœdd„ZdS)éN)ÚCounteré)Ú PathManager)Úcoloredcs(eZdZ‡fdd„Z‡fdd„Z‡ZS)Ú_ColorfulFormattercsJ| d¡d|_| dd¡|_t|jƒr4|jd|_tt|ƒj||ŽdS)NÚ    root_nameÚ.Ú abbrev_nameÚ)ÚpopÚ
_root_nameÚ _abbrev_nameÚlenÚsuperrÚ__init__)ÚselfÚargsÚkwargs©Ú    __class__©ú./utils/logger.pyr s
 
 z_ColorfulFormatter.__init__cs~|j |j|j¡|_tt|ƒ |¡}|jtj    krBt
dddgd}n0|jtj ksZ|jtj krnt
ddddgd}n|S|d|S)NÚWARNINGÚredÚblink)ÚattrsÚERRORÚ    underlineú ) ÚnameÚreplacer r rrÚ formatMessageÚlevelnoÚloggingrrrÚCRITICAL)rÚrecordÚlogÚprefixrrrr!s z _ColorfulFormatter.formatMessage)Ú__name__Ú
__module__Ú __qualname__rr!Ú __classcell__rrrrr s rTZfastreid)Úcolorrr    c Cs,t |¡}| tj¡d|_|dkr4|dkr0dn|}tjddd}|dkr tjtjd    }| tj¡|rˆt    t
d
d ƒd d|t |ƒd }n|}|  |¡|  |¡|dk    r(| d¡s¾| d¡rÄ|}    ntj |d¡}    |dkrè|    d |¡}    t tj |    ¡¡t t|    ƒ¡}
|
 tj¡|
  |¡|  |
¡|S)a(
    Args:
        output (str): a file name or a directory to save log. If None, will not save log file.
            If ends with ".txt" or ".log", assumed to be a file name.
            Otherwise, logs will be saved to `output/log.txt`.
        name (str): the root module name of this logger
        abbrev_name (str): an abbreviation of the module, to avoid long names in logs.
            Set to "" to not log the root module in logs.
            By default, will abbreviate "detectron2" to "d2" and leave other
            modules unchanged.
    FNÚ
detectron2Úd2z1[%(asctime)s] %(name)s %(levelname)s: %(message)sz%m/%d %H:%M:%S)Údatefmtr)Ústreamz[%(asctime)s %(name)s]: Úgreenz %(message)s)r/rr    z.txtz.logzlog.txtz.rank{})r#Ú    getLoggerÚsetLevelÚDEBUGÚ    propagateÚ    FormatterÚ StreamHandlerÚsysÚstdoutrrÚstrÚ setFormatterÚ
addHandlerÚendswithÚosÚpathÚjoinÚformatrÚmkdirsÚdirnameÚ_cached_log_stream) ÚoutputÚdistributed_rankr,rr    ÚloggerZplain_formatterÚchÚ    formatterÚfilenameÚfhrrrÚ setup_logger sD
 ÿ  ü
 
 
 
 
rL)ÚmaxsizecCs t |d¡S)NÚa)rÚopen)rJrrrrD]srDcCs^t d¡}|rZ|j}tj dd¡|jkrR|jd}|dkr>d}||j|j|j    ffS|j
}q
dS)z€
    Returns:
        str: module name of the caller
        tuple: a hashable key to be used to identify different callers
    éÚutilszlogger.r(Ú__main__r-N) r8Ú    _getframeÚf_coder>r?r@Ú co_filenameÚ    f_globalsÚf_linenoÚco_nameÚf_back)ÚframeÚcodeÚmod_namerrrÚ _find_calleris
 
r]Úcaller)rÚkeycCs†t|tƒr|f}t|ƒdks t‚tƒ\}}d}d|kr>||}d|krP||f}t|d7<t||kr‚t |pv|¡ ||¡dS)a[
    Log only for the first n times.
    Args:
        lvl (int): the logging level
        msg (str):
        n (int):
        name (str): name of the logger to use. Will use the caller's module by default.
        key (str or tuple[str]): the string(s) can be one of "caller" or
            "message", which defines how to identify duplicated logs.
            For example, if called with `n=1, key="caller"`, this function
            will only log the first call from the same caller, regardless of
            the message content.
            If called with `n=1, key="message"`, this function will log the
            same content only once, even if they are called from different places.
            If called with `n=1, key=("caller", "message")`, this function
            will not log only if the same caller has logged the same message before.
    rrr^ÚmessagerN)    Ú
isinstancer:rÚAssertionErrorr]Ú _LOG_COUNTERr#r2r&)ÚlvlÚmsgÚnrr_Ú caller_moduleZ
caller_keyZhash_keyrrrÚ log_first_n~s
 
 
 rh)rcCsLtƒ\}}t|d7<|dks2t||dkrHt |p<|¡ ||¡dS)zÊ
    Log once per n times.
    Args:
        lvl (int): the logging level
        msg (str):
        n (int):
        name (str): name of the logger to use. Will use the caller's module by default.
    rN)r]rcr#r2r&)rdrerfrrgr_rrrÚ log_every_n s    
ricCsTtƒ\}}t |d¡}t ¡}|dks2|||krPt |p<|¡ ||¡|t|<dS)zÙ
    Log no more than once per n seconds.
    Args:
        lvl (int): the logging level
        msg (str):
        n (int):
        name (str): name of the logger to use. Will use the caller's module by default.
    N)r]Ú
_LOG_TIMERÚgetÚtimer#r2r&)rdrerfrrgr_Z last_loggedZ current_timerrrÚlog_every_n_seconds¯s     
 rm)Nr)r)r)r)Ú    functoolsr#r>r8rlÚ collectionsrÚfile_iorÚ    termcolorrr6rÚ    lru_cacherLrDr]rcrjrhrirmrrrrÚ<module>s0   ÿÿ<
 
"