test_logging_utils.py 5.1 KB
Newer Older
1
#   Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import io
import logging
import os
import sys
import unittest
20
from unittest import mock
21 22

import paddle
23
from paddle.jit.dy2static import logging_utils
24
from paddle.utils import gast
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47


class TestLoggingUtils(unittest.TestCase):
    def setUp(self):
        self.verbosity_level = 1
        self.code_level = 3
        self.translator_logger = logging_utils._TRANSLATOR_LOGGER

    def test_verbosity(self):
        paddle.jit.set_verbosity(None)
        os.environ[logging_utils.VERBOSITY_ENV_NAME] = '3'
        self.assertEqual(logging_utils.get_verbosity(), 3)

        paddle.jit.set_verbosity(self.verbosity_level)
        self.assertEqual(self.verbosity_level, logging_utils.get_verbosity())

        # String is not supported
        with self.assertRaises(TypeError):
            paddle.jit.set_verbosity("3")

        with self.assertRaises(TypeError):
            paddle.jit.set_verbosity(3.3)

48 49 50
    def test_also_to_stdout(self):
        logging_utils._TRANSLATOR_LOGGER.need_to_echo_log_to_stdout = None
        self.assertEqual(
51 52
            logging_utils._TRANSLATOR_LOGGER.need_to_echo_log_to_stdout, False
        )
53

54 55
        paddle.jit.set_verbosity(also_to_stdout=False)
        self.assertEqual(
56 57
            logging_utils._TRANSLATOR_LOGGER.need_to_echo_log_to_stdout, False
        )
58 59 60

        logging_utils._TRANSLATOR_LOGGER.need_to_echo_node_to_stdout = None
        self.assertEqual(
61 62
            logging_utils._TRANSLATOR_LOGGER.need_to_echo_code_to_stdout, False
        )
63 64 65

        paddle.jit.set_code_level(also_to_stdout=True)
        self.assertEqual(
66 67
            logging_utils._TRANSLATOR_LOGGER.need_to_echo_code_to_stdout, True
        )
68 69 70 71 72 73 74 75

        with self.assertRaises(AssertionError):
            paddle.jit.set_verbosity(also_to_stdout=1)

        with self.assertRaises(AssertionError):
            paddle.jit.set_code_level(also_to_stdout=1)

    def test_set_code_level(self):
76 77 78 79 80 81 82 83 84 85 86 87 88
        paddle.jit.set_code_level(None)
        os.environ[logging_utils.CODE_LEVEL_ENV_NAME] = '2'
        self.assertEqual(logging_utils.get_code_level(), 2)

        paddle.jit.set_code_level(self.code_level)
        self.assertEqual(logging_utils.get_code_level(), self.code_level)

        paddle.jit.set_code_level(9)
        self.assertEqual(logging_utils.get_code_level(), 9)

        with self.assertRaises(TypeError):
            paddle.jit.set_code_level(3.3)

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    def test_log_api(self):
        # test api for CI Converage
        logging_utils.set_verbosity(1, True)

        logging_utils.warn("warn")
        logging_utils.error("error")

        logging_utils.log(1, "log level 1")
        logging_utils.log(2, "log level 2")

        source_code = "x = 3"
        ast_code = gast.parse(source_code)
        logging_utils.set_code_level(1, True)
        logging_utils.log_transformed_code(1, ast_code, "TestTransformer")
        logging_utils.set_code_level(logging_utils.LOG_AllTransformer, True)
104 105 106
        logging_utils.log_transformed_code(
            logging_utils.LOG_AllTransformer, ast_code, "TestTransformer"
        )
107 108

    def test_log_message(self):
T
tianshuo78520a 已提交
109
        stream = io.StringIO()
110 111 112 113 114 115 116 117 118
        log = self.translator_logger.logger
        stdout_handler = logging.StreamHandler(stream)
        log.addHandler(stdout_handler)

        warn_msg = "test_warn"
        error_msg = "test_error"
        log_msg_1 = "test_log_1"
        log_msg_2 = "test_log_2"

T
tianshuo78520a 已提交
119 120 121 122 123 124
        with mock.patch.object(sys, 'stdout', stream):
            logging_utils.set_verbosity(1, False)
            logging_utils.warn(warn_msg)
            logging_utils.error(error_msg)
            logging_utils.log(1, log_msg_1)
            logging_utils.log(2, log_msg_2)
125

T
tianshuo78520a 已提交
126
        result_msg = '\n'.join(
127 128
            [warn_msg, error_msg, "(Level 1) " + log_msg_1, ""]
        )
T
tianshuo78520a 已提交
129
        self.assertEqual(result_msg, stream.getvalue())
130 131 132 133 134

    def test_log_transformed_code(self):
        source_code = "x = 3"
        ast_code = gast.parse(source_code)

T
tianshuo78520a 已提交
135
        stream = io.StringIO()
136 137 138 139
        log = self.translator_logger.logger
        stdout_handler = logging.StreamHandler(stream)
        log.addHandler(stdout_handler)

T
tianshuo78520a 已提交
140 141
        with mock.patch.object(sys, 'stdout', stream):
            paddle.jit.set_code_level(1)
142 143 144
            logging_utils.log_transformed_code(
                1, ast_code, "BasicApiTransformer"
            )
145

T
tianshuo78520a 已提交
146
            paddle.jit.set_code_level()
147 148 149
            logging_utils.log_transformed_code(
                logging_utils.LOG_AllTransformer, ast_code, "All Transformers"
            )
150

T
tianshuo78520a 已提交
151
        self.assertIn(source_code, stream.getvalue())
152 153 154 155


if __name__ == '__main__':
    unittest.main()