Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 1 | # Copyright (C) 2023 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """Logic for printing user-friendly logs in repo.""" |
| 16 | |
| 17 | import logging |
Aravind Vasudevan | 712e62b | 2023-09-06 17:25:58 +0000 | [diff] [blame^] | 18 | from typing import List |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 19 | |
| 20 | from color import Coloring |
| 21 | |
Mike Frysinger | 6447733 | 2023-08-21 21:20:32 -0400 | [diff] [blame] | 22 | |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 23 | SEPARATOR = "=" * 80 |
| 24 | |
| 25 | |
Aravind Vasudevan | 712e62b | 2023-09-06 17:25:58 +0000 | [diff] [blame^] | 26 | class _ConfigMock: |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 27 | """Default coloring config to use when Logging.config is not set.""" |
| 28 | |
| 29 | def __init__(self): |
| 30 | self.default_values = {"color.ui": "auto"} |
| 31 | |
| 32 | def GetString(self, x): |
| 33 | return self.default_values.get(x, None) |
| 34 | |
| 35 | |
Aravind Vasudevan | 712e62b | 2023-09-06 17:25:58 +0000 | [diff] [blame^] | 36 | class _LogColoring(Coloring): |
| 37 | """Coloring outstream for logging.""" |
| 38 | |
| 39 | def __init__(self, config): |
| 40 | super().__init__(config, "logs") |
| 41 | self.error = self.colorer("error", fg="red") |
| 42 | self.warning = self.colorer("warn", fg="yellow") |
| 43 | self.levelMap = { |
| 44 | "WARNING": self.warning, |
| 45 | "ERROR": self.error, |
| 46 | } |
| 47 | |
| 48 | |
| 49 | class _LogColoringFormatter(logging.Formatter): |
| 50 | """Coloring formatter for logging.""" |
| 51 | |
| 52 | def __init__(self, config=None, *args, **kwargs): |
| 53 | self.config = config if config else _ConfigMock() |
| 54 | self.colorer = _LogColoring(self.config) |
| 55 | super().__init__(*args, **kwargs) |
| 56 | |
| 57 | def format(self, record): |
| 58 | """Formats |record| with color.""" |
| 59 | msg = super().format(record) |
| 60 | colorer = self.colorer.levelMap.get(record.levelname) |
| 61 | return msg if not colorer else colorer(msg) |
| 62 | |
| 63 | |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 64 | class RepoLogger(logging.Logger): |
| 65 | """Repo Logging Module.""" |
| 66 | |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 67 | def __init__(self, name: str, config=None, **kwargs): |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 68 | super().__init__(name, **kwargs) |
Aravind Vasudevan | 712e62b | 2023-09-06 17:25:58 +0000 | [diff] [blame^] | 69 | handler = logging.StreamHandler() |
| 70 | handler.setFormatter(_LogColoringFormatter(config)) |
| 71 | self.addHandler(handler) |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 72 | |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 73 | def log_aggregated_errors(self, errors: List[Exception]): |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 74 | """Print all aggregated logs.""" |
Aravind Vasudevan | 712e62b | 2023-09-06 17:25:58 +0000 | [diff] [blame^] | 75 | super().error(SEPARATOR) |
| 76 | super().error("Repo command failed due to following errors:") |
| 77 | super().error("\n".join(str(e) for e in errors)) |