# synarere -- a trivial Python IRC bot.
# Copyright (C) 2010 Michael Rodriguez.
# Rights to this code are documented in docs/LICENSE.

"""Facility for handling logging information to a file."""

# Import required Python modules.
import os, sys
from time import strftime

# Import required source module.
import vars

class Logger:
    def __init__(self, file):
        self.file = file

        try:
            self.lfile = open(self.file, 'w')
        except IOError, e:
            print 'Unable to open log file %s: %s.' % (self.file, os.strerror(e.args[0]))
            sys.exit(os.EX_SOFTWARE)

    def log(self, text):
        if not vars.fork:
            print '%s: %s' % (strftime('%Y-%m-%d %H:%M:%S'), text)
 
        self.lfile.write('%s: %s\n' % (strftime('%Y-%m-%d %H:%M:%S'), text))
        self.lfile.flush()
