<commit_before>#  -*- encoding:UTF-8 -*-
#
# NS2 Server Watch dog -- a Natural Selection 2 server monitoring script
#
# Wrote by John <<EMAIL>>
# MIT License.

import datetime
import json
import os
import platform
import shlex
import signal
import sys
import time
from subprocess import Popen

import psutil

from Utils.TextfileWriter import TextfileWriter

if cmp(platform.system(), 'Windows') is 0:
    from Utils.WindowsConsoleWriter import WindowsConsoleWriter as PlatformConsoleWriter
    from subprocess import CREATE_NEW_PROCESS_GROUP
else:
    from Utils.UnixConsoleWriter import UnixConsoleWriter as PlatformConsoleWriter

VERBOSE_LEVEL = 0
ExitFlag = False


class Logger:
    __TIME_LABEL_PATTERN = '%m/%d/%y-%H:%M:%S'
    __console_writer = PlatformConsoleWriter()
    __file_logger = TextfileWriter()
    __LINE_PATTERN = u"[%s] <%s>: %s\n"

    @staticmethod
    def init_logger():
        global VERBOSE_LEVEL
        VERBOSE_LEVEL = ConfigManager.get_config("verbose_level")

    def __init__(self):
        raise NotImplementedError("This class should never be instantiated.")

    @staticmethod
    def debug(text):
        if VERBOSE_LEVEL >= 2:
            log_line = Logger.__LINE_PATTERN % (time.strftime(Logger.__TIME_LABEL_PATTERN, time.localtime(time.time())),
                                                "DEBUG", unicode(text))
            Logger.__console_writer.debug(log_line)
            Logger.__file_logger.log(log_line)

    @staticmethod
    def verbose(text):
        if VERBOSE_LEVEL >= 1:
            log_line = Logger.__LINE_PATTERN % (time.strftime(Logger.__TIME_LABEL_PATTERN, time.localtime(time.time())),
                                                "VERBOSE", unicode(text))
            Logger.__console_writer.verbose(log_line)
            Logger.__file_logger.log(log_line)

    @staticmethod
    def info(text):
        log_line = Logger.__LINE_PATTERN % (time.strftime(Logger.__TIME_LABEL_PATTERN, time.localtime(time.time())),
                                            "INFO", unicode(text))
        Logger.__console_writer.normal(log_line)
        Logger.__file_logger.log(log_line)

    @staticmethod
    def warn(text):
        log_line = Logger.__LINE_PATTERN % (time.strftime(Logger.__TIME_LABEL_PATTERN, time.localtime(time.time())),
                                            "WARN", unicode(text))
        Logger.__console_writer.warn(log_line)
        Logger.__file_logger.log(log_line)

    @staticmethod
    def fatal(text, exitcode=-1):
        log_line = Logger.__LINE_PATTERN % (time.strftime(Logger.__TIME_LABEL_PATTERN, time.localtime(time.time())),
                                            "FATAL", unicode(text))
        Logger.__console_writer.error(log_line)
        Logger.__file_logger.log(log_line)

        log_line = Logger.__LINE_PATTERN % (time.strftime(Logger.__TIME_LABEL_PATTERN, time.localtime(time.time())),
                                            "FATAL", (u"Program terminated, exit code: %d." % exitcode))
        Logger.__console_writer.error(log_line)
        Logger.__file_logger.log(log_line)
        sys.exit(exitcode)


