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