-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
72 lines (66 loc) · 2.36 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
#Logger by Rémi Audrezet #
#=========================================#
# #
#=========================================#
#IMPORTATIONS DES LIBRAIRIES ET MODULES====
import os, tools, sys
from time import localtime
#DEFINITON DES FONCTIONS===================
#PSEUDO CONSTANTES=========================
LOG_REPLACE = "replace"
LOG_APPEND = "append"
#INITIALISATION DES CLASSES================
class Log():
def __init__(self,path="",enable=True,mode=LOG_APPEND):
if mode==LOG_APPEND or mode==LOG_REPLACE:
self._mode=mode
else:
self._mode=LOG_APPEND
if type(enable)==bool:
self._enabled=enable
if path!="":
self.open(path)
else:
self._enabled=False
def _buildTimeStamp(self):
return str(localtime().tm_mday)+"/"+str(localtime().tm_mon)+"/"+str(localtime().tm_year)+"|"+str(localtime().tm_hour)+":"+str(localtime().tm_min)+":"+str(localtime().tm_sec)
def open(self,path):
parent=tools.list_to_str(path.split("/")[0:-1],"/")
try:
if parent[0]!="/":
parent=sys.path[0]+"/"+parent
except:
parent=sys.path[0]+"/"+parent
if os.path.exists(parent) and os.path.exists(path) and os.path.isdir(path)==False:
self._path=path
if self._mode==LOG_REPLACE:
file=open(self._path,"w")
file.close()
return True
elif os.path.exists(parent) and os.path.exists(path)==False:
file=open(path,"w")
file.close()
self._path=path
return True
else:
return False
def enable(self,op):
if type(op)==bool:
self._enabled=op
return True
else:
return False
def enabled(self):
return self._enabled
def write(self,name,content):
if self._enabled:
with open(self._path,"a") as file:
if name!="":
file.write("["+self._buildTimeStamp()+"] <"+name+"> "+content+"\n")
else:
file.write("["+self._buildTimeStamp()+"] "+content+"\n")
file.close()
#==============================================
if __name__=="__main__":
l=Log("data/log.txt",True,"replace")
print("l=Log()")