From f31d5e34cdfe9594678316dc100d8c381aac1f43 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 24 Jan 2021 22:42:07 +0100 Subject: Trying to centralize the configuration for logging. Phasing out some parameters which also affects the current ability to create multiple log files for multiple runs. This will be re-introduced when logging is made into a class object in a later version --- archinstall/lib/output.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'archinstall/lib/output.py') diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index 3c1b12e2..267f2635 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -2,6 +2,7 @@ import abc import os import sys import logging +from pathlib import Path from .storage import storage class LOG_LEVELS: @@ -76,30 +77,28 @@ def stylize_output(text :str, *opts, **kwargs): def log(*args, **kwargs): string = orig_string = ' '.join([str(x) for x in args]) + # Attempt to colorize the output if supported + # Insert default colors and override with **kwargs if supports_color(): kwargs = {'bg' : 'black', 'fg': 'white', **kwargs} string = stylize_output(string, **kwargs) - if (logfile := storage.get('logfile', None)) and 'file' not in kwargs: - kwargs['file'] = logfile + # If a logfile is defined in storage, + # we use that one to output everything + if (logfile := storage.get('LOG_FILE', None)): + absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), logfile) + if not os.path.isfile(absolute_logfile): + os.makedirs(os.path.dirname(absolute_logfile)) + Path(absolute_logfile).touch() # Overkill? - # Log to a file output unless specifically told to suppress this feature. - # (level has no effect on the log file, everything will be written there) - if 'file' in kwargs and ('suppress' not in kwargs or kwargs['suppress'] == False): - if type(kwargs['file']) is str: - with open(kwargs['file'], 'a') as log_file: - log_file.write(f"{orig_string}\n") - elif kwargs['file']: - kwargs['file'].write(f"{orig_string}\n") + with open(absolute_logfile, 'a') as log_file: + logfile.write(f"{orig_string}\n") # If we assigned a level, try to log it to systemd's journald. # Unless the level is higher than we've decided to output interactively. # (Remember, log files still get *ALL* the output despite level restrictions) if 'level' in kwargs: - if 'LOG_LEVEL' not in storage: - storage['LOG_LEVEL'] = LOG_LEVELS.Info - - if kwargs['level'] > storage['LOG_LEVEL']: + if kwargs['level'] > storage.get('LOG_LEVEL', LOG_LEVELS.Info): # Level on log message was Debug, but output level is set to Info. # In that case, we'll drop it. return None @@ -111,4 +110,5 @@ def log(*args, **kwargs): # Finally, print the log unless we skipped it based on level. # And we print the string which may or may not contain color formatting. - print(string) \ No newline at end of file + sys.stdout.write(string) + sys.stdout.flush() \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 828a09b9c8750dfb582ccd12d79fa7faa4512415 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 24 Jan 2021 22:57:16 +0100 Subject: Simpligied installer.log() to wrap output.log() with it's changes. --- archinstall/lib/installer.py | 15 ++++----------- archinstall/lib/output.py | 8 +++++++- 2 files changed, 11 insertions(+), 12 deletions(-) (limited to 'archinstall/lib/output.py') diff --git a/archinstall/lib/installer.py b/archinstall/lib/installer.py index 025c15cb..1218b840 100644 --- a/archinstall/lib/installer.py +++ b/archinstall/lib/installer.py @@ -41,7 +41,6 @@ class Installer(): self.init_time = time.strftime('%Y-%m-%d_%H-%M-%S') self.milliseconds = int(str(time.time()).split('.')[1]) - if logdir: storage['LOG_PATH'] = logdir if logfile: @@ -60,16 +59,10 @@ class Installer(): self.boot_partition = boot_partition def log(self, *args, level=LOG_LEVELS.Debug, **kwargs): - if not file: - if 'logfile' not in storage: - log_root = os.path.join(os.path.expanduser('~/'), '.cache/archinstall') - if not os.path.isdir(log_root): - os.makedirs(log_root) - - storage['logfile'] = f"{log_root}/install-session_{self.init_time}.{self.milliseconds}.log" - - file = storage['logfile'] - + """ + installer.log() wraps output.log() mainly to set a default log-level for this install session. + Any manual override can be done per log() call. + """ log(*args, level=level, **kwargs) def __enter__(self, *args, **kwargs): diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index 267f2635..52b2ce2c 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -37,6 +37,11 @@ class journald(dict): # Fallback logger log_adapter.debug(message) +# TODO: Replace log() for session based logging. +class SessionLogging(): + def __init__(self): + pass + # Found first reference here: https://stackoverflow.com/questions/7445658/how-to-detect-if-the-console-does-support-ansi-escape-codes-in-python # And re-used this: https://github.com/django/django/blob/master/django/core/management/color.py#L12 def supports_color(): @@ -109,6 +114,7 @@ def log(*args, **kwargs): pass # Ignore writing to journald # Finally, print the log unless we skipped it based on level. - # And we print the string which may or may not contain color formatting. + # We use sys.stdout.write()+flush() instead of print() to try and + # fix issue #94 sys.stdout.write(string) sys.stdout.flush() \ No newline at end of file -- cgit v1.2.3-70-g09d2 From ca2408a3d290a37d0b955ea3368f7eabb73d20a3 Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 24 Jan 2021 23:31:41 +0100 Subject: Mixup with variable name --- archinstall/lib/output.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'archinstall/lib/output.py') diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index 52b2ce2c..baa454ec 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -90,14 +90,14 @@ def log(*args, **kwargs): # If a logfile is defined in storage, # we use that one to output everything - if (logfile := storage.get('LOG_FILE', None)): - absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), logfile) + if (filename := storage.get('LOG_FILE', None)): + absolute_logfile = os.path.join(storage.get('LOG_PATH', './'), filename) if not os.path.isfile(absolute_logfile): os.makedirs(os.path.dirname(absolute_logfile)) Path(absolute_logfile).touch() # Overkill? with open(absolute_logfile, 'a') as log_file: - logfile.write(f"{orig_string}\n") + log_file.write(f"{orig_string}\n") # If we assigned a level, try to log it to systemd's journald. # Unless the level is higher than we've decided to output interactively. -- cgit v1.2.3-70-g09d2 From a58331868e784886764873c7d85573d74f36248a Mon Sep 17 00:00:00 2001 From: Anton Hvornum Date: Sun, 24 Jan 2021 23:41:45 +0100 Subject: Added debug output as well as corrected output formatting in log(). --- archinstall/lib/luks.py | 2 +- archinstall/lib/output.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'archinstall/lib/output.py') diff --git a/archinstall/lib/luks.py b/archinstall/lib/luks.py index 1c78bc83..e1f14bab 100644 --- a/archinstall/lib/luks.py +++ b/archinstall/lib/luks.py @@ -26,7 +26,7 @@ class luks2(): def encrypt(self, partition, password, key_size=512, hash_type='sha512', iter_time=10000, key_file=None): # TODO: We should be able to integrate this into the main log some how. # Perhaps post-mortem? - log(f'Encrypting {partition}', level=LOG_LEVELS.Info) + log(f'Encrypting {partition} (This might take a while)', level=LOG_LEVELS.Info) if not key_file: key_file = f"/tmp/{os.path.basename(self.partition.path)}.disk_pw" # TODO: Make disk-pw-file randomly unique? diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index baa454ec..956ad0c4 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -116,5 +116,5 @@ def log(*args, **kwargs): # Finally, print the log unless we skipped it based on level. # We use sys.stdout.write()+flush() instead of print() to try and # fix issue #94 - sys.stdout.write(string) + sys.stdout.write(f"{string}\n") sys.stdout.flush() \ No newline at end of file -- cgit v1.2.3-70-g09d2