Skip to content

Commit

Permalink
Log argument to save log file.
Browse files Browse the repository at this point in the history
  • Loading branch information
PetterKraabol committed Aug 2, 2019
1 parent 7a44a9c commit 360d86f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions tcd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def main():
default=str(Path.home()) + '/.config/tcd/settings.json',
help='Settings file location')
parser.add_argument(f'--{Arguments.Name.DEBUG}', action='store_true', help='Print debug messages')
parser.add_argument(f'--{Arguments.Name.LOG}', action='store_true', help='Save log file')

Arguments(parser.parse_args().__dict__)
Settings(Arguments().settings_file,
Expand Down
2 changes: 2 additions & 0 deletions tcd/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Name:
FORMAT: str = 'format'
TIMEZONE: str = 'timezone'
DEBUG: str = 'debug'
LOG: str = 'log'

def __init__(self, arguments: Optional[Dict[str, Union[str, bool, int]]] = None):
"""
Expand All @@ -49,6 +50,7 @@ def __init__(self, arguments: Optional[Dict[str, Union[str, bool, int]]] = None)
self.print_formats: bool = arguments[Arguments.Name.FORMATS]
self.print_version: bool = arguments[Arguments.Name.VERSION]
self.output: str = arguments[Arguments.Name.OUTPUT]
self.log: bool = arguments[Arguments.Name.LOG]

# Optional or prompted arguments
self.client_id: Optional[str] = arguments[Arguments.Name.CLIENT_ID]
Expand Down
2 changes: 2 additions & 0 deletions tcd/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def video(self, video: Video) -> None:
# Special case for JSON
# Build JSON object before writing it
if 'json' in self.formats:
Logger().log('Downloading JSON data', Log.VERBOSE)
output: str = Pipe(Settings().config['formats']['json']['output']).output(video.data)
os.makedirs(os.path.dirname(output), exist_ok=True)

Expand Down Expand Up @@ -129,6 +130,7 @@ def video(self, video: Video) -> None:

# For each format (ignore json this time)
for format_name in [x for x in self.formats if x not in ['json']]:
Logger().log(f'Formatting chat using: {format_name}', Log.VERBOSE)

# Get (formatted_comment, comment), output
comment_tuple, output = formatter.use(format_name)
Expand Down
11 changes: 8 additions & 3 deletions tcd/logger.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
from datetime import datetime
from typing import List, Optional

from .arguments import Arguments
Expand All @@ -15,9 +16,9 @@ class Log:
PROGRESS: str = 'progress'

def __init__(self, message: str = '', log_type: str = REGULAR):
self.message: str = message
self.message: str = message.strip()
self.type: str = log_type
self.timestamp: float = time.time()
self.timestamp: str = datetime.utcfromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')

def __str__(self) -> str:
if self.type in [Log.DEBUG, Log.ERROR, Log.CRITICAL]:
Expand Down Expand Up @@ -53,6 +54,10 @@ def log(self, message: str = '', log_type: str = Log.REGULAR, retain: bool = Tru
if retain and log.type is not Log.PREVIEW:
self.logs.append(log)

# Save log when debugging
if Arguments().log:
self.save()

# Print
if self.should_print_type(log.type):
print(log)
Expand All @@ -75,7 +80,7 @@ def should_print_type(log_type: str) -> bool:
return False

# Progress - default output
if log_type == Log.PROGRESS and (Arguments().debug or Arguments().verbose or Arguments().preview):
if log_type == Log.PROGRESS and (Arguments().verbose or Arguments().preview):
return False

# Debug
Expand Down

0 comments on commit 360d86f

Please sign in to comment.