<commit_before>"""
Logging module
"""

from .util.decorators import initializer
import time
import sys

logfile = None

@initializer
def plugin_init(bot):
    global logfile
    output = "%s/james.log" % bot.botdir
    logfile = open(output, "a", encoding="utf-8")
    for event in bot.state.events.values():
        event.register(logger)

def close_log(*args):
    logfile.close()

def log(data):
    """ log and print data """
    timestamp = time.strftime("[%H:%M:%S] ")
    if sys.stdout != sys.__stdout__:
        sys.__stdout__.write((timestamp+data+"\n").encode("utf-8").decode("utf-8"))
    else:
        try:
            print((timestamp+data).encode("utf-8").decode("utf-8"))
        except:
            pass
    if not logfile.closed:
        logfile.write(timestamp+data+"\n")

def log_message(*args):
    nick = args[1]
    chan = args[2]
    msg = args[-1] # or 3
    ws = " "*(20 - len("[%s]" % (chan)))
    log("[%s]%s<%s> %s" % (chan, ws, nick, msg))


def log_join(*args):
    user = args[1]
    chan = args[2]
    ws = " "*(20 - len("[%s]" % (chan)))
    log("[%s]%sJOIN %s" % (chan, ws, user))

def log_part(*args):
    user = args[1]
    chan = args[2]
    ws = " "*(20 - len("[%s]" % (chan)))
    log("[%s]%sPART %s" % (chan, ws, user))

def log_welcome(*args):
    log("Server has welcomed us.")

def log_kick(*args):
    (bot, user, chan) = args
    ws = " "*(20 - len("[%s]" % (chan)))
    log("[%s]%sKICK %s" % (chan, ws, user))

def log_notice(*args):
    sender = args[1]
    args = args[2]
    ws = " "*(20 - len("-%s-" % (sender)))
    log("-%s-%s%s" % (sender, ws, args))

def logger(*args, **kwargs):
    etype = kwargs["type"]
    handlers = {"message": log_message,
     "join": log_join,
     "part": log_part,
     "welcome": log_welcome,
     "notice": log_notice,
     "kick": log_kick,
     "close_log": close_log}
    handlers.get(etype, lambda *args: None)(*args)

logger._want_type = True<commit_msg>Add logfile.flush() and os.fsync() to the logger so that the file updates quicker<commit_after>"""
Logging module
"""

from .util.decorators import initializer
import time
import sys
import os

logfile = None

@initializer
def plugin_init(bot):
    global logfile
    output = "%s/james.log" % bot.botdir
    logfile = open(output, "a", encoding="utf-8")
    for event in bot.state.events.values():
        event.register(logger)

def close_log(*args):
    logfile.close()

def log(data):
    """ log and print data """
    timestamp = time.strftime("[%H:%M:%S] ")
    if sys.stdout != sys.__stdout__:
        sys.__stdout__.write((timestamp+data+"\n").encode("utf-8").decode("utf-8"))
    else:
        try:
            print((timestamp+data).encode("utf-8").decode("utf-8"))
        except:
            pass
    if not logfile.closed:
        logfile.write(timestamp+data+"\n")

def log_message(*args):
    nick = args[1]
    chan = args[2]
    msg = args[-1] # or 3
    ws = " "*(20 - len("[%s]" % (chan)))
    log("[%s]%s<%s> %s" % (chan, ws, nick, msg))


def log_join(*args):
    user = args[1]
    chan = args[2]
    ws = " "*(20 - len("[%s]" % (chan)))
    log("[%s]%sJOIN %s" % (chan, ws, user))

def log_part(*args):
    user = args[1]
    chan = args[2]
    ws = " "*(20 - len("[%s]" % (chan)))
    log("[%s]%sPART %s" % (chan, ws, user))

def log_welcome(*args):
    log("Server has welcomed us.")

def log_kick(*args):
    (bot, user, chan) = args
    ws = " "*(20 - len("[%s]" % (chan)))
    log("[%s]%sKICK %s" % (chan, ws, user))

def log_notice(*args):
    sender = args[1]
    args = args[2]
    ws = " "*(20 - len("-%s-" % (sender)))
    log("-%s-%s%s" % (sender, ws, args))

def logger(*args, **kwargs):
    etype = kwargs["type"]
    handlers = {"message": log_message,
     "join": log_join,
     "part": log_part,
     "welcome": log_welcome,
     "notice": log_notice,
     "kick": log_kick,
     "close_log": close_log}
    handlers.get(etype, lambda *args: None)(*args)

    logfile.flush()
    os.fsync()

logger._want_type = True
{'fraction_non_alphanumeric': 0.206, 'fraction_numerical': 0.0125, 'mean_word_length': 2.8996101364522415, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '763536', 'n_tokens_mistral': 1548, 'n_tokens_neox': 1499, 'n_words': 345}