예제 #1
0
 def wrapper(*args, **kwargs):
     state = cur_ctx().find_object(self.LogState)
     if state.profile_mem:
         max_mem, returnobj = memory_usage(
             (user_func, args, kwargs),
             retval=True,
             include_children=True,
             max_usage=True,
             multiprocess=True,
             max_iterations=1,
         )
         state.max_mem = int(max_mem)
         logger.log(
             level.upper(),
             f"Peak total memory use = {state.max_mem} MB.",
         )
     else:
         returnobj = user_func(*args, **kwargs)
     return returnobj
예제 #2
0
 def get_user_global_options(self):
     """Return dict of global user options."""
     return cur_ctx().find_object(self.LogState).user_options
예제 #3
0
 def get_global_options(self):
     """Return dictionary of global options."""
     return cur_ctx().find_object(self.LogState)
예제 #4
0
 def wrapper(*args, **kwargs):
     state = cur_ctx().find_object(self.LogState)
     state.subcommand = cur_ctx().invoked_subcommand
     return user_func(*args, **kwargs)
예제 #5
0
 def wrapper(*args, **kwargs):
     state = cur_ctx().find_object(self.LogState)
     # get the verbose/quiet levels from context
     if state.verbose:
         log_level = "DEBUG"
     elif state.quiet:
         log_level = "ERROR"
     else:
         log_level = self._stderr_log_level
     logger.remove()  # remove existing default logger
     logger.add(sys.stderr,
                level=log_level,
                format=self.stderr_format_func)
     if logfile and state.logfile:  # start a log file
         # If a subcommand was used, log to a file in the
         # logs/ subdirectory with the subcommand in the file name.
         if log_dir_parent is not None:
             self._log_dir_parent = log_dir_parent
         if self._log_dir_parent is None:
             log_dir_path = Path(".") / "logs"
         else:
             log_dir_path = Path(self._log_dir_parent)
         subcommand = cur_ctx().invoked_subcommand
         if subcommand is None:
             subcommand = state.subcommand
         if subcommand is not None:
             logfile_prefix = f"{self._name}-{subcommand}"
         else:
             logfile_prefix = f"{self._name}"
         if log_dir_path.exists():
             log_numbers = [
                 f.name[len(logfile_prefix) + 1:-4]
                 for f in log_dir_path.glob(logfile_prefix +
                                            "_*.log")
             ]
             log_number_ints = sorted(
                 [int(n) for n in log_numbers if n.isnumeric()])
             if len(log_number_ints) > 0:
                 log_number = log_number_ints[-1] + 1
                 if (self._retention is not None and
                         len(log_number_ints) > self._retention):
                     for remove in log_number_ints[:len(
                             log_number_ints) - self._retention]:
                         (log_dir_path /
                          f"{logfile_prefix}_{remove}.log"
                          ).unlink()
             else:
                 log_number = 0
         else:
             log_number = 0
         if self._retention == 0:
             state.logfile_path = (log_dir_path /
                                   f"{logfile_prefix}.log")
         else:
             state.logfile_path = (
                 log_dir_path /
                 f"{logfile_prefix}_{log_number}.log")
         state.logfile_handler_id = logger.add(
             str(state.logfile_path), level=self._file_log_level)
     logger.debug(f'Command line: "{" ".join(sys.argv)}"')
     logger.debug(f"{self._name} version {self._version}")
     logger.debug(
         f"Run started at {str(self.start_times['Total']['wall'])[:SKIP_FIELDS]}"
     )
     return user_func(*args, **kwargs)