def _log_message(self, message_type: str, message: str):
     from sims4communitylib.utils.common_date_utils import CommonRealDateUtils
     from pprint import pformat
     from sims4communitylib.exceptions.common_exceptions_handler import CommonExceptionHandler
     current_date_time = CommonRealDateUtils.get_current_date_string()
     new_message = '{} [{}] {}: [{}]: {}\n'.format(current_date_time, self._mod_name, str(message_type), self.name, message)
     try:
         from sims4communitylib.utils.common_io_utils import CommonIOUtils
         file_path = CommonLogUtils.get_message_file_path(self._mod_name)
         CommonIOUtils.write_to_file(file_path, new_message, ignore_errors=True)
     except Exception as ex:
         CommonExceptionHandler.log_exception(self._mod_name, 'Error occurred while attempting to log message: {}'.format(pformat(message)), exception=ex)
Beispiel #2
0
 def _delete_old_log_files(self, mod_identifier: Union[str, CommonModIdentity]):
     from sims4communitylib.utils.common_io_utils import CommonIOUtils
     mod_name = CommonModIdentity._get_mod_name(mod_identifier)
     files_to_delete = (
         CommonLogUtils.get_message_file_path(mod_name),
         CommonLogUtils.get_exceptions_file_path(mod_name),
         CommonLogUtils.get_old_message_file_path(mod_name),
         CommonLogUtils.get_old_exceptions_file_path(mod_name)
     )
     for file_to_delete in files_to_delete:
         # noinspection PyBroadException
         try:
             CommonIOUtils.delete_file(file_to_delete, ignore_errors=True)
         except:
             continue
    def load_from_file(file_path: str,
                       buffering: int = 1,
                       encoding: str = 'utf-8') -> Union[Any, None]:
        """load_from_file(file_path, buffering=1, encoding='utf-8')

        Deserialize an object from a JSON file.

        :param file_path: The file to read from.
        :type: file_path: str
        :param buffering: See the built-in python :func:`~open` function documentation for more details.
        :type buffering: int, optional
        :param encoding: See the built-in python :func:`~open` function documentation for more details.
        :type encoding: str, optional
        :return: The contents of the file as an object or None if an error occurred.
        :rtype: Union[Any, None]
        """
        file_contents: str = CommonIOUtils.load_from_file(file_path,
                                                          buffering=buffering,
                                                          encoding=encoding)
        if file_contents is None:
            return None
        try:
            return json.loads(file_contents)
        except Exception as ex:
            CommonExceptionHandler.log_exception(
                ModInfo.get_identity(),
                'Error occurred while reading JSON from file \'{}\''.format(
                    file_path),
                exception=ex)
            return None
 def _log_error(self, message: str, exception: Exception = None):
     from sims4communitylib.utils.common_date_utils import CommonRealDateUtils
     from sims4communitylib.exceptions.common_exceptions_handler import CommonExceptionHandler
     try:
         exceptions = CommonStacktraceUtil.get_full_stack_trace()
         if exception is not None:
             stack_trace = '{}{} -> {}: {}\n'.format(
                 ''.join(exceptions), message,
                 type(exception).__name__, exception)
         else:
             stack_trace = 'No stack trace provided.'
         file_path = self.exceptions_file_path
         os.makedirs(os.path.dirname(file_path), exist_ok=True)
         exception_traceback_text = '[{}] {} {}\n'.format(
             self.mod_name, CommonRealDateUtils.get_current_date_string(),
             stack_trace)
         result = CommonIOUtils.write_to_file(file_path,
                                              exception_traceback_text,
                                              ignore_errors=True)
         if result:
             CommonExceptionHandler._notify_exception_occurred(
                 file_path, mod_identifier=self.mod_name)
     except Exception as ex:
         CommonExceptionHandler.log_exception(
             self.mod_name,
             'Error occurred while attempting to log message: {}'.format(
                 pformat(message)),
             exception=ex,
             custom_file_path=self._custom_file_path)
 def _log_stacktrace(mod_name: str, _traceback, file_path: str) -> bool:
     exception_traceback_text = '[{}] {} {}\n'.format(
         mod_name, CommonRealDateUtils.get_current_date_string(),
         _traceback)
     return CommonIOUtils.write_to_file(file_path,
                                        exception_traceback_text,
                                        ignore_errors=True)
Beispiel #6
0
 def _log_stacktrace(mod_identifier: Union[str, CommonModIdentity],
                     _traceback: str, file_path: str) -> bool:
     mod_identifier = CommonModIdentity._get_mod_name(mod_identifier)
     exception_traceback_text = '[{}] {} {}\n'.format(
         mod_identifier, CommonRealDateUtils.get_current_date_string(),
         _traceback)
     return CommonIOUtils.write_to_file(file_path,
                                        exception_traceback_text,
                                        ignore_errors=True)
 def _log_message(self, message_type: CommonMessageType, message: str):
     from sims4communitylib.utils.common_date_utils import CommonRealDateUtils
     from sims4communitylib.exceptions.common_exceptions_handler import CommonExceptionHandler
     current_date_time = CommonRealDateUtils.get_current_date_string()
     new_message = '{} {}: [{}]: {}\n'.format(current_date_time,
                                              message_type.name, self.name,
                                              message)
     try:
         from sims4communitylib.utils.common_io_utils import CommonIOUtils
         file_path = self.messages_file_path
         os.makedirs(os.path.dirname(file_path), exist_ok=True)
         CommonIOUtils.write_to_file(file_path,
                                     new_message,
                                     ignore_errors=True)
     except Exception as ex:
         CommonExceptionHandler.log_exception(
             self.mod_name,
             'Error occurred while attempting to log message: {}'.format(
                 pformat(message)),
             exception=ex,
             custom_file_path=self._custom_file_path)
    def load_from_file(file_path: str,
                       buffering: int = 1,
                       encoding: str = 'utf-8') -> Union[Any, None]:
        """load_from_file(file_path, buffering=1, encoding='utf-8')

        Deserialize an object from a JSON file.

        :param file_path: The file to read from.
        :type: file_path: str
        :param buffering: See the built-in python :func:`~open` function documentation for more details.
        :type buffering: int, optional
        :param encoding: See the built-in python :func:`~open` function documentation for more details.
        :type encoding: str, optional
        :return: The contents of the file as an object or None if an error occurred.
        :rtype: Union[Any, None]
        """
        file_contents: str = CommonIOUtils.load_from_file(file_path,
                                                          buffering=buffering,
                                                          encoding=encoding)
        if file_contents is None:
            return None
        return json.loads(file_contents)