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 | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 18 | |
| 19 | from color import Coloring |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 20 | from error import RepoExitError |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 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 |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 24 | MAX_PRINT_ERRORS = 5 |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 25 | |
| 26 | |
Aravind Vasudevan | 712e62b | 2023-09-06 17:25:58 +0000 | [diff] [blame] | 27 | class _ConfigMock: |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 28 | """Default coloring config to use when Logging.config is not set.""" |
| 29 | |
| 30 | def __init__(self): |
| 31 | self.default_values = {"color.ui": "auto"} |
| 32 | |
| 33 | def GetString(self, x): |
| 34 | return self.default_values.get(x, None) |
| 35 | |
| 36 | |
Aravind Vasudevan | 712e62b | 2023-09-06 17:25:58 +0000 | [diff] [blame] | 37 | class _LogColoring(Coloring): |
| 38 | """Coloring outstream for logging.""" |
| 39 | |
| 40 | def __init__(self, config): |
| 41 | super().__init__(config, "logs") |
Shik Chen | 9bf8236 | 2024-07-01 18:51:33 +0800 | [diff] [blame] | 42 | self.error = self.nofmt_colorer("error", fg="red") |
| 43 | self.warning = self.nofmt_colorer("warn", fg="yellow") |
Aravind Vasudevan | 712e62b | 2023-09-06 17:25:58 +0000 | [diff] [blame] | 44 | self.levelMap = { |
| 45 | "WARNING": self.warning, |
| 46 | "ERROR": self.error, |
| 47 | } |
| 48 | |
| 49 | |
| 50 | class _LogColoringFormatter(logging.Formatter): |
| 51 | """Coloring formatter for logging.""" |
| 52 | |
| 53 | def __init__(self, config=None, *args, **kwargs): |
| 54 | self.config = config if config else _ConfigMock() |
| 55 | self.colorer = _LogColoring(self.config) |
| 56 | super().__init__(*args, **kwargs) |
| 57 | |
| 58 | def format(self, record): |
| 59 | """Formats |record| with color.""" |
| 60 | msg = super().format(record) |
| 61 | colorer = self.colorer.levelMap.get(record.levelname) |
| 62 | return msg if not colorer else colorer(msg) |
| 63 | |
| 64 | |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 65 | class RepoLogger(logging.Logger): |
| 66 | """Repo Logging Module.""" |
| 67 | |
Aravind Vasudevan | e914ec2 | 2023-08-31 20:57:31 +0000 | [diff] [blame] | 68 | def __init__(self, name: str, config=None, **kwargs): |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 69 | super().__init__(name, **kwargs) |
Aravind Vasudevan | 712e62b | 2023-09-06 17:25:58 +0000 | [diff] [blame] | 70 | handler = logging.StreamHandler() |
| 71 | handler.setFormatter(_LogColoringFormatter(config)) |
| 72 | self.addHandler(handler) |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 73 | |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 74 | def log_aggregated_errors(self, err: RepoExitError): |
Aravind Vasudevan | 8c35d94 | 2023-07-26 19:16:59 +0000 | [diff] [blame] | 75 | """Print all aggregated logs.""" |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 76 | self.error(SEPARATOR) |
| 77 | |
| 78 | if not err.aggregate_errors: |
| 79 | self.error("Repo command failed: %s", type(err).__name__) |
Josh Bartel | 6d82112 | 2023-11-10 15:49:42 -0600 | [diff] [blame] | 80 | self.error("\t%s", str(err)) |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 81 | return |
| 82 | |
| 83 | self.error( |
| 84 | "Repo command failed due to the following `%s` errors:", |
| 85 | type(err).__name__, |
| 86 | ) |
| 87 | self.error( |
| 88 | "\n".join(str(e) for e in err.aggregate_errors[:MAX_PRINT_ERRORS]) |
| 89 | ) |
| 90 | |
| 91 | diff = len(err.aggregate_errors) - MAX_PRINT_ERRORS |
Aravind Vasudevan | 83c66ec | 2023-09-28 19:06:59 +0000 | [diff] [blame] | 92 | if diff > 0: |
Aravind Vasudevan | b8fd192 | 2023-09-14 22:54:04 +0000 | [diff] [blame] | 93 | self.error("+%d additional errors...", diff) |