forked from MrS0m30n3/youtube-dl-gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logmanager.py
98 lines (70 loc) · 2.55 KB
/
logmanager.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""Youtubedlg module responsible for handling the log stuff. """
from __future__ import unicode_literals
import os.path
from time import strftime
from .utils import (
os_path_exists,
get_encoding,
check_path
)
class LogManager(object):
"""Simple log manager for youtube-dl.
This class is mainly used to log the youtube-dl STDERR.
Attributes:
LOG_FILENAME (string): Filename of the log file.
TIME_TEMPLATE (string): Custom template to log the time.
MAX_LOGSIZE (int): Maximum size(Bytes) of the log file.
Args:
config_path (string): Absolute path where LogManager should
store the log file.
add_time (boolean): If True LogManager will also log the time.
"""
LOG_FILENAME = "log"
TIME_TEMPLATE = "[{time}] {error_msg}"
MAX_LOGSIZE = 524288 # Bytes
def __init__(self, config_path, add_time=False):
self.config_path = config_path
self.add_time = add_time
self.log_file = os.path.join(config_path, self.LOG_FILENAME)
self._encoding = get_encoding()
self._init_log()
self._auto_clear_log()
def log_size(self):
"""Return log file size in Bytes. """
if not os_path_exists(self.log_file):
return 0
return os.path.getsize(self.log_file)
def clear(self):
"""Clear log file. """
self._write('', 'w')
def log(self, data):
"""Log data to the log file.
Args:
data (string): String to write to the log file.
"""
if isinstance(data, basestring):
self._write(data + '\n', 'a')
def _write(self, data, mode):
"""Write data to the log file.
That's the main method for writing to the log file.
Args:
data (string): String to write on the log file.
mode (string): Can be any IO mode supported by python.
"""
check_path(self.config_path)
with open(self.log_file, mode) as log:
if mode == 'a' and self.add_time:
msg = self.TIME_TEMPLATE.format(time=strftime('%c'), error_msg=data)
else:
msg = data
log.write(msg.encode(self._encoding, 'ignore'))
def _init_log(self):
"""Initialize the log file if not exist. """
if not os_path_exists(self.log_file):
self._write('', 'w')
def _auto_clear_log(self):
"""Auto clear the log file. """
if self.log_size() > self.MAX_LOGSIZE:
self.clear()