1179860b2SJed Brownimport args 2179860b2SJed Brownimport sys 3179860b2SJed Brownimport os 4179860b2SJed Brown 5179860b2SJed Brown# Ugly stuff to have curses called ONLY once, instead of for each 6179860b2SJed Brown# new Configure object created (and flashing the screen) 7179860b2SJed Brownglobal LineWidth 8179860b2SJed Brownglobal RemoveDirectory 9179860b2SJed Brownglobal backupRemoveDirectory 10179860b2SJed BrownLineWidth = -1 11179860b2SJed BrownRemoveDirectory = os.path.join(os.getcwd(),'') 12179860b2SJed BrownbackupRemoveDirectory = '' 13179860b2SJed Brown 14179860b2SJed Brown# Compatibility fixes 15179860b2SJed Browntry: 16179860b2SJed Brown enumerate([0, 1]) 17179860b2SJed Brownexcept NameError: 18179860b2SJed Brown def enumerate(l): 19179860b2SJed Brown return zip(range(len(l)), l) 20179860b2SJed Browntry: 21179860b2SJed Brown True, False 22179860b2SJed Brownexcept NameError: 23179860b2SJed Brown True, False = (0==0, 0!=0) 24179860b2SJed Brown 25179860b2SJed Brownclass Logger(args.ArgumentProcessor): 26179860b2SJed Brown '''This class creates a shared log and provides methods for writing to it''' 27179860b2SJed Brown defaultLog = None 28179860b2SJed Brown defaultOut = sys.stdout 29179860b2SJed Brown 30179860b2SJed Brown def __init__(self, clArgs = None, argDB = None, log = None, out = defaultOut, debugLevel = None, debugSections = None, debugIndent = None): 31179860b2SJed Brown args.ArgumentProcessor.__init__(self, clArgs, argDB) 32179860b2SJed Brown self.logName = None 33179860b2SJed Brown self.log = log 34179860b2SJed Brown self.out = out 35179860b2SJed Brown self.debugLevel = debugLevel 36179860b2SJed Brown self.debugSections = debugSections 37179860b2SJed Brown self.debugIndent = debugIndent 38179860b2SJed Brown self.getRoot() 39179860b2SJed Brown return 40179860b2SJed Brown 41179860b2SJed Brown def __getstate__(self): 42179860b2SJed Brown '''We do not want to pickle the default log stream''' 43179860b2SJed Brown d = args.ArgumentProcessor.__getstate__(self) 44179860b2SJed Brown if 'log' in d: 45179860b2SJed Brown if d['log'] is Logger.defaultLog: 46179860b2SJed Brown del d['log'] 47179860b2SJed Brown else: 48179860b2SJed Brown d['log'] = None 49179860b2SJed Brown if 'out' in d: 50179860b2SJed Brown if d['out'] is Logger.defaultOut: 51179860b2SJed Brown del d['out'] 52179860b2SJed Brown else: 53179860b2SJed Brown d['out'] = None 54179860b2SJed Brown return d 55179860b2SJed Brown 56179860b2SJed Brown def __setstate__(self, d): 57179860b2SJed Brown '''We must create the default log stream''' 58179860b2SJed Brown args.ArgumentProcessor.__setstate__(self, d) 59179860b2SJed Brown if not 'log' in d: 60179860b2SJed Brown self.log = self.createLog(None) 61179860b2SJed Brown if not 'out' in d: 62179860b2SJed Brown self.out = Logger.defaultOut 63179860b2SJed Brown self.__dict__.update(d) 64179860b2SJed Brown return 65179860b2SJed Brown 66179860b2SJed Brown def setupArguments(self, argDB): 67179860b2SJed Brown '''Setup types in the argument database''' 68179860b2SJed Brown import nargs 69179860b2SJed Brown 70179860b2SJed Brown argDB = args.ArgumentProcessor.setupArguments(self, argDB) 71179860b2SJed Brown argDB.setType('log', nargs.Arg(None, 'build.log', 'The filename for the log')) 72179860b2SJed Brown argDB.setType('logAppend', nargs.ArgBool(None, 0, 'The flag determining whether we backup or append to the current log', isTemporary = 1)) 73179860b2SJed Brown argDB.setType('debugLevel', nargs.ArgInt(None, 3, 'Integer 0 to 4, where a higher level means more detail', 0, 5)) 74179860b2SJed Brown argDB.setType('debugSections', nargs.Arg(None, [], 'Message types to print, e.g. [compile,link,hg,install]')) 75179860b2SJed Brown argDB.setType('debugIndent', nargs.Arg(None, ' ', 'The string used for log indentation')) 76179860b2SJed Brown argDB.setType('scrollOutput', nargs.ArgBool(None, 0, 'Flag to allow output to scroll rather than overwriting a single line')) 77179860b2SJed Brown argDB.setType('noOutput', nargs.ArgBool(None, 0, 'Flag to suppress output to the terminal')) 78179860b2SJed Brown return argDB 79179860b2SJed Brown 80179860b2SJed Brown def setup(self): 81179860b2SJed Brown '''Setup the terminal output and filtering flags''' 82179860b2SJed Brown self.log = self.createLog(self.logName, self.log) 83179860b2SJed Brown args.ArgumentProcessor.setup(self) 84179860b2SJed Brown 85179860b2SJed Brown if self.argDB['noOutput']: 86179860b2SJed Brown self.out = None 87179860b2SJed Brown if self.debugLevel is None: 88179860b2SJed Brown self.debugLevel = self.argDB['debugLevel'] 89179860b2SJed Brown if self.debugSections is None: 90179860b2SJed Brown self.debugSections = self.argDB['debugSections'] 91179860b2SJed Brown if self.debugIndent is None: 92179860b2SJed Brown self.debugIndent = self.argDB['debugIndent'] 93179860b2SJed Brown return 94179860b2SJed Brown 95179860b2SJed Brown def checkLog(self, logName): 96179860b2SJed Brown import nargs 97179860b2SJed Brown import os 98179860b2SJed Brown 99179860b2SJed Brown if logName is None: 100179860b2SJed Brown logName = nargs.Arg.findArgument('log', self.clArgs) 101179860b2SJed Brown if logName is None: 102179860b2SJed Brown if not self.argDB is None and 'log' in self.argDB: 103179860b2SJed Brown logName = self.argDB['log'] 104179860b2SJed Brown else: 105179860b2SJed Brown logName = 'default.log' 106179860b2SJed Brown self.logName = logName 107179860b2SJed Brown self.logExists = os.path.exists(self.logName) 108179860b2SJed Brown return self.logExists 109179860b2SJed Brown 110179860b2SJed Brown def createLog(self, logName, initLog = None): 111179860b2SJed Brown '''Create a default log stream, unless initLog is given''' 112179860b2SJed Brown import nargs 113179860b2SJed Brown 114179860b2SJed Brown if not initLog is None: 115179860b2SJed Brown log = initLog 116179860b2SJed Brown else: 117179860b2SJed Brown if Logger.defaultLog is None: 118179860b2SJed Brown appendArg = nargs.Arg.findArgument('logAppend', self.clArgs) 119179860b2SJed Brown if self.checkLog(logName): 120179860b2SJed Brown if not self.argDB is None and ('logAppend' in self.argDB and self.argDB['logAppend']) or (not appendArg is None and bool(appendArg)): 121179860b2SJed Brown Logger.defaultLog = file(self.logName, 'a') 122179860b2SJed Brown else: 123179860b2SJed Brown try: 124179860b2SJed Brown import os 125179860b2SJed Brown 126179860b2SJed Brown os.rename(self.logName, self.logName+'.bkp') 127179860b2SJed Brown Logger.defaultLog = file(self.logName, 'w') 128179860b2SJed Brown except OSError: 12915ac2963SJed Brown sys.stdout.write('WARNING: Cannot backup log file, appending instead.\n') 130179860b2SJed Brown Logger.defaultLog = file(self.logName, 'a') 131179860b2SJed Brown else: 132179860b2SJed Brown Logger.defaultLog = file(self.logName, 'w') 133179860b2SJed Brown log = Logger.defaultLog 134179860b2SJed Brown return log 135179860b2SJed Brown 136179860b2SJed Brown def closeLog(self): 137179860b2SJed Brown '''Closes the log file''' 138179860b2SJed Brown self.log.close() 139179860b2SJed Brown 140*a75b4e77SMatthew G. Knepley def saveLog(self): 141*a75b4e77SMatthew G. Knepley import StringIO 142*a75b4e77SMatthew G. Knepley self.logBkp = self.log 143*a75b4e77SMatthew G. Knepley self.log = StringIO.StringIO() 144*a75b4e77SMatthew G. Knepley 145*a75b4e77SMatthew G. Knepley def restoreLog(self): 146*a75b4e77SMatthew G. Knepley s = self.log.getvalue() 147*a75b4e77SMatthew G. Knepley self.log.close() 148*a75b4e77SMatthew G. Knepley self.log = self.logBkp 149*a75b4e77SMatthew G. Knepley del(self.logBkp) 150*a75b4e77SMatthew G. Knepley return s 151*a75b4e77SMatthew G. Knepley 152179860b2SJed Brown def getLinewidth(self): 153179860b2SJed Brown global LineWidth 154179860b2SJed Brown if not hasattr(self, '_linewidth'): 155179860b2SJed Brown if self.out is None or not self.out.isatty() or self.argDB['scrollOutput']: 156179860b2SJed Brown self._linewidth = -1 157179860b2SJed Brown else: 158179860b2SJed Brown if LineWidth == -1: 159179860b2SJed Brown try: 160179860b2SJed Brown import curses 161179860b2SJed Brown 162179860b2SJed Brown try: 163179860b2SJed Brown curses.setupterm() 164179860b2SJed Brown (y, self._linewidth) = curses.initscr().getmaxyx() 165179860b2SJed Brown curses.endwin() 166179860b2SJed Brown except curses.error: 167179860b2SJed Brown self._linewidth = -1 168179860b2SJed Brown except: 169179860b2SJed Brown self._linewidth = -1 170179860b2SJed Brown LineWidth = self._linewidth 171179860b2SJed Brown else: 172179860b2SJed Brown self._linewidth = LineWidth 173179860b2SJed Brown return self._linewidth 174179860b2SJed Brown def setLinewidth(self, linewidth): 175179860b2SJed Brown self._linewidth = linewidth 176179860b2SJed Brown return 177179860b2SJed Brown linewidth = property(getLinewidth, setLinewidth, doc = 'The maximum number of characters per log line') 178179860b2SJed Brown 179179860b2SJed Brown def checkWrite(self, f, debugLevel, debugSection, writeAll = 0): 180179860b2SJed Brown '''Check whether the log line should be written 181179860b2SJed Brown - If writeAll is true, return true 182179860b2SJed Brown - If debugLevel >= current level, and debugSection in current section or sections is empty, return true''' 183179860b2SJed Brown if not isinstance(debugLevel, int): 184179860b2SJed Brown raise RuntimeError('Debug level must be an integer: '+str(debugLevel)) 185179860b2SJed Brown if f is None: 186179860b2SJed Brown return False 187179860b2SJed Brown if writeAll: 188179860b2SJed Brown return True 189179860b2SJed Brown if self.debugLevel >= debugLevel and (not len(self.debugSections) or debugSection in self.debugSections): 190179860b2SJed Brown return True 191179860b2SJed Brown return False 192179860b2SJed Brown 193179860b2SJed Brown def logIndent(self, debugLevel = -1, debugSection = None, comm = None): 194179860b2SJed Brown '''Write the proper indentation to the log streams''' 195179860b2SJed Brown import traceback 196179860b2SJed Brown 197179860b2SJed Brown indentLevel = len(traceback.extract_stack())-5 198179860b2SJed Brown for writeAll, f in enumerate([self.out, self.log]): 199179860b2SJed Brown if self.checkWrite(f, debugLevel, debugSection, writeAll): 200179860b2SJed Brown if not comm is None: 201179860b2SJed Brown f.write('[') 202179860b2SJed Brown f.write(str(comm.rank())) 203179860b2SJed Brown f.write(']') 204179860b2SJed Brown for i in range(indentLevel): 205179860b2SJed Brown f.write(self.debugIndent) 206179860b2SJed Brown return 207179860b2SJed Brown 208179860b2SJed Brown def logBack(self): 209179860b2SJed Brown '''Backup the current line if we are not scrolling output''' 210179860b2SJed Brown if not self.out is None and self.linewidth > 0: 211179860b2SJed Brown self.out.write('\r') 212179860b2SJed Brown return 213179860b2SJed Brown 214179860b2SJed Brown def logClear(self): 215179860b2SJed Brown '''Clear the current line if we are not scrolling output''' 216179860b2SJed Brown if not self.out is None and self.linewidth > 0: 217179860b2SJed Brown self.out.write('\r') 218179860b2SJed Brown self.out.write(''.join([' '] * self.linewidth)) 219179860b2SJed Brown self.out.write('\r') 220179860b2SJed Brown return 221179860b2SJed Brown 222179860b2SJed Brown def logPrintDivider(self, debugLevel = -1, debugSection = None, single = 0): 223179860b2SJed Brown if single: 224179860b2SJed Brown self.logPrint('-------------------------------------------------------------------------------', debugLevel = debugLevel, debugSection = debugSection) 225179860b2SJed Brown else: 226179860b2SJed Brown self.logPrint('===============================================================================', debugLevel = debugLevel, debugSection = debugSection) 227179860b2SJed Brown return 228179860b2SJed Brown 229179860b2SJed Brown def logPrintBox(self,msg, debugLevel = -1, debugSection = 'screen', indent = 1, comm = None): 230179860b2SJed Brown self.logClear() 231179860b2SJed Brown self.logPrintDivider(debugLevel = debugLevel, debugSection = debugSection) 232179860b2SJed Brown [self.logPrint(' '+line, debugLevel = debugLevel, debugSection = debugSection) for line in msg.split('\n')] 233179860b2SJed Brown self.logPrintDivider(debugLevel = debugLevel, debugSection = debugSection) 234179860b2SJed Brown self.logPrint('', debugLevel = debugLevel, debugSection = debugSection) 235179860b2SJed Brown return 236179860b2SJed Brown 237179860b2SJed Brown def logClearRemoveDirectory(self): 238179860b2SJed Brown global RemoveDirectory 239179860b2SJed Brown global backupRemoveDirectory 240179860b2SJed Brown backupRemoveDirectory = RemoveDirectory 241179860b2SJed Brown RemoveDirectory = '' 242179860b2SJed Brown 243179860b2SJed Brown def logResetRemoveDirectory(self): 244179860b2SJed Brown global RemoveDirectory 245179860b2SJed Brown global backupRemoveDirectory 246179860b2SJed Brown RemoveDirectory = backupRemoveDirectory 247179860b2SJed Brown 248179860b2SJed Brown 249179860b2SJed Brown def logWrite(self, msg, debugLevel = -1, debugSection = None, forceScroll = 0): 250179860b2SJed Brown '''Write the message to the log streams''' 251179860b2SJed Brown for writeAll, f in enumerate([self.out, self.log]): 252179860b2SJed Brown if self.checkWrite(f, debugLevel, debugSection, writeAll): 253179860b2SJed Brown if not forceScroll and not writeAll and self.linewidth > 0: 254179860b2SJed Brown global RemoveDirectory 255179860b2SJed Brown self.logBack() 256179860b2SJed Brown msg = msg.replace(RemoveDirectory,'') 257179860b2SJed Brown for ms in msg.split('\n'): 258179860b2SJed Brown f.write(ms[0:self.linewidth]) 259179860b2SJed Brown f.write(''.join([' '] * (self.linewidth - len(ms)))) 260179860b2SJed Brown else: 261179860b2SJed Brown if not debugSection is None and not debugSection == 'screen' and len(msg): 262179860b2SJed Brown f.write(str(debugSection)) 263179860b2SJed Brown f.write(': ') 264179860b2SJed Brown f.write(msg) 265179860b2SJed Brown if hasattr(f, 'flush'): 266179860b2SJed Brown f.flush() 267179860b2SJed Brown return 268179860b2SJed Brown 269179860b2SJed Brown def logPrint(self, msg, debugLevel = -1, debugSection = None, indent = 1, comm = None, forceScroll = 0): 270179860b2SJed Brown '''Write the message to the log streams with proper indentation and a newline''' 271179860b2SJed Brown if indent: 272179860b2SJed Brown self.logIndent(debugLevel, debugSection, comm) 273179860b2SJed Brown self.logWrite(msg, debugLevel, debugSection, forceScroll = forceScroll) 274179860b2SJed Brown for writeAll, f in enumerate([self.out, self.log]): 275179860b2SJed Brown if self.checkWrite(f, debugLevel, debugSection, writeAll): 276179860b2SJed Brown if writeAll or self.linewidth < 0: 277179860b2SJed Brown f.write('\n') 278179860b2SJed Brown return 279179860b2SJed Brown 280179860b2SJed Brown 281179860b2SJed Brown def getRoot(self): 282179860b2SJed Brown '''Return the directory containing this module 283179860b2SJed Brown - This has the problem that when we reload a module of the same name, this gets screwed up 284179860b2SJed Brown Therefore, we call it in the initializer, and stash it''' 285179860b2SJed Brown #print ' In getRoot' 286179860b2SJed Brown #print hasattr(self, '__root') 287179860b2SJed Brown #print ' done checking' 288179860b2SJed Brown if not hasattr(self, '__root'): 289179860b2SJed Brown import os 290179860b2SJed Brown import sys 291179860b2SJed Brown 292179860b2SJed Brown # Work around a bug with pdb in 2.3 293179860b2SJed Brown if hasattr(sys.modules[self.__module__], '__file__') and not os.path.basename(sys.modules[self.__module__].__file__) == 'pdb.py': 294179860b2SJed Brown self.__root = os.path.abspath(os.path.dirname(sys.modules[self.__module__].__file__)) 295179860b2SJed Brown else: 296179860b2SJed Brown self.__root = os.getcwd() 297179860b2SJed Brown #print ' Exiting getRoot' 298179860b2SJed Brown return self.__root 299179860b2SJed Brown def setRoot(self, root): 300179860b2SJed Brown self.__root = root 301179860b2SJed Brown return 302179860b2SJed Brown root = property(getRoot, setRoot, doc = 'The directory containing this module') 303