Skip to content

Commit

Permalink
Fix Readme, and do some modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
sdf611097 committed Mar 9, 2018
1 parent 9bc2600 commit 8045b7c
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 303 deletions.
47 changes: 27 additions & 20 deletions README.md
Expand Up @@ -11,27 +11,33 @@ RESET, BOLD, FAINT, ITALIC, UNDERLINE
#COLORS
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
```

## Usages

### Time prefix
Set your environment variable ENABLE_TIME_PREFIX=1, each log from logger will contians time prefix with format [%m-%d %H:%M:%S]

### color(arg1, arg2, ...)
Usage is just like print(), but the log will with color which you specified.
Like print(), but the log is with color which you specified.
```python
logger.cyan('this', 'is', 'a', 'cyan', 'log')
logger.red(1, 2, 3)
```
## bgColor(arg1, arg2, ...)

## bg_color(arg1, arg2, ...)
Simalar to color(), this funciton is for background color and the font-color will be complementary color.
```python
logger.bgBlue('this', 'log', 'is', 'blue', 'and with background color yellow')
logger.bg_blue('this', 'background color blue, and font is yellow')
```

### log(optList, arg1, arg2, ...)
print log with options(possible options are shown above) and option is case-insensitive.
```python
logger.log(['red','Bg_Yellow'], 3,4,5) #print 3,4,5 (with font-color:red, backgroud is yellow)
logger.log(['blue', 'Bold'], 'this', 'is', 'a', 'blue-Bold', 'log')
logger.log(['BG_BLUE', 'YELLOW'], 'this', 'is', 'as', 'same', 'as', 'logger.bgBlue')
logger.log(['BG_BLUE', 'YELLOW'], 'this', 'is', 'as', 'same', 'as', 'logger.bg_blue')
```

### start(*opts), end()
You can set optsm and then below log from print will apply this opts util end() be called.
```python
Expand All @@ -41,34 +47,35 @@ logger.end()
print('this is normal log')
```

### byCodes(codeList, arg1, arg2, ...)
If you know the code of SGR (Select Graphic Rendition) parameters, you can pass it directly(string list).
### by_codes(codeList, arg1, arg2, ...)
You can pass other codes, and try it.
Find more codes: [ANSI ESCAPE CODE](https://en.wikipedia.org/wiki/ANSI_escape_code)
```python
logger.byCodes(['31','4'], "this is equl to logger.log(['red','underline'], ...)")
logger.by_codes(['31','4'], "this is equl to logger.log(['red','underline'], ...)")
```

### showOpts()
### show_opts()
Once you forgot which option can be use, you can call this function to see all options.
```python
logger.showOpts()
logger.show_opts()
```

### debug()
This log will be print only if the env variable VERBOSE been set.
This log will be print only if the env variable VERBOSE be set.
```python
logger.debug('shows only when', 'VERBOSE', 'been', 'set')
```

### printLineInfo(arg1, arg2...) printStack()
### print_line_info(arg1, arg2...) print_stack()
```python
def test():
printLineInfo('1','2') # <YOUR_FILE_NAME>:12/test 1 2
printStack()
#<YOUR_FILE_NAME>:13/test logger.printStack()
#<YOUR_FILE_NAME>:18/a test()
#<YOUR_FILE_NAME>:19/<module> a()
def bar():
print_line_info('1','2') # <YOUR_FILE_NAME>:12/bar 1 2
print_stack()
#<YOUR_FILE_NAME>:13/bar logger.printStack()
#<YOUR_FILE_NAME>:18/foo bar()
#<YOUR_FILE_NAME>:19/<module> foo()
#...
def a():
test()
a()
def foo():
bar()
foo()
```
103 changes: 52 additions & 51 deletions ctlogger/logger.py
Expand Up @@ -15,7 +15,7 @@
time_format = '[%m-%d %H:%M:%S]'

END = '\x1b[0m'
_bgColors = {
__bgColors = {
'BG_BLACK': '40',
'BG_RED': '41',
'BG_GREEN': '42',
Expand All @@ -26,15 +26,15 @@
'BG_WHITE': '47',
}

_effects = {
__effects = {
'RESET': '0', # all attributes off
'BOLD': '1', # Bold or increased intensity
'FAINT': '2', # Faint (decreased intensity), Not widely supported.
'ITALIC': '3', # Not widely supported. Sometimes treated as inverse.
'UNDERLINE': '4',
}

_colors = {
__colors = {
'BLACK': '30',
'RED': '31',
'GREEN': '32',
Expand All @@ -45,7 +45,7 @@
'WHITE': '37',
}

_fontColorForBGs = {
__fontColorForBGs = {
'BG_BLACK': 'WHITE',
'BG_RED': 'CYAN',
'BG_GREEN': 'MAGENTA',
Expand All @@ -56,14 +56,14 @@
'BG_WHITE': 'BLACK',
}

_CODE_DICT = {**_bgColors, **_effects, **_colors}
__CODE_DICT = {**__bgColors, **__effects, **__colors}


def get_all_options():
return _CODE_DICT.keys()
return __CODE_DICT.keys()


def _transform_codes(codes):
def __transform_codes(codes):
return '\x1b[' + ';'.join(codes) + 'm'


Expand All @@ -72,37 +72,37 @@ def by_codes(codes, *args, **kwargs):
This is core function
"""
arguments = list(args)
arguments.insert(0, _transform_codes(codes))
arguments.insert(0, __transform_codes(codes))
if ENABLE_TIME_PREFIX:
arguments.insert(0, time.strftime(time_format))

arguments.append(END)
print(*arguments, **kwargs)


def _show(title, d):
def __show(title, d):
cyan(title)
italic(', '.join(d.keys()))


def show_opts():
"""
BG_COLORS
BG__colors
BG_BLACK, BG_RED, BG_GREEN, BG_YELLOW, BG_BLUE, BG_MAGENTA, BG_CYAN, BG_WHITE
EFFECTS
RESET, BOLD, FAINT, ITALIC, UNDERLINE
COLORS
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE
"""
_show('BG_COLORS', _bgColors)
_show('EFFECTS', _effects)
_show('COLORS', _colors)
__show('BG__colors', __bgColors)
__show('EFFECTS', __effects)
__show('COLORS', __colors)


def _opts_to_codes(opts):
def __opts_to_codes(opts):
opts_upper = [opt.upper() for opt in opts]
try:
codes = [_CODE_DICT[opt] for opt in opts_upper]
codes = [__CODE_DICT[opt] for opt in opts_upper]
return codes
except KeyError:
opts.insert(0, 'Unexpected options:')
Expand All @@ -112,7 +112,7 @@ def _opts_to_codes(opts):


def log(opts, *args):
codes = _opts_to_codes(opts)
codes = __opts_to_codes(opts)
if codes:
by_codes(codes, *args)

Expand All @@ -126,9 +126,9 @@ def print_line_info(*args, **kwargs):
(filename, line_number, function_name, _, _) = inspect.getframeinfo(previous_frame)
line_info = filename + ':' + str(line_number) + '/' + function_name
if ENABLE_LINE_INFO_TO_STDERR:
by_codes([_colors['RED']], line_info, *args, file=stderr)
by_codes([__colors['RED']], line_info, *args, file=stderr)
else:
by_codes([_colors['RED']], line_info, *args)
by_codes([__colors['RED']], line_info, *args)


def print_stack():
Expand All @@ -142,57 +142,58 @@ def debug(opts, *args):
log(opts, *args)


def _one_opt(opt):
def __one_opt(opt):
return lambda *args: log([opt], *args)


def _bg_color_shorthand(bg_color):
def __bg_color_shorthand(bg_color):
def with_bg_color(*args):
try:
log([bg_color, _fontColorForBGs[bg_color.upper()]], *args)
log([bg_color, __fontColorForBGs[bg_color.upper()]], *args)
except KeyError:
_show('BG_COLORS', _bgColors)
__show('BG__colors', __bgColors)

return with_bg_color


def start(*opts):
codes = _opts_to_codes(opts)
print(_transform_codes(codes), end='')
codes = __opts_to_codes(opts)
print(__transform_codes(codes), end='')


def end():
# Once start with bg color
# Although using RESET to clear bgColor, but the spaces at the end still remain 1 line
# Seems there a bug on osx?
print(_transform_codes(['21', '22', '23', '24', _effects['RESET']]), end='')


black = _one_opt('black')
red = _one_opt('red')
green = _one_opt('green')
yellow = _one_opt('yellow')
blue = _one_opt('blue')
magenta = _one_opt('magenta')
cyan = _one_opt('cyan')
white = _one_opt('white')

bold = _one_opt('bold')
faint = _one_opt('faint')
italic = _one_opt('italic')
underline = _one_opt('underline')

bgBlack = _bg_color_shorthand('bg_black')
bgRed = _bg_color_shorthand('bg_red')
bgGreen = _bg_color_shorthand('bg_green')
bgYellow = _bg_color_shorthand('bg_yellow')
bgBlue = _bg_color_shorthand('bg_blue')
bgMagenta = _bg_color_shorthand('bg_magenta')
bgCyan = _bg_color_shorthand('bg_cyan')
bgWhite = _bg_color_shorthand('bg_white')
print(__transform_codes(['21', '22', '23', '24', __effects['RESET']]), end='')


black = __one_opt('black')
red = __one_opt('red')
green = __one_opt('green')
yellow = __one_opt('yellow')
blue = __one_opt('blue')
magenta = __one_opt('magenta')
cyan = __one_opt('cyan')
white = __one_opt('white')

bold = __one_opt('bold')
faint = __one_opt('faint')
italic = __one_opt('italic')
underline = __one_opt('underline')

#below are functions so they should be snake_case
bg_black = __bg_color_shorthand('bg_black')
bg_red = __bg_color_shorthand('bg_red')
bg_green = __bg_color_shorthand('bg_green')
bg_yellow = __bg_color_shorthand('bg_yellow')
bg_blue = __bg_color_shorthand('bg_blue')
bg_magenta = __bg_color_shorthand('bg_magenta')
bg_cyan = __bg_color_shorthand('bg_cyan')
bg_white = __bg_color_shorthand('bg_white')

if __name__ == '__main__':
by_codes([_colors['RED'], _effects['ITALIC'], _bgColors['BG_GREEN']], 1, 2, 3)
by_codes([__colors['RED'], __effects['ITALIC'], __bgColors['BG_GREEN']], 1, 2, 3)

log(['red', 'Bg_Yellow', 'wrongOpt'], 3, 4, 5)
debug(['BG_CYAN'], 'this is debug msg')
Expand All @@ -214,7 +215,7 @@ def a():
green(123, 'abc')
cyan('aa')

bgYellow('log with bg yellow')
bg_yellow('log with bg yellow')
print('start', 'italic blue bg_green')

start('italic', 'blue', 'bg_green')
Expand Down
3 changes: 1 addition & 2 deletions ctlogger/test_logger.py
Expand Up @@ -4,12 +4,11 @@
def test_print_green_color(capsys):
# arrange
msg = 'Hi'
expect_colored_msg = '\x1b[32m %s \x1b[0m\n' % msg
expect_colored_msg = '\x1b[32m {} \x1b[0m\n'.format(msg)

# action
logger.green(msg)

# assert
out, err = capsys.readouterr()
assert out == expect_colored_msg

0 comments on commit 8045b7c

Please sign in to comment.