Skip to content

Commit

Permalink
fix io stream
Browse files Browse the repository at this point in the history
  • Loading branch information
wayyoungboy committed Jul 31, 2024
1 parent 90697d9 commit e2c2250
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
3 changes: 3 additions & 0 deletions conf/inner_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
'file_number_limit': 20,
'file_size_limit': '2G',
'dis_rsa_algorithms': 0,
'print_type': 0,
},
'logger': {
'log_dir': '~/.obdiag/log',
Expand All @@ -74,6 +73,7 @@
'log_level': 'INFO',
'mode': 'obdiag',
'stdout_handler_log_level': 'INFO',
'error_stream': 'sys.stdout',
},
'ssh_client': {
'remote_client_sudo': False,
Expand Down
3 changes: 1 addition & 2 deletions core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion diag_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')


Expand Down
39 changes: 28 additions & 11 deletions stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from __future__ import absolute_import, division, print_function

import json
import os
import signal
import sys
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit e2c2250

Please sign in to comment.