
import os.path


from mulib import eventrouter


class Console(eventrouter.Router):
    transforms = os.path.join(os.path.dirname(__file__), 'logconsole.js')
    stylesheet = os.path.join(os.path.dirname(__file__), 'logconsole.css')

    def __init__(self, my_ip, *args, **kw):
        self.my_ip = my_ip
        eventrouter.Router.__init__(self, *args, **kw)

    def event_connect(self, target):
        return "logconsole running on %s" % (self.my_ip, )

    def emit(self, output):
        self.broadcast('log', output, log=False)

    def log(self, *message):
        output = ' '.join([str(x) for x in message])
        self.broadcast('log', output, log=False)

    def log_request(self, request_id, method, netloc, path, headers, body):
        self.broadcast('log-request', {'request-id': request_id, 'method': method, 'url': netloc, 'path': path, 'headers': headers, 'body': body}, log=False)

    def log_response(self, request_id, status, headers, body):
        self.broadcast('log-response', {'request-id': request_id, 'status': status, 'headers': headers, 'body': body}, log=False)

    def log_transform(self, (transform, data)):
        self.broadcast('log-transform', (transform, data), log=False)

    def log_send_data(self, (post, target, body)):
        self.broadcast('log-send-data', (post, target, body), log=False)

