[TIC-CORE] change the license from apache 2.0 to flora 1.1
[archive/20170607/tools/tic-core.git] / tic / utils / log.py
1 #!/usr/bin/python
2 # Copyright (c) 2016 Samsung Electronics Co., Ltd
3 #
4 # Licensed under the Flora License, Version 1.1 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 #     http://floralicense.org/license/
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Contributors:
17 # - S-Core Co., Ltd
18
19 import logging.handlers
20 import os
21 import errno
22
23 LOG_FILE_MAX_BYTES=10*1024*1024
24 TIC_LOG_DIR='/var/tmp/tic-core/log'
25
26 def setup(root):
27     logger = logging.getLogger(root)
28     logger.setLevel(logging.DEBUG)
29     
30     formatter = logging.Formatter('%(asctime)s [%(levelname)s][%(filename)s(%(lineno)s)-%(funcName)s()] %(message)s')
31     formatter.datefmt = '%Y-%m-%d %H:%M:%S'
32     
33     mkdir_p(TIC_LOG_DIR)
34     fileHandler = logging.handlers.RotatingFileHandler(os.path.join(TIC_LOG_DIR, 'tic-core.log'), maxBytes=LOG_FILE_MAX_BYTES, backupCount=10)
35     streamHandler = logging.StreamHandler()
36     
37     fileHandler.setFormatter(formatter)
38     streamHandler.setFormatter(formatter)
39     
40     logger.addHandler(fileHandler)
41     logger.addHandler(streamHandler)
42     
43 def mkdir_p(path):
44     try:
45         os.makedirs(path)
46     except OSError as err:
47         if err.errno != errno.EEXIST:
48             raise