source: trunk/packages/xen-3.1/xen-3.1/tools/python/logging/logging-0.4.9.2/test/log_test17.py @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

  • Property svn:executable set to *
  • Property svn:mime-type set to text/script
File size: 3.8 KB
Line 
1#!/usr/bin/env python
2#
3# Copyright 2001-2002 by Vinay Sajip. All Rights Reserved.
4#
5# Permission to use, copy, modify, and distribute this software and its
6# documentation for any purpose and without fee is hereby granted,
7# provided that the above copyright notice appear in all copies and that
8# both that copyright notice and this permission notice appear in
9# supporting documentation, and that the name of Vinay Sajip
10# not be used in advertising or publicity pertaining to distribution
11# of the software without specific, written prior permission.
12# VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
13# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
14# VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
15# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
16# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18#
19# This file is part of the Python logging distribution. See
20# http://www.red-dove.com/python_logging.html
21#
22"""
23A test harness for the logging module. Tests thread safety.
24
25To use as a server, run with no arguments in one process.
26To use as a client, run with arguments "-client <filename>" where <filename>
27is the name of a file containing a logging configuration.
28The example files debug.ini, warn.ini, error.ini and critical.ini are
29provided to use in the test. They each have a customized message format
30(prefixed with their name) and the loggers have their levels set to the
31value implied by their name.
32
33Copyright (C) 2001-2002 Vinay Sajip. All Rights Reserved.
34"""
35
36import sys, logging, logging.config, thread, threading, random, time, struct
37
38NUM_THREADS = 10
39LOOP_COUNT = 10
40
41CONFIG_PORT = 9077
42
43logging.raiseExceptions = 1
44
45LOG_MESSAGES = [
46    (logging.DEBUG, "%3d This is a %s message", "debug"),
47    (logging.INFO, "%3d This is an %s message", "informational"),
48    (logging.WARNING, "%3d This is a %s message", "warning"),
49    (logging.ERROR, "%3d This is an %s message", "error"),
50    (logging.CRITICAL, "%3d This is a %s message", "critical"),
51]
52
53LOG_NAMES = ["A", "A.B", "A.B.C", "A.B.C.D"]
54
55def doLog(num):
56    logger = logging.getLogger('')
57    logger.setLevel(logging.DEBUG)
58    logger.info("*** thread %s started (%d)", thread.get_ident(), num)
59    for i in xrange(LOOP_COUNT):
60        logger = logging.getLogger(random.choice(LOG_NAMES))
61        a = random.choice(LOG_MESSAGES)
62        args = a[0:2] + (num,) + a[2:]
63        time.sleep(random.random() * 3)
64        apply(logger.log, args)
65
66def runserver():
67    f = logging.Formatter("%(asctime)s %(levelname)-9s %(name)-8s %(thread)5s %(message)s")
68    root = logging.getLogger('')
69    h = logging.StreamHandler()
70    root.addHandler(h)
71    h.setFormatter(f)
72    threads = []
73    for i in xrange(NUM_THREADS):
74        threads.append(threading.Thread(target=doLog, args=(len(threads),)))
75    threads.append(logging.config.listen(CONFIG_PORT))    #don't use default port
76    for t in threads:
77        t.start()
78    for t in threads[:-1]:
79        t.join()
80    logging.config.stopListening()
81    threads[-1].join()
82
83def runclient(fname):
84    import socket
85
86    print "configuring with '%s'" % fname
87    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
88    sock.connect(('localhost', CONFIG_PORT))
89    f = open(fname, "r")
90    s = f.read()
91    f.close()
92    slen = struct.pack(">L", len(s))
93    s = slen + s
94    sentsofar = 0
95    left = len(s)
96    while left > 0:
97        sent = sock.send(s[sentsofar:])
98        sentsofar = sentsofar + sent
99        left = left - sent
100    sock.close()
101
102if __name__ == "__main__":
103    if "-client" not in sys.argv:
104        runserver()
105    else:
106        sys.argv.remove("-client")
107        if len(sys.argv) > 1:
108            fname = sys.argv[1]
109        else:
110            fname = "warn.ini"
111        runclient(fname)
Note: See TracBrowser for help on using the repository browser.