source: trunk/anuga_core/source/anuga/utilities/log_test.py @ 8710

Last change on this file since 8710 was 8145, checked in by wilsonr, 14 years ago

Removed '@brief' comments.

File size: 3.1 KB
Line 
1#!/usr/bin/env python
2
3import os
4import sys
5import unittest
6import logging
7import log
8
9LOGFILE_NAME = 'test.log'
10STDOUT_LOG_NAME = 'stdout.log'
11
12
13class Test_Log(unittest.TestCase):
14    def setUp(self):
15        # just in case
16        if os.path.exists(LOGFILE_NAME):
17            os.remove(LOGFILE_NAME)
18        if os.path.exists(STDOUT_LOG_NAME):
19            os.remove(STDOUT_LOG_NAME)
20
21        # set log module logfile name
22        log.log_filename = LOGFILE_NAME
23
24        # set logging levels for this test
25        log.console_logging_level = logging.INFO
26        log.log_logging_level = logging.DEBUG
27
28    def tearDown(self):
29        if os.path.exists(LOGFILE_NAME):
30            os.remove(LOGFILE_NAME)
31        if os.path.exists(STDOUT_LOG_NAME):
32            os.remove(STDOUT_LOG_NAME)
33
34    def test_simple(self):
35        '''Check that logging works in simple case.'''
36
37        # vvvvv  WARNING - DO NOT REFORMAT THESE LINES!  vvvvv
38        log_expect = '''2009-03-23 12:16:53,487 CRITICAL                       log:0   |Logfile is 'test.log' with logging level of DEBUG, console logging level is INFO
392009-03-23 12:16:53,488 DEBUG                     test_log:37  |test at level DEBUG
402009-03-23 12:35:26,107 INFO                      test_log:97  |Resource usage: memory=0.0 resident=0.0 stacksize=0.0
412009-03-23 12:16:53,488 INFO                      test_log:38  |test at level INFO'''
42
43        stdout_expect = '''Logfile is 'test.log' with logging level of DEBUG, console logging level is INFO
44Resource usage: memory=0.0 resident=0.0 stacksize=0.0
45test at level INFO'''
46        # ^^^^^  WARNING - DO NOT REFORMAT THESE LINES!  ^^^^^
47
48        # capture stdout to a file
49        save_stdout = sys.stdout
50        save_stderr = sys.stderr
51        sys.stdout = sys.stderr = open(STDOUT_LOG_NAME, 'w')
52
53        # do some logging
54        log.debug('test at level DEBUG')
55        log.resource_usage(logging.INFO)
56        log.info('test at level INFO')
57
58        # put stdout/stderr back to normal
59        sys.stderr = save_stderr
60        sys.stdout = save_stdout
61
62        # check logfile is as expected
63        fd = open(LOGFILE_NAME, 'r')
64        lines = fd.readlines()
65        fd.close()
66        result = []
67        for line in lines:
68            l = line.strip('\n')
69            ndx = l.index('|')
70            result.append([l.split()[2], l[ndx:]])
71        expected = []
72        for line in log_expect.split('\n'):
73            ndx = line.index('|')
74            expected.append([line.split()[2], line[ndx:]])
75        self.failUnlessEqual(result, expected)
76
77        # check that captured stdout is as expected
78        fd = open(STDOUT_LOG_NAME, 'r')
79        lines = fd.readlines()
80        fd.close()
81        result = []
82        for line in lines:
83            l = line.strip('\n')
84            result.append(l)
85        expected = []
86        for line in stdout_expect.split('\n'):
87            expected.append(line)
88        self.failUnlessEqual(result, expected)
89
90################################################################################
91
92if __name__ == "__main__":
93    suite = unittest.makeSuite(Test_Log, 'test')
94    runner = unittest.TextTestRunner()
95    runner.run(suite)
Note: See TracBrowser for help on using the repository browser.