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