forked from merlinthered/sublime-rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
logger.py
84 lines (55 loc) · 1.87 KB
/
logger.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
"""This module provides general methods for logging puprposes.
Basic operations are:
* info
* error
with these operations it is easier to track from where the information is printed
"""
import os
import inspect
from datetime import datetime
import sublime
__LOG = None
'''
Called automatically from ST3 if plugin is loaded
# Is required now due to async call and ignoring sublime.* from main routine
'''
__SETTING_KEY = "rainmeter_enable_logging"
def plugin_loaded():
"""Will be called when sublime API is ready to use."""
settings = __load_settings()
settings.add_on_change(__SETTING_KEY, __load_settings)
info("Logger succesfully loaded.")
def __load_settings():
settings = sublime.load_settings("Rainmeter.sublime-settings")
global __LOG
__LOG = settings.get(__SETTING_KEY, False)
return settings
def info(message):
"""
Display information about the current state it is in.
Only shown if logging is enabled.
"""
if __LOG:
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
caller = calframe[1]
caller_name = caller[3]
caller_file = caller[1]
_log("info", caller_file, caller_name, message)
def error(message):
"""
Display error states.
Always shown because supposed not to reach that level.
"""
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
caller = calframe[1]
caller_name = caller[3]
caller_file = caller[1]
_log("error", caller_file, caller_name, message)
def _log(error_type, file_path, function, string):
now = datetime.now()
timestamp = now.strftime("%H:%M:%S.%f")[:-3]
filename = os.path.basename(file_path)
withoutext = os.path.splitext(filename)[0]
print("[" + timestamp + "]", "[" + error_type + "]", withoutext + "." + function + ':', string)