class ConfigManager:
    __CONFIG_FILENAME = "config.json"
    __DEFAULT_CONFIG = {
        # Monitoring interval in second.
        "monitor_interval": 1,

        # Check whether the lua engine is still alive or not by utilizing the helper mod (mod_id=44AE3979).
        "check_lua_engine_status": True,

        # Lua engine unresponsive tolerance (in seconds).
        # PS: DO NOT set it too low, because the helper mod will stop update the record during the initializing &  
        #     map changing. If the threshold is too low, the server would get interrupted when performing those action.
        "lua_engine_no_response_threshold": 60,

        # Date format of the helper mod's output.
        "format_helper_mod_record": "%m/%d/%y %H:%M:%S",

        # Dump the memory before restart the unresponsive server.
        "create_mem_dump": False,

        # Whether restart the server every day.
        "daily_restart": True,<commit_msg>attempt to solve the logger's charset related crash<commit_after>#  -*- encoding:UTF-8 -*-
#
# NS2 Server Watch dog -- a Natural Selection 2 server monitoring script
#
# Wrote by John <<EMAIL>>
# MIT License.

import datetime
import json
import os
import platform
import shlex
import signal
import sys
import time
from subprocess import Popen

import psutil

from Utils.TextfileWriter import TextfileWriter

if cmp(platform.system(), 'Windows') is 0:
    from Utils.WindowsConsoleWriter import WindowsConsoleWriter as PlatformConsoleWriter
    from subprocess import CREATE_NEW_PROCESS_GROUP
else:
    from Utils.UnixConsoleWriter import UnixConsoleWriter as PlatformConsoleWriter

VERBOSE_LEVEL = 0
ExitFlag = False


class Logger:
    __TIME_LABEL_PATTERN = '%m/%d/%y-%H:%M:%S'
    __console_writer = PlatformConsoleWriter()
    __file_logger = TextfileWriter()
    __LINE_PATTERN = u"[%s] <%s>: %s\n"

    @staticmethod
    def init_logger():
        global VERBOSE_LEVEL
        VERBOSE_LEVEL = ConfigManager.get_config("verbose_level")

    def __init__(self):
        raise NotImplementedError("This class should never be instantiated.")

    @staticmethod
    def __gen_log_line(str_level, text):
        log_line = Logger.__LINE_PATTERN % (time.strftime(Logger.__TIME_LABEL_PATTERN, time.localtime(time.time())),
                                            str_level, unicode(text))
        log_line = unicode(log_line).encode("utf-8")
        return log_line

    @staticmethod
    def debug(text):
        if VERBOSE_LEVEL >= 2:
            log_line = Logger.__gen_log_line("DEBUG", text)
            Logger.__console_writer.debug(log_line)
            Logger.__file_logger.log(log_line)

    @staticmethod
    def verbose(text):
        if VERBOSE_LEVEL >= 1:
            log_line = Logger.__gen_log_line("VERBOSE", text)
            Logger.__console_writer.verbose(log_line)
            Logger.__file_logger.log(log_line)

    @staticmethod
    def info(text):
        log_line = Logger.__gen_log_line("INFO", text)
        Logger.__console_writer.normal(log_line)
        Logger.__file_logger.log(log_line)

    @staticmethod
    def warn(text):
        log_line = Logger.__gen_log_line("WARN", text)
        Logger.__console_writer.warn(log_line)
        Logger.__file_logger.log(log_line)

    @staticmethod
    def fatal(text, exitcode=-1):
        log_line = Logger.__gen_log_line("FATAL", text)
        Logger.__console_writer.error(log_line)
        Logger.__file_logger.log(log_line)

        log_line = Logger.__gen_log_line("FATAL", ("Program terminated, exit code: %d." % exitcode))
        Logger.__console_writer.error(log_line)
        Logger.__file_logger.log(log_line)

        sys.exit(exitcode)


class ConfigManager:
    __CONFIG_FILENAME = "config.json"
    __DEFAULT_CONFIG = {
        # Monitoring interval in second.
        "monitor_interval": 1,

        # Check whether the lua engine is still alive or not by utilizing the helper mod (mod_id=44AE3979).
        "check_lua_engine_status": True,

        # Lua engine unresponsive tolerance (in seconds).
        # PS: DO NOT set it too low, because the helper mod will stop update the record during the initializing &  
        #     map changing. If the threshold is too low, the server would get interrupted when performing those action.
        "lua_engine_no_response_threshold": 60,

        # Date format of the helper mod's output.
        "format_helper_mod_record": "%m/%d/%y %H:%M:%S",

        # Dump the memory before restart the unresponsive server.
        "create_mem_dump": False,

        # Whether restart the server every day.
        "daily_restart": True,
{'fraction_non_alphanumeric': 0.09083860963948831, 'fraction_numerical': 0.004522548132833699, 'mean_word_length': 2.812807881773399, 'pattern_counts': {'":': 12, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '759544', 'n_tokens_mistral': 2294, 'n_tokens_neox': 2187, 'n_words': 554}