Log to a file.

def log(args, kwargs):
    """Log to a file."""
    logfile = os.path.join(args.logdir, 'log.tsv')

    if 'log_created' not in globals():
        if os.path.exists(logfile):
            logging.error('Logfile %s already exists.', logfile)
            sys.exit(1)

        global log_created

        log_created = sorted(kwargs.keys())
        header = '\t'.join((str(k) for k in log_created)) + '\n'
        with open(logfile, 'w') as f:
            f.write(header)

    # Log variables shouldn't change during training
    assert log_created == sorted(kwargs.keys())

    with open(logfile, 'a') as f:
        f.write('\t'.join((str(kwargs[k]) for k in log_created)) + '\n')