New Feature, Logger for Amanuensis

in #utopian-io6 years ago

Repository

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

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

What is Amanuensis?

The Amanuensis is an automated songwriting and recording system created by @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

Sort:  
Loading...

Hey @ajmaln
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by ajmaln from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Congratulations @ajmaln! You have completed the following achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard:
SteemitBoard World Cup Contest - France vs Belgium


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!

Just want to say working with @ajmaln was a breeze and I'm looking forward to the possibility of doing it again. He was professional, skilled, knew exactly what I was asking for and prompt. A credit to the utopian ecosystem!

Thank you for your kind words @to-the-sun. Looking forward to working with you again.

Congratulations! This post has been chosen as one of the daily Whistle Stops for The STEEM Engine!

You can see your post's place along the track here: The Daily Whistle Stops, Issue 193 (7/12/18)

The STEEM Engine is an initiative dedicated to promoting meaningful engagement across Steemit. Find out more about us and join us today.

Coin Marketplace

STEEM 0.20
TRX 0.25
JST 0.039
BTC 97371.04
ETH 3473.35
USDT 1.00
SBD 3.20