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 |
| 18 | import multiprocessing |
| 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 | |
| 26 | class LogColoring(Coloring): |
| 27 | """Coloring outstream for logging.""" |
| 28 | |
| 29 | def __init__(self, config): |
| 30 | super().__init__(config, "logs") |
| 31 | self.error = self.colorer("error", fg="red") |
| 32 | self.warning = self.colorer("warn", fg="yellow") |
| 33 | |
| 34 | |
| 35 | class ConfigMock: |
| 36 | """Default coloring config to use when Logging.config is not set.""" |
| 37 | |
| 38 | def __init__(self): |
| 39 | self.default_values = {"color.ui": "auto"} |
| 40 | |
| 41 | def GetString(self, x): |
| 42 | return self.default_values.get(x, None) |
| 43 | |
| 44 | |
| 45 | class RepoLogger(logging.Logger): |
| 46 | """Repo Logging Module.""" |
| 47 | |
| 48 | # Aggregates error-level logs. This is used to generate an error summary |
| 49 | # section at the end of a command execution. |
| 50 | errors = multiprocessing.Manager().list() |
| 51 | |
| 52 | def __init__(self, name, config=None, **kwargs): |
| 53 | super().__init__(name, **kwargs) |
| 54 | self.config = config if config else ConfigMock() |
| 55 | self.colorer = LogColoring(self.config) |
| 56 | |
| 57 | def error(self, msg, *args, **kwargs): |
| 58 | """Print and aggregate error-level logs.""" |
| 59 | colored_error = self.colorer.error(msg, *args) |
| 60 | RepoLogger.errors.append(colored_error) |
| 61 | |
| 62 | super().error(colored_error, **kwargs) |
| 63 | |
| 64 | def warning(self, msg, *args, **kwargs): |
| 65 | """Print warning-level logs with coloring.""" |
| 66 | colored_warning = self.colorer.warning(msg, *args) |
| 67 | super().warning(colored_warning, **kwargs) |
| 68 | |
| 69 | def log_aggregated_errors(self): |
| 70 | """Print all aggregated logs.""" |
| 71 | super().error(self.colorer.error(SEPARATOR)) |
| 72 | super().error( |
| 73 | self.colorer.error("Repo command failed due to following errors:") |
| 74 | ) |
| 75 | super().error("\n".join(RepoLogger.errors)) |