New Feature, Logger for Amanuensis

Words
313
Reading
2 min
Listen
Play
8y

Repository

https://github.com/to-the-sun/amanuensis

This development contribution is submitted in association with task request by to-the-sun@to-the-sun:
Link to Task Request

What is Amanuensis?

The Amanuensis is an automated songwriting and recording system created by to-the-sun@to-the-sun aimed at ridding the process of anything left-brained, so one need never leave a creative, spontaneous and improvisational state of mind, from the inception of the song until its final master. The program will construct a cohesive song structure, using the best of what you give it, looping around you and growing in real-time as you play. All you have to do is jam and fully written songs will flow out behind you wherever you go.

New Feature

The Logger Script

I created a python script for logging the messages sent from the main application using UDP, which is written to a text file by the script.
There are two types of logs,

  • A default log created in the app directory.
  • A project-specific log that gets created once a project is opened. The location of the log file is determined by a message mentioning the projectPath

The projectPath message was parsed and the path was extracted and handled OS independently by using python's Pathlib library. So in future, even if the supporting platforms are changed the path strings will be handled seamlessly.

I also had to create a thread for implementing a timeout. This was to roll back to the default log file on 30 seconds of inactivity (Not receiving any messages from the app) as required by the Project owner.

Implementation

The script file: https://github.com/to-the-sun/amanuensis/blob/master/logger.py

To implement the timeout, a variable was set to 30, which gets decremented every second in a thread. On arrival of a message, it is reset to 30 again.

Relevant Codes

UDP Socket

logger = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
logger.bind(("127.0.0.1", 10247))

Function to receive the message

def receiveMessage(self):
        """
        Receives a message from the socket and writes to the file.
        The counter is reset on each call.
        Returns the message as an array of strings.
        """
        message = bytes.decode(logger.recv(99999))
        if self.defaultReceiveMode:
            self.setReceiveDefaultMode(False)
            return self.defaultReceive(message)
        else:
            self.resetCounter()
            print(message)
            self.log_file.write(message + "\n")
            return message.split(' ')

Thread to check timeout:

def check_timeout():
    """
    Thread to check if a timeout has occurred.
    Log directory is changed to default on timeout.
    Helps in closing the file every 30 seconds to update it.
    """
    while True:
        if not log.counterTimedOut():
            time.sleep(1)
            log.counter -= 1
        else:
            log.resetCounter()
            if not log.pathIsDefault():
                log.closeLog()
                log.setReceiveDefaultMode(True)
            else:
                log.changeProjectPath(defaultPath)



Function to change log directory:

def changeProjectPath(self, newPath):
        """
        Change the log directory to specified newPath, and create a new log file.
        Returns the new projectPath
        """
        if newPath == defaultPath:
            self.closeLog()
            if self.pathIsDefault():
                self.log_file = self.projectPath.open('a')
            else:
                self.log_file = newPath.open('w')
        elif not newPath == self.projectPath:
            self.closeLog()
            copyfile(str(self.projectPath), str(newPath))
            self.log_file = newPath.open('a')
        self.projectPath = newPath
        return self.projectPath

GitHub Account

https://github.com/ajmaln

New Feature, Logger for Amanuensis | Ecency