from __future__ import unicode_literals

import os
import re

import ttfw_idf


@ttfw_idf.idf_example_test(env_tag='test_jtag_arm')
def test_examples_app_trace_to_host(env, extra_data):
    rel_project_path = os.path.join('examples', 'system', 'app_trace_to_host')
    dut = env.get_dut('app_trace_to_host', rel_project_path)
    idf_path = dut.app.get_sdk_path()
    proj_path = os.path.join(idf_path, rel_project_path)

    with ttfw_idf.OCDBackend(os.path.join(proj_path, 'openocd.log'), dut.app.target) as ocd:
        dut.start_app()
        dut.expect_all('example: Enabling ADC1 on channel 6 / GPIO34.',
                       'example: Enabling CW generator on DAC channel 1',
                       'example: Sampling ADC and sending data to the host...',
                       re.compile(r'example: Collected \d+ samples in 20 ms.'),
                       'example: Sampling ADC and sending data to the UART...',
                       re.compile(r'example: Sample:\d, Value:\d+'),
                       re.compile(r'example: Collected \d+ samples in 20 ms.'),
                       timeout=20)

        ocd.apptrace_start('file://adc.log 0 9000 5 0 0')
        ocd.apptrace_wait_stop(tmo=30)

    with ttfw_idf.CustomProcess(' '.join([os.path.join(idf_path, 'tools/esp_app_trace/logtrace_proc.py'),
                                          'adc.log',
                                          os.path.join(dut.app.binary_path, 'app_trace_to_host.elf')]),
                                logfile='logtrace_proc.log') as logtrace:
        logtrace.pexpect_proc.expect_exact('Parse trace file')
        logtrace.pexpect_proc.expect_exact('Parsing completed.')
        logtrace.pexpect_proc.expect_exact('====================================================================')
        logtrace.pexpect_proc.expect(re.compile(r'example: Sample:\d+, Value:\d+'))
        logtrace.pexpect_proc.expect_exact('====================================================================')
        logtrace.pexpect_proc.expect(re.compile(r'Log records count: \d+'))


if __name__ == '__main__':
    test_examples_app_trace_to_host()

@@ -418,38 +418,28 @@
 th)%0A
-%0A
     
-with ttfw_idf.OCDBackend(
+oocd_log_path = 
 os.p
@@ -472,16 +472,60 @@
 cd.log')
+%0A%0A    with ttfw_idf.OCDBackend(oocd_log_path
 , dut.ap
@@ -1231,24 +1231,445 @@
 op(tmo=30)%0A%0A
+    with open(oocd_log_path) as oocd_log:%0A        cores = 1 if dut.app.get_sdkconfig().get('CONFIG_FREERTOS_UNICORE', '').replace('%22','') == 'y' else 2%0A        params_str = 'App trace params: from %7B%7D cores'.format(cores)%0A        for line in oocd_log:%0A            if params_str in line:%0A                break%0A        else:%0A            raise RuntimeError('%22%7B%7D%22 could not be found in %7B%7D'.format(params_str, oocd_log_path))%0A%0A
     with ttf

Check the number of detected cores of apptrace