1# Copyright 2017 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import glob
6import os
7import logging
8import subprocess
9
10def chrome_vmodule_flag():
11    """Return vmodule flag for chrome to enable VDAs/VEAs/JDAs/V4L2IP logs"""
12    logging_patterns = ['*/media/gpu/*video_decode_accelerator.cc=2',
13                        '*/media/gpu/*video_encode_accelerator.cc=2',
14                        '*/media/gpu/*jpeg_decode_accelerator.cc=2',
15                        '*/media/gpu/v4l2_image_processor.cc=2']
16    chrome_video_vmodule_flag = '--vmodule=' + ','.join(logging_patterns)
17    logging.info('chrome video vmodule flag: %s', chrome_video_vmodule_flag)
18    return chrome_video_vmodule_flag
19
20
21def video_log_wrapper(func):
22    """
23    Return decorator that make verbose video logs enable
24    before test and make them disable after completing test.
25
26    @param func: function, the test function, e.g., run_once
27    @returns decorator function
28    """
29    vlog = VideoLog()
30
31    #videobuf2 log
32    files = glob.glob('/sys/module/videobuf2_*/parameters/debug')
33    vlog.add_log(files,
34                 ['1'] * len(files),
35                 ['0'] * len(files),
36                 'videobuf2 log')
37
38    #s5p_mfc log
39    fpath = '/sys/module/s5p_mfc/parameters/debug'
40    if os.path.exists(fpath):
41        vlog.add_log([fpath],
42                     ['1'],
43                     ['0'],
44                     's5p_mfc log')
45
46    #rk3399 log
47    #rk3399 debug level is controlled by bits.
48    #Here, 3 means to enable log level 0 and 1.
49    fpath = '/sys/module/rockchip_vpu/parameters/debug'
50    if os.path.exists(fpath):
51        vlog.add_log([fpath],
52                     ['3'],
53                     ['0'],
54                     'rk3399 log')
55
56    #rk3288 log
57    #rk3288 debug level is controlled by bits.
58    #Here, 3 means to enable log level 0 and 1.
59    fpath = '/sys/module/rk3288_vpu/parameters/debug'
60    if os.path.exists(fpath):
61        vlog.add_log([fpath],
62                     ['3'],
63                     ['0'],
64                     'rk3288 log')
65
66    #go2001 log
67    fpath = '/sys/module/go2001/parameters/go2001_debug_level'
68    if os.path.exists(fpath):
69        vlog.add_log([fpath],
70                     ['1'],
71                     ['0'],
72                     'go2001 log')
73
74    def call(*args, **kwargs):
75        """
76        Enable logs before the test, execute the test and disable logs
77        after the test.
78
79        In any case, it is guranteed to disable logs.
80        """
81        with vlog:
82            return func(*args, **kwargs)
83
84    return call
85
86
87def cmdexec_with_log(cmd, info_message):
88    """
89    Execute command, logging infomation message.
90
91    @param cmd: string, command to be executed
92    @param info_message: string, the messages to be shown in log message
93    """
94    try:
95        logging.info('%s : %s', info_message, cmd)
96        subprocess.check_call(cmd, shell=True)
97    except subprocess.CalledProcessError:
98        logging.warning('Fail to execute command [%s] : %s',
99                        info_message, cmd)
100
101
102class VideoLog:
103    """
104    Enable/Disable video logs.
105    """
106    def __init__(self):
107        self.logs = []
108
109    def add_log(self, files, enable_values, disable_values, log_name):
110        """
111        Add new log
112
113        @param files: list of string, file paths
114        @param enable_values: list of string, the list of value
115                              to write to each file path for enabling
116        @param disable_values: list of string, the list of value
117                               to write to each file path for disabling
118        @param log_name: string, name to be shown in log message
119        """
120        self.logs.append({'files': files,
121                          'enable values': enable_values,
122                          'disable values': disable_values,
123                          'log name': log_name})
124
125    def __enter__(self):
126        """Enable logs"""
127        for log in self.logs:
128            log_name = log['log name']
129            for f, ev in zip(log['files'], log['enable values']):
130                cmdexec_with_log('echo %s > %s' % (ev, f),
131                                 '%s enable' % log_name)
132
133    def __exit__(self, exception_type, exception_value, traceback):
134        """Disable logs"""
135        for log in self.logs:
136            log_name = log['log name']
137            for f, dv in zip(log['files'], log['disable values']):
138                cmdexec_with_log('echo %s > %s' % (dv, f),
139                                 '%s disable' % log_name)
140