Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out logging code from Buildozer class #1632

Merged
merged 6 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 59 additions & 127 deletions buildozer/__init__.py

Large diffs are not rendered by default.

87 changes: 87 additions & 0 deletions buildozer/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
"""
Logger
======

Logger implementation used by Buildozer.

Supports colored output, where available.
"""

from os import environ
from pprint import pformat
import sys

try:
# if installed, it can give color to Windows as well
import colorama

colorama.init()
except ImportError:
colorama = None

# set color codes
if colorama:
RESET_SEQ = colorama.Fore.RESET + colorama.Style.RESET_ALL
COLOR_SEQ = lambda x: x # noqa: E731
BOLD_SEQ = ""
if sys.platform == "win32":
BLACK = colorama.Fore.BLACK + colorama.Style.DIM
else:
BLACK = colorama.Fore.BLACK + colorama.Style.BRIGHT
RED = colorama.Fore.RED
BLUE = colorama.Fore.CYAN
USE_COLOR = "NO_COLOR" not in environ
elif sys.platform != "win32":
RESET_SEQ = "\033[0m"
COLOR_SEQ = lambda x: "\033[1;{}m".format(30 + x) # noqa: E731
BOLD_SEQ = "\033[1m"
BLACK = 0
RED = 1
BLUE = 4
USE_COLOR = "NO_COLOR" not in environ
else:
RESET_SEQ = ""
COLOR_SEQ = ""
BOLD_SEQ = ""
RED = BLUE = BLACK = 0
USE_COLOR = False


class Logger:
ERROR = 0
INFO = 1
DEBUG = 2

LOG_LEVELS_C = (RED, BLUE, BLACK) # Map levels to colors
LOG_LEVELS_T = "EID" # error, info, debug

log_level = ERROR

def log(self, level, msg):
if level > self.log_level:
return
if USE_COLOR:
color = COLOR_SEQ(Logger.LOG_LEVELS_C[level])
print("".join((RESET_SEQ, color, "# ", msg, RESET_SEQ)))
else:
print("{} {}".format(Logger.LOG_LEVELS_T[level], msg))

def debug(self, msg):
self.log(self.DEBUG, msg)

def info(self, msg):
self.log(self.INFO, msg)

def error(self, msg):
self.log(self.ERROR, msg)

def log_env(self, level, env):
"""dump env into logger in readable format"""
self.log(level, "ENVIRONMENT:")
for k, v in env.items():
self.log(level, " {} = {}".format(k, pformat(v)))

@classmethod
def set_level(cls, level):
"""set minimum threshold for log messages"""
cls.log_level = level
17 changes: 10 additions & 7 deletions buildozer/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
from os.path import join

from buildozer.logger import Logger


def no_config(f):
f.__no_config = True
Expand All @@ -14,16 +16,17 @@ def __init__(self, buildozer):
self.build_mode = 'debug'
self.artifact_format = 'apk'
self.platform_update = False
self.logger = Logger()

def check_requirements(self):
pass

def check_configuration_tokens(self, errors=None):
if errors:
self.buildozer.info('Check target configuration tokens')
self.buildozer.error(
self.logger.info('Check target configuration tokens')
self.logger.error(
'{0} error(s) found in the buildozer.spec'.format(
len(errors)))
len(errors)))
for error in errors:
print(error)
exit(1)
Expand All @@ -49,7 +52,7 @@ def get_available_packages(self):

def run_commands(self, args):
if not args:
self.buildozer.error('Missing target command')
self.logger.error('Missing target command')
self.buildozer.usage()
exit(1)

Expand All @@ -68,7 +71,7 @@ def run_commands(self, args):
last_command.append(arg)
else:
if not last_command:
self.buildozer.error('Argument passed without a command')
self.logger.error('Argument passed without a command')
self.buildozer.usage()
exit(1)
last_command.append(arg)
Expand All @@ -80,7 +83,7 @@ def run_commands(self, args):
for item in result:
command, args = item[0], item[1:]
if not hasattr(self, 'cmd_{0}'.format(command)):
self.buildozer.error('Unknown command {0}'.format(command))
self.logger.error('Unknown command {0}'.format(command))
exit(1)

func = getattr(self, 'cmd_{0}'.format(command))
Expand All @@ -106,7 +109,7 @@ def cmd_debug(self, *args):
self.buildozer.build()

def cmd_release(self, *args):
error = self.buildozer.error
error = self.logger.error
self.buildozer.prepare_for_build()
if self.buildozer.config.get("app", "package.domain") == "org.test":
error("")
Expand Down
Loading
Loading