blob: e94af7df16daa1940af38430bad9c926c0ed4c3f [file] [log] [blame]
Aravind Vasudevan8c35d942023-07-26 19:16:59 +00001# 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
17import logging
Aravind Vasudevane914ec22023-08-31 20:57:31 +000018from typing import Any, List
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000019
20from color import Coloring
21
Mike Frysinger64477332023-08-21 21:20:32 -040022
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000023SEPARATOR = "=" * 80
24
25
26class 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
35class 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
45class RepoLogger(logging.Logger):
46 """Repo Logging Module."""
47
Aravind Vasudevane914ec22023-08-31 20:57:31 +000048 def __init__(self, name: str, config=None, **kwargs):
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000049 super().__init__(name, **kwargs)
50 self.config = config if config else ConfigMock()
51 self.colorer = LogColoring(self.config)
52
Aravind Vasudevane914ec22023-08-31 20:57:31 +000053 def error(self, msg: Any, *args, **kwargs):
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000054 """Print and aggregate error-level logs."""
Aravind Vasudevane914ec22023-08-31 20:57:31 +000055 colored_error = self.colorer.error(str(msg), *args)
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000056 super().error(colored_error, **kwargs)
57
Aravind Vasudevane914ec22023-08-31 20:57:31 +000058 def warning(self, msg: Any, *args, **kwargs):
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000059 """Print warning-level logs with coloring."""
Aravind Vasudevane914ec22023-08-31 20:57:31 +000060 colored_warning = self.colorer.warning(str(msg), *args)
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000061 super().warning(colored_warning, **kwargs)
62
Aravind Vasudevane914ec22023-08-31 20:57:31 +000063 def log_aggregated_errors(self, errors: List[Exception]):
Aravind Vasudevan8c35d942023-07-26 19:16:59 +000064 """Print all aggregated logs."""
65 super().error(self.colorer.error(SEPARATOR))
66 super().error(
67 self.colorer.error("Repo command failed due to following errors:")
68 )
Aravind Vasudevane914ec22023-08-31 20:57:31 +000069 super().error("\n".join(map(str, errors)))