import service

import errno, os

class LogSetting(service.Setting):
    __config__ = ["logdir"]

    def __init__(self, logdir):
        service.Setting.__init__(self)
        self.logdir = logdir
        try:
            os.makedirs(logdir)
        except OSError, e:
            if e.errno != errno.EEXIST:
                raise

    def link(self, d):
        self.__d = d

    def saveConfig(self):
        """Save the configuration of the directory containing this
        object to a file in the log directory.  This should be called
        after linking the directory."""

        f = file(os.path.join(self.logdir, "config"), "w")
        for s in self.__d.allSettings() + self.__d.allServices():
            print >> f, "[%s]" % s.getName()
            for (name, val) in s.getConfig():
                print >> f, "%s: %s" % (name, `val`)
            print >> f
        f.close()
