Package fife :: Package extensions :: Module fifelog
[hide private]
[frames] | no frames]

Source Code for Module fife.extensions.fifelog

  1  # -*- coding: utf-8 -*- 
  2   
  3  # #################################################################### 
  4  #  Copyright (C) 2005-2010 by the FIFE team 
  5  #  http://www.fifengine.net 
  6  #  This file is part of FIFE. 
  7  # 
  8  #  FIFE is free software; you can redistribute it and/or 
  9  #  modify it under the terms of the GNU Lesser General Public 
 10  #  License as published by the Free Software Foundation; either 
 11  #  version 2.1 of the License, or (at your option) any later version. 
 12  # 
 13  #  This library is distributed in the hope that it will be useful, 
 14  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 16  #  Lesser General Public License for more details. 
 17  # 
 18  #  You should have received a copy of the GNU Lesser General Public 
 19  #  License along with this library; if not, write to the 
 20  #  Free Software Foundation, Inc., 
 21  #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 22  # #################################################################### 
 23   
 24  from fife import fife 
 25   
26 -class LogManager(object):
27 """ 28 Log manager provides convenient apis to access engine logging functionality. 29 You can set log targets individually (prompt, file). You can also adjust 30 things like visible modules through log manager. 31 """ 32
33 - def __init__(self, engine, promptlog=True, filelog=False):
34 """ 35 Constructs new log manager 36 @param engine: Engine to hook into 37 @param promptlog: If true, logs to prompt 38 @param filelog: If true, logs to file (fife.log) 39 """ 40 self.engine = engine 41 self.lm = engine.getLogManager() 42 self.lm.setLogToPrompt(promptlog) 43 self.lm.setLogToFile(filelog) 44 self.mod2name = {} 45 for k, v in fife.__dict__.items(): 46 if k.startswith('LM_') and k not in ('LM_CORE', 'LM_MODULE_MAX'): 47 self.mod2name[v] = self.lm.getModuleName(v) 48 self.name2mod = dict([(v.lower(), k) for k, v in self.mod2name.items()])
49
50 - def addVisibleModules(self, *names):
51 """ 52 Adds modules that are visible in logs. By default, all modules 53 are disabled. Does not remove previously visible modules 54 @param names: module names to set visible 55 @see: modules.h file for available modules in the engine 56 """ 57 names = [n.lower() for n in names] 58 if 'all' in names: 59 for k in self.mod2name.keys(): 60 self.lm.addVisibleModule(k) 61 else: 62 for m in names: 63 try: 64 self.lm.addVisibleModule(self.name2mod[m]) 65 except KeyError: 66 print 'Tried to enable non-existing log module "%s"' % m
67
68 - def removeVisibleModules(self, *names):
69 """ 70 Removes modules that are visible in logs. By default, all modules 71 are disabled. 72 @param names: module names to set invisible 73 @see: addVisibleModules 74 """ 75 names = [n.lower() for n in names] 76 if 'all' in names: 77 for k in self.mod2name.keys(): 78 self.lm.removeVisibleModule(k) 79 else: 80 for m in names: 81 self.lm.removeVisibleModule(self.name2mod[m])
82
83 - def getVisibleModules(self):
84 """ 85 Gets currently visible modules 86 @see: addVisibleModules 87 """ 88 mods = [] 89 for k in self.mod2name.keys(): 90 if self.lm.isVisible(k): 91 mods.append(self.mod2name[k])
92
93 - def setVisibleModules(self, *names):
94 """ 95 Sets visible modules. Clears previously set modules. 96 @param names: module names to set visible 97 @see: addVisibleModules 98 """ 99 self.lm.clearVisibleModules() 100 self.addVisibleModules(*names)
101
102 - def setLevelFilter(self, fltr):
103 """ 104 Sets the minimum log level to view. 105 106 @param fltr: The filter level 107 108 Valid values: 109 fife.LogManager.LEVEL_DEBUG 110 fife.LogManager.LEVEL_LOG 111 fife.LogManager.LEVEL_WARN 112 fife.LogManager.LEVEL_ERROR 113 """ 114 self.lm.setLevelFilter(fltr)
115
116 - def log_debug(self, message):
117 self.lm.log(0, self.name2mod["script"], message)
118
119 - def log_log(self, message):
120 self.lm.log(1, self.name2mod["script"], message)
121
122 - def log_warn(self, message):
123 self.lm.log(2, self.name2mod["script"], message)
124
125 - def log_error(self, message):
126 self.lm.log(3, self.name2mod["script"], message)
127