• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# encoding=utf-8
2from util import time_info
3
4import os
5import sys
6
7
8class Singleton():
9    _instance = None
10    def __new__(cls, *args, **kw):
11        if not hasattr(cls, '_instance'):
12            cls._instance = super(Singleton, cls).__new__(cls, *args, **kw)
13        else:
14            return cls._instance
15
16class Logger():
17
18    __info = "[INFO ]"
19    __error = "[ERROR ]"
20    __debug = "[DEBUG ]"
21    __warning = "[WARNING ]"
22
23    __user = "[USER]"
24
25    log_file_path = ""
26
27    def __init__(self):
28        pass
29
30
31    def switchFilePath(self, path):
32        self.log_file_path = path
33        self.printLog("Write detail log to File: %s" % path)
34
35
36    def __printMessage(self, type, msg, log_level='developer'):
37        '''
38        #====================================================================================
39        #   @Method:        __printMessage()
40        #   @Precondition:  none
41        #   @Func:          print the log
42        #   @PostStatus:    none
43        #   @Param:         type, log type:info, debug, error or worning
44        #   @Param:         msg, log message
45        #   @Example:       logger.__printMessage(self.__info, "test")
46        #   @Author:        zwx205888
47        #   @ModifyRecord:  None
48        #====================================================================================
49        '''
50
51        time_str = time_info.get_now_time_str_info()
52        filename = "unknow file"
53        lineno_str = "unknow line number"
54        try:
55            f = sys._getframe(2)
56            filename = os.path.basename(f.f_code.co_filename)
57            lineno_str = str(f.f_lineno)
58        except Exception as e:
59            print(e)
60        log_message = "%s : [%s - %s line: %s - %s]" % (type, time_str, filename, lineno_str, msg)
61#         print(log_message)
62        self.printIntoLogFile(log_message, log_level)
63
64    def printIntoLogFile(self, log_message, log_level):
65        if log_level == 'user':
66            print(log_message)
67
68        param_file_path, _ = os.path.split(self.log_file_path)
69        if not os.path.exists(param_file_path):
70            os.makedirs(param_file_path)
71        with open(self.log_file_path, 'a', encoding='utf-8') as f:
72            f.write(log_message + '\n')
73
74    def printLog(self, log_message):
75        self.__printMessage(self.__info, log_message, 'user')
76
77    def info(self, msg):
78        '''
79        #====================================================================================
80        #   @Method:        info()
81        #   @Precondition:  none
82        #   @Func:          print the log of info type
83        #   @PostStatus:    none
84        #   @Param:         msg, log message
85        #   @Example:       logger.info("test")
86        #   @Author:        zwx205888
87        #   @ModifyRecord:  None
88        #====================================================================================
89        '''
90        self.__printMessage(self.__info, msg)
91
92    def error(self, msg):
93        '''
94        #====================================================================================
95        #   @Method:        error()
96        #   @Precondition:  none
97        #   @Func:          print the log of error type
98        #   @PostStatus:    none
99        #   @Param:         msg, log message
100        #   @Example:       logger.error("test")
101        #   @Author:        zwx205888
102        #   @ModifyRecord:  None
103        #====================================================================================
104        '''
105        self.__printMessage(self.__error, msg)
106
107    def debug(self, msg):
108        '''
109        #====================================================================================
110        #   @Method:        debug()
111        #   @Precondition:  none
112        #   @Func:          print the log of debug type
113        #   @PostStatus:    none
114        #   @Param:         msg, log message
115        #   @Example:       logger.debug("test")
116        #   @Author:        zwx205888
117        #   @ModifyRecord:  None
118        #====================================================================================
119        '''
120
121        self.__printMessage(self.__debug, msg)
122
123    def warning(self, msg):
124        '''
125        #====================================================================================
126        #   @Method:        warning()
127        #   @Precondition:  none
128        #   @Func:          print the log of warning type
129        #   @PostStatus:    none
130        #   @Param:         msg, log message
131        #   @Example:       logger.warning("test")
132        #   @Author:        zwx205888
133        #   @ModifyRecord:  None
134        #====================================================================================
135        '''
136
137        self.__printMessage(self.__warning, msg)
138
139'''
140#   use logger object
141'''
142logger = Logger()
143