def _test_indent_print(): # Indent test code doesnt work in doctest blocks. import utool as ut flag = ut.ensure_logging() print('Checking indent. Should have none') with ut.Indenter('[INDENT] '): print('Checking indent. Should be indented') print('Should no longer be indented') text = ut.get_current_log_text() # The last line might sometimes be empty or not. # Not sure. # New hack: had to put in stride. Seems like logs get written # with two line breaks now last_lines = text.split('\n')[-8::2] if last_lines[-1] != '': assert False, 'DEV ERROR. REMOVE FIRST LINE INSTEAD OF LAST' last_lines = last_lines[:-1] #print('last_lines = %r' % (ut.repr3(last_lines))) try: assert last_lines[0].find('[INDENT] ') == -1, last_lines[0] assert last_lines[1].find('[INDENT] ') >= 0, 'did not indent %r' % ( last_lines[1], ) assert last_lines[2].find('[INDENT] ') == -1, last_lines[2] except AssertionError: print('Error. Last 3 lines') print(ut.repr3(last_lines)) raise if not flag: ut.stop_logging()
def set_logdir(log_dir): from os.path import realpath, expanduser log_dir = realpath(expanduser(log_dir)) ut.ensuredir(log_dir, verbose=True) ut.stop_logging() _ibeis_cache_write(LOGDIR_CACHEID, log_dir) ut.start_logging(appname=__APPNAME__)
def test(): print('enter test') log_fpath1 = utool.get_app_resource_dir('utool', 'test_logfile1.txt') log_fpath2 = utool.get_app_resource_dir('utool', 'test_logfile2.txt') utool.start_logging(log_fpath1, 'w') func1() func2() utool.stop_logging() print('\n\n') print('This line is NOT logged') print('\n\n') utool.start_logging(log_fpath2, 'w') print('This line is logged') utool.stop_logging() log1 = utool.read_from(log_fpath1, verbose=False) log2 = utool.read_from(log_fpath2, verbose=False) target1 = utool.unindent(''' <__LOG_START__> logging to log_fpath=%r [test][func1]enter func1 [test][func1]exit func1 [test][func2]enter func2 [test][func2][func1]enter func1 [test][func2][func1]exit func1 [test][func2]exit func2 <__LOG_STOP__>''' % log_fpath1).strip() target2 = utool.unindent(''' <__LOG_START__> logging to log_fpath=%r [test]This line is logged <__LOG_STOP__>''' % log_fpath2).strip() output1 = remove_timestamp(log1).strip() output2 = remove_timestamp(log2).strip() try: assert target1 == output1, 'target1 failed' assert target2 == output2, 'target2 failed' builtins.print('TEST PASSED') except AssertionError: builtins.print('\n<!!! TEST FAILED !!!>') builtins.print('\ntarget1:') builtins.print(target1) builtins.print('\noutput1:') builtins.print(output1) builtins.print('\ntarget2:') builtins.print(target2) builtins.print('\noutput2:') builtins.print(output2) builtins.print('</!!! TEST FAILED !!!>\n') raise
def _test_indent_print(): # Indent test code doesnt work in doctest blocks. import utool as ut flag = ut.ensure_logging() print('Checking indent. Should have none') with ut.Indenter('[INDENT] '): print('Checking indent. Should be indented') print('Should no longer be indented') text = ut.get_current_log_text() # The last line might sometimes be empty or not. # Not sure. # New hack: had to put in stride. Seems like logs get written # with two line breaks now last_lines = text.split('\n')[-8::2] if last_lines[-1] != '': assert False, 'DEV ERROR. REMOVE FIRST LINE INSTEAD OF LAST' last_lines = last_lines[:-1] #print('last_lines = %r' % (ut.repr3(last_lines))) try: assert last_lines[0].find('[INDENT] ') == -1, last_lines[0] assert last_lines[1].find('[INDENT] ') >= 0, 'did not indent %r' % (last_lines[1],) assert last_lines[2].find('[INDENT] ') == -1, last_lines[2] except AssertionError: print('Error. Last 3 lines') print(ut.repr3(last_lines)) raise if not flag: ut.stop_logging()
def test_body2(count, logmode, backspace): ut.colorprint('\n---- count = %r -----' % (count,), 'yellow') ut.colorprint('backspace = %r' % (backspace,), 'yellow') ut.colorprint('logmode = %r' % (logmode,), 'yellow') if logmode: ut.delete('test.log') ut.start_logging('test.log') print('Start main loop') import time for count in ut.ProgressIter(range(2), freq=1, backspace=backspace): for count in ut.ProgressIter(range(50), freq=1, backspace=backspace): time.sleep(.01) print('Done with main loop work') print('Exiting main body') if logmode: ut.stop_logging()
def test_body2(count, logmode, backspace): ut.colorprint('\n---- count = %r -----' % (count, ), 'yellow') ut.colorprint('backspace = %r' % (backspace, ), 'yellow') ut.colorprint('logmode = %r' % (logmode, ), 'yellow') if logmode: ut.delete('test.log') ut.start_logging('test.log') print('Start main loop') import time for count in ut.ProgressIter(range(2), freq=1, backspace=backspace): for count in ut.ProgressIter(range(50), freq=1, backspace=backspace): time.sleep(.01) print('Done with main loop work') print('Exiting main body') if logmode: ut.stop_logging()
def test_body(count, logmode, backspace): ut.colorprint('\n---- count = %r -----' % (count, ), 'yellow') ut.colorprint('backspace = %r' % (backspace, ), 'yellow') ut.colorprint('logmode = %r' % (logmode, ), 'yellow') if logmode: ut.delete('test.log') ut.start_logging('test.log') print('Start main loop') import time for count in ut.ProgressIter(range(20), freq=3, backspace=backspace): time.sleep(.01) print('Done with main loop work') print('Exiting main body') if logmode: ut.stop_logging() #print('-----DONE LOGGING----') testlog_text = ut.readfrom('test.log') print(ut.indent(testlog_text.replace('\r', '\n'), ' '))
def test_body(count, logmode, backspace): ut.colorprint('\n---- count = %r -----' % (count,), 'yellow') ut.colorprint('backspace = %r' % (backspace,), 'yellow') ut.colorprint('logmode = %r' % (logmode,), 'yellow') if logmode: ut.delete('test.log') ut.start_logging('test.log') print('Start main loop') import time for count in ut.ProgressIter(range(20), freq=3, backspace=backspace): time.sleep(.01) print('Done with main loop work') print('Exiting main body') if logmode: ut.stop_logging() #print('-----DONE LOGGING----') testlog_text = ut.readfrom('test.log') print(ut.indent(testlog_text.replace('\r', '\n'), ' '))
def set_logdir(log_dir): utool.ensuredir(log_dir) utool.stop_logging() _ibeis_cache_write(LOGDIR_CACHEID, log_dir) utool.start_logging(appname=__APPNAME__)