Пример #1
0
def print_to_file(f_name, msg, *reflex, type='INFO', separator='default', 
        log_dir=_LOG_DIR_DEFAULT, mode='a', perm='default'):
    """
    write message into file
        * create dir / file if not exist
        * read-only policy
    argument:
        f_name      name of the log file
        msg,
        reflex, 
        type, 
        separator   see logf.stringf
        log_dir     the directory to be appended in front of f_name
        mode        'w' for truncate; 'a' for append
        perm        permission of f_name after write to it
            Policy of perm='default':
              if f_name exists before, then don't change permission
              if f_name doesn't exist, then set permission to '0444'
            Policy of perm='xxxx' (string consisting of 4 digits):
              set permission to 'xxxx' no matter f_name exists or not
    """
    msg = str(msg)
    f_full = '{}/{}'.format(log_dir, f_name)
    if perm == 'default':
        perm = (os.path.exists(f_full)) and os.stat(f_full).st_mode or '0444'
    # make sure directory exists
    mkdir_r(os.path.dirname(f_full))
    if os.path.exists(f_full):
        set_f_perm(f_full, '0222')
    f = open(f_full, mode)
    print(stringf(msg, *reflex, type=type, separator=separator), file=f)
    printf('write to file: {}', f_full, separator=None)
    f.close()
    
    set_f_perm(f_full, perm)
Пример #2
0
def printf(string, *reflex, type='INFO', separator='default'):
    """
    for usage, check out logf.stringf
    """
    print(stringf(string, *reflex, type=type, separator=separator))