• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 
4 #
5 # Copyright (c) 2023 Huawei Device Co., Ltd.
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 
19 import sys
20 import re
21 import os
22 
23 from containers.colors import Colors
24 from helper.noInstance import NoInstance
25 
26 
27 class LogLevel():
28     INFO = 0
29     WARNING = 1
30     ERROR = 2
31     DEBUG = 3
32 
33 
34 class LogUtil(metaclass=NoInstance):
35 
36     @staticmethod
37     def hb_info(msg):
38         level = 'info'
39         for line in str(msg).splitlines():
40             sys.stdout.write(LogUtil.message(level, line))
41             sys.stdout.flush()
42 
43     @staticmethod
44     def hb_warning(msg):
45         level = 'warning'
46         for line in str(msg).splitlines():
47             sys.stderr.write(LogUtil.message(level, line))
48             sys.stderr.flush()
49 
50     @staticmethod
51     def hb_error(msg):
52         level = 'error'
53         for line in str(msg).splitlines():
54             sys.stderr.write(LogUtil.message(level, line))
55             sys.stderr.flush()
56 
57     @staticmethod
58     def message(level, msg):
59         if isinstance(msg, str) and not msg.endswith('\n'):
60             msg += '\n'
61         if level == 'error':
62             msg = msg.replace('error:', f'{Colors.ERROR}error{Colors.END}:')
63             return f'{Colors.ERROR}[OHOS {level.upper()}]{Colors.END} {msg}'
64         elif level == 'info':
65             return f'[OHOS {level.upper()}] {msg}'
66         else:
67             return f'{Colors.WARNING}[OHOS {level.upper()}]{Colors.END} {msg}'
68 
69     @staticmethod
70     def write_log(log_path, msg, level):
71         os.makedirs(os.path.dirname(log_path), exist_ok=True)
72         with open(log_path, 'at', encoding='utf-8') as log_file:
73             for line in str(msg).splitlines():
74                 sys.stderr.write(LogUtil.message(level, line))
75                 sys.stderr.flush()
76                 log_file.write(LogUtil.message(level, line))
77 
78     @staticmethod
79     def get_failed_log(log_path):
80         with open(log_path, 'rt', encoding='utf-8') as log_file:
81             data = log_file.read()
82 
83         failed_pattern = re.compile(
84             r'(\[\d+/\d+\].*?)(?=\[\d+/\d+\]|'
85             'ninja: build stopped)', re.DOTALL)
86         failed_log = failed_pattern.findall(data)
87         for log in failed_log:
88             if 'FAILED:' in log:
89                 LogUtil.hb_error(log)
90 
91         failed_pattern = re.compile(r'(ninja: error:.*?)\n', re.DOTALL)
92         failed_log = failed_pattern.findall(data)
93         for log in failed_log:
94             LogUtil.hb_error(log)
95 
96         error_log = os.path.join(os.path.dirname(log_path), 'error.log')
97         if os.path.isfile(error_log):
98             with open(error_log, 'rt', encoding='utf-8') as log_file:
99                 LogUtil.hb_error(log_file.read())
100