Saturday 24 November 2012

Constant logging

In the upcoming 1.0 release of Whyteboard, there is substantial logging across the program. This can help track down bugs when users report crashes from the program's internal error catching dialog. Whyteboard can now submit a log of the user's actions leading up to the crash as well as the stack trace. I needed the program to operate in several view states: it's always in debug mode but the debugging may not always be visible. I had to create a custom log handler to allow me to 'save' every log message in order to attach it to the error email.
from collections import deque
from logging import StreamHandler, DEBUG
from time import gmtime, strftime

class LogRemembererHandler(StreamHandler):
    '''
    A custom log handler that remembers the last x many logs sent to it
    for retrieving log messages (in this case, we want to fetch the logs 
    if the program crashes)
    '''
    _instance = None
    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super(LogRemembererHandler, cls).__new__(cls, *args, **kwargs)
            cls._instance.logs = deque(maxlen=101)            
        return cls._instance
    
    def emit(self, record):
        self.format(record)
        time = strftime("%d/%m/%Y %H:%M:%S", gmtime(record.created))
        self.logs.append(u"%s: %s" % (time, record.message))
        
    def get_logs(self):
        return self.logs
and to use:
        formatter = logging.Formatter('%(levelname)s %(asctime)s %(message)s')
        handler.setFormatter(formatter)
        logger.addHandler(handler)


...


        LogRemembererHandler().get_logs()

Thursday 19 April 2012

Just do it

I've started a new job as an ASP.NET developer. The development process here is much different to my previous job - no unit tests, all logic crammed into the aspx.vb files, huge methods, no ORM, ad-hoc release process and such.

Thankfully they use version control, however no bug database. I've only been working for 3 days and, while having done no coding I feel that I work better with issue tracking already, especially in a team. The current flow is bug tracking via email or verbally. I've been looking around and found that Fogbugz offers an On Demand service that lets you run Fogbugz on their servers for a free 45 day period.

In an attempt to win over my bosses to issue tracking I'm simply going to use it to track all my work. Any existing issues I come across on our sites will be logged and I may create a per-project milestone for tracking bits of code to refactor.

In regards to unit testing, I'll probably do something similar to a Visual Basic ASP.NET project at my old company where I extract our page logic into a separate DLL that can be unit tested independently, and the aspx.vb pages act as simple controllers that updates the view. I actually like this approach and don't mind doing much view-related programming in these controllers - I like the integration of control IDs to typed variables in the code-behind code.