Skip to content

Commit

Permalink
Merge pull request #33 from plesk/avoid-encoding-problem
Browse files Browse the repository at this point in the history
Use utf-8 encoding on file IO to avoid problems on host with changed default encoding
  • Loading branch information
SandakovMM authored May 22, 2024
2 parents 9ba66e4 + 4d18a18 commit df0f72c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
12 changes: 12 additions & 0 deletions pleskdistup/common/src/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ def init_logger(logfiles: typing.List[str], streams: typing.List[typing.Any],
for handler in stream_handlers:
logger.streams_logger.addHandler(handler)

@staticmethod
def reinit_logger(logfiles: typing.List[str], streams: typing.List[typing.Any],
console: bool = False, loglevel: int = logging.INFO) -> None:
logger.files_logger = logging.getLogger("distupgrade_files")
logger.streams_logger = logging.getLogger("distupgrade_streams")
logger.init_logger(logfiles, streams, console, loglevel)

@staticmethod
def debug(msg: str, to_file: bool = True, to_stream: bool = True) -> None:
if to_file:
Expand Down Expand Up @@ -75,6 +82,11 @@ def init_logger(logfiles: typing.List[str], streams: typing.List[typing.Any],
logger.init_logger(logfiles, streams, console, loglevel)


def reinit_logger(logfiles: typing.List[str], streams: typing.List[typing.Any],
console: bool = False, loglevel: int = logging.INFO) -> None:
logger.reinit_logger(logfiles, streams, console, loglevel)


def debug(msg: str, to_file: bool = True, to_stream: bool = True) -> None:
logger.debug(msg, to_file, to_stream)

Expand Down
10 changes: 8 additions & 2 deletions pleskdistup/common/src/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ def __exit__(self, *args):


class StdoutWriter(Writer):
def __init__(self):
self.out = open(1, 'w', closefd=False)

def write(self, message: str) -> None:
sys.stdout.write(message)
sys.stdout.flush()
self.out.write(message)
self.out.flush()

def __exit__(self, *args):
self.out.close()


class FileWriter(Writer):
Expand Down
19 changes: 19 additions & 0 deletions pleskdistup/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ def main():
)
# Overrides safety checks, dangerous
parser.add_argument("--I-know-what-I-am-doing", action="store_true", dest="unsafe_mode", help=argparse.SUPPRESS)
parser.add_argument("--locale", default=None, help=argparse.SUPPRESS)
parser.add_argument("--log-file", default=f"/var/log/plesk/{util_name}.log", help="path to the log file.")
operation_group.add_argument(
"--monitor", action="store_true",
Expand Down Expand Up @@ -373,6 +374,13 @@ def main():
else:
options.help = False

# Configure locale to avoid problems on systems where LANG or LC_CTYPE changed,
# while files on the system still has utf-8 encoding
# We should do it before initializing logger to make sure utf-8 symbols will be
# written correctly to the log file.
if options.locale is not None:
locale.setlocale(locale.LC_CTYPE, options.locale)

logfile_path = options.log_file
log.init_logger(
[logfile_path],
Expand Down Expand Up @@ -418,6 +426,17 @@ def main():
resume_options.resume_path = options.resume_path
resume_options.resume_data = resume_data
options = resume_options

# Recreate logger on resume to make sure that right log files are opened
if resume_options.local is not None:
locale.setlocale(locale.LC_CTYPE, resume_options.locale)

logfile_path = resume_options.log_file
log.reinit_logger(
[logfile_path],
[],
loglevel=getattr(logging, resume_options.verbose, logging.DEBUG)
)
except Exception as ex:
ex_info = traceback.format_exc()
printerr(f"Couldn't resume from {options.resume_path!r}: {ex}\n{ex_info}")
Expand Down

0 comments on commit df0f72c

Please sign in to comment.