diff --git a/conf/inner_config.yml b/conf/inner_config.yml index c0480eda..db4aa329 100644 --- a/conf/inner_config.yml +++ b/conf/inner_config.yml @@ -12,6 +12,9 @@ obdiag: log_level: INFO mode: obdiag stdout_handler_log_level: INFO + error_stream: sys.stdout + ssh_client: + remote_client_sudo: 0 check: ignore_version: false work_path: "~/.obdiag/check" diff --git a/config.py b/config.py index 0ff7d58e..cc2fc19f 100644 --- a/config.py +++ b/config.py @@ -65,7 +65,6 @@ 'file_number_limit': 20, 'file_size_limit': '2G', 'dis_rsa_algorithms': 0, - 'print_type': 0, }, 'logger': { 'log_dir': '~/.obdiag/log', @@ -74,6 +73,7 @@ 'log_level': 'INFO', 'mode': 'obdiag', 'stdout_handler_log_level': 'INFO', + 'error_stream': 'sys.stdout', }, 'ssh_client': { 'remote_client_sudo': False, diff --git a/core.py b/core.py index c19c1d39..3a7d996c 100644 --- a/core.py +++ b/core.py @@ -73,8 +73,7 @@ def __init__(self, stdio=None, config_path=os.path.expanduser('~/.obdiag/config. self.context = None self.inner_config_manager = InnerConfigManager(stdio=stdio, inner_config_change_map=inner_config_change_map) if self.inner_config_manager.config.get("obdiag") is not None and self.inner_config_manager.config.get("obdiag").get("basic") is not None and self.inner_config_manager.config.get("obdiag").get("basic").get("print_type") is not None: - stdio.set_print_type(self.inner_config_manager.config.get("obdiag").get("basic").get("print_type")) - pass + stdio.set_err_stream(self.inner_config_manager.config.get("obdiag").get("logger").get("error_stream")) self.set_stdio(stdio) self.config_manager = ConfigManager(config_path, stdio) diff --git a/diag_cmd.py b/diag_cmd.py index c54012dc..ccca161d 100644 --- a/diag_cmd.py +++ b/diag_cmd.py @@ -29,7 +29,8 @@ from common.version import get_obdiag_version from telemetry.telemetry import telemetry -ROOT_IO = IO(1) +# TODO when obdiag_version ≥ 3.0, the default value of err_stream will be changed to sys.stderr +ROOT_IO = IO(1, error_stream=sys.stdout) OBDIAG_HOME_PATH = os.path.join(os.getenv('HOME'), 'oceanbase-diagnostic-tool') diff --git a/stdio.py b/stdio.py index 96b70795..bdb10105 100644 --- a/stdio.py +++ b/stdio.py @@ -13,6 +13,7 @@ from __future__ import absolute_import, division, print_function +import json import os import signal import sys @@ -358,7 +359,7 @@ class IO(object): WARNING_PREV = FormtatText.warning('[WARN]') ERROR_PREV = FormtatText.error('[ERROR]') - def __init__(self, level, msg_lv=MsgLevel.DEBUG, use_cache=False, track_limit=0, root_io=None, input_stream=SysStdin, output_stream=sys.stdout, error_stream=sys.stderr): + def __init__(self, level, msg_lv=MsgLevel.DEBUG, use_cache=False, track_limit=0, root_io=None, input_stream=SysStdin, output_stream=sys.stdout, error_stream=sys.stdout): self.level = level self.msg_lv = msg_lv self.default_confirm = False @@ -382,14 +383,6 @@ def __init__(self, level, msg_lv=MsgLevel.DEBUG, use_cache=False, track_limit=0, self.set_input_stream(input_stream) self.set_output_stream(output_stream) self.set_err_stream(error_stream) - # TODO print on doc - self.print_type = 0 # 0: use stdout, 1: use stdout and stderr, 2: just print result (json). when obdiag_version ≥ 3.0, print_type default 1 - - def set_print_type(self, print_type=0): - try: - self.print_type = int(print_type) - except Exception as e: - pass def isatty(self): if self._root_io: @@ -412,6 +405,15 @@ def set_output_stream(self, output_stream): return True def set_err_stream(self, error_stream): + if isinstance(error_stream, str): + error_stream = error_stream.strip().lower() + if error_stream == "sys.stderr": + error_stream = sys.stderr + elif error_stream == "sys.stdout": + error_stream = sys.stdout + else: + # TODO 3.X NEED CHANGE TO sys.stderr + error_stream = sys.stdout if self._root_io: return False if self._cur_err_obj == self._err_obj: @@ -437,7 +439,7 @@ def __getstate__(self): state = {} for key in self.__dict__: state[key] = self.__dict__[key] - for key in ['_trace_logger', 'input_stream', 'sync_obj', '_out_obj', '_cur_out_obj', '_before_critical', '_cur_err_obj']: + for key in ['_trace_logger', 'input_stream', 'sync_obj', '_out_obj', '_err_obj', '_cur_out_obj', '_cur_err_obj', '_before_critical']: state[key] = None return state @@ -545,11 +547,16 @@ def _stop_buffer_io(self): return False if self._cur_out_obj == self._out_obj: return False + if self._cur_err_obj == self._err_obj: + return False text = self._cur_out_obj.read() + text_err = self._cur_err_obj.read() self._cur_out_obj = self._out_obj self._cur_err_obj = self._err_obj if text: self.print(text) + if text_err: + self.error(text_err) return True @staticmethod @@ -707,7 +714,7 @@ def _print(self, msg_lv, msg, *args, **kwargs): del kwargs['prev_msg'] else: print_msg = msg - if msg_lv == MsgLevel.ERROR and self.print_type == 1: + if msg_lv == MsgLevel.ERROR: kwargs['file'] = self.get_cur_err_obj() else: kwargs['file'] = self.get_cur_out_obj() @@ -763,6 +770,16 @@ def verbose(self, msg, *args, **kwargs): return self._print(MsgLevel.VERBOSE, '%s %s' % (self._verbose_prefix, msg), *args, **kwargs) + def print_result_json(self, result): + + if not result: + return + if isinstance(result, dict): + result = json.dumps(result, indent=4) + self.print(result) + + pass + if sys.version_info.major == 2: def exception(self, msg='', *args, **kwargs):