# script_logger.py - script functions developed for StarDrop Script Plugins
#
# Use of this code is subject to the terms of the StarDrop License Agreement
# and use, copying or redistribution of this file for use by other persons or
# organisations is prohibited.
#
# Copyright(C) Optibrium Ltd 2026
# support@optibrium.com
#

"""
Module to manage logging of script plugin activity
"""

import os
import datetime


class ScriptLogger:
    """ScriptLogger represents a single log file that can be writen to."""

    def __init__(self, filename, purge=True):
        """Construct a ScriptLogger with a file.
        @param: filename: The full path to the file to be used for logging
        @param: purge: If true, log entries will be created in an empty file, otherwise appended to existing file
        """
        self.filename = filename
        if purge or not os.path.isfile(self.filename):
            fout = open(self.filename, 'w')
            fout.close()

    def log(self, message):
        """Write a message to the log file.
        @param: message: The text to log
        """
        fout = open(self.filename, 'a')
        dt = datetime.datetime.now()
        fout.write(dt.strftime('%d/%m/%Y %H:%M ') + str(message.encode('utf-8')) + '\n')
        fout.close()
