Package sfc :: Package common :: Module output
[hide private]
[frames] | no frames]

Source Code for Module sfc.common.output

 1  #!/usr/bin/env python 
 2  # -*- coding: utf-8 -*- 
 3  """ 
 4  This module contains output and logging utilities. 
 5  """ 
 6   
 7  # Copyright (C) 2008 Martin Sandve Alnes and Simula Resarch Laboratory 
 8  # 
 9  # This file is part of SyFi. 
10  # 
11  # SyFi is free software: you can redistribute it and/or modify 
12  # it under the terms of the GNU General Public License as published by 
13  # the Free Software Foundation, either version 2 of the License, or 
14  # (at your option) any later version. 
15  # 
16  # SyFi is distributed in the hope that it will be useful, 
17  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
18  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
19  # GNU General Public License for more details. 
20  # 
21  # You should have received a copy of the GNU General Public License 
22  # along with SyFi. If not, see <http://www.gnu.org/licenses/>. 
23  # 
24  # First added:  2008-09-03 
25  # Last changed: 2008-09-03 
26   
27  import logging 
28   
29  # Logging wrappers 
30   
31  _log = logging.getLogger("sfc") 
32  _loghandler = logging.StreamHandler() 
33  _log.addHandler(_loghandler) 
34  _log.setLevel(logging.WARNING) 
35  #_log.setLevel(logging.DEBUG) 
36   
37 -def get_log_handler():
38 return _loghandler
39
40 -def get_logger():
41 return _log
42
43 -def set_log_handler(handler):
44 global _loghandler 45 _log.removeHandler(_loghandler) 46 _loghandler = handler 47 _log.addHandler(_loghandler)
48
49 -def set_logging_level(level):
50 if isinstance(level, str): 51 level = level.upper() 52 assert level in ("INFO", "WARNING", "ERROR", "DEBUG") 53 level = getattr(logging, level) 54 else: 55 assert isinstance(level, int) 56 _log.setLevel(level)
57 58 # Aliases for calling log consistently: 59
60 -def sfc_debug(*message):
61 _log.debug(*message)
62
63 -def sfc_info(*message):
64 _log.info(*message)
65
66 -def sfc_warning(*message):
67 _log.warning(*message)
68
69 -def sfc_error(*message):
70 _log.error(*message) 71 text = message[0] % message[1:] 72 raise RuntimeError(text)
73
74 -def sfc_assert(condition, *message):
75 if not condition: 76 _log.error(*message) 77 text = message[0] % message[1:] 78 raise AssertionError(text)
79 80 # Utility functions for file handling: 81
82 -def write_file(filename, text):
83 "Write text to a file and close it." 84 try: 85 f = open(filename, "w") 86 f.write(text) 87 f.close() 88 except IOError, e: 89 sfc_error("Can't open '%s': %s" % (filename, e))
90