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): 133*dc0f114dSBarry Smith if self.debugLevel <= 3: return 1342d964b9fSJed Brown import io 135a75b4e77SMatthew G. Knepley self.logBkp = self.log 1362d964b9fSJed Brown if sys.version_info < (3,): 1372d964b9fSJed Brown self.log = io.BytesIO() 1382d964b9fSJed Brown else: 1392d964b9fSJed Brown self.log = io.StringIO() 140a75b4e77SMatthew G. Knepley 141a75b4e77SMatthew G. Knepley def restoreLog(self): 142*dc0f114dSBarry Smith if self.debugLevel <= 3: return 143a75b4e77SMatthew G. Knepley s = self.log.getvalue() 144a75b4e77SMatthew G. Knepley self.log.close() 145a75b4e77SMatthew G. Knepley self.log = self.logBkp 146a75b4e77SMatthew G. Knepley del(self.logBkp) 147a75b4e77SMatthew G. Knepley return s 148a75b4e77SMatthew G. Knepley 149179860b2SJed Brown def getLinewidth(self): 150179860b2SJed Brown global LineWidth 151179860b2SJed Brown if not hasattr(self, '_linewidth'): 152179860b2SJed Brown if self.out is None or not self.out.isatty() or self.argDB['scrollOutput']: 153179860b2SJed Brown self._linewidth = -1 154179860b2SJed Brown else: 155179860b2SJed Brown if LineWidth == -1: 156179860b2SJed Brown try: 157179860b2SJed Brown import curses 158179860b2SJed Brown 159179860b2SJed Brown try: 160179860b2SJed Brown curses.setupterm() 161179860b2SJed Brown (y, self._linewidth) = curses.initscr().getmaxyx() 162179860b2SJed Brown curses.endwin() 163179860b2SJed Brown except curses.error: 164179860b2SJed Brown self._linewidth = -1 165179860b2SJed Brown except: 166179860b2SJed Brown self._linewidth = -1 167179860b2SJed Brown LineWidth = self._linewidth 168179860b2SJed Brown else: 169179860b2SJed Brown self._linewidth = LineWidth 170179860b2SJed Brown return self._linewidth 171179860b2SJed Brown def setLinewidth(self, linewidth): 172179860b2SJed Brown self._linewidth = linewidth 173179860b2SJed Brown return 174179860b2SJed Brown linewidth = property(getLinewidth, setLinewidth, doc = 'The maximum number of characters per log line') 175179860b2SJed Brown 176179860b2SJed Brown def checkWrite(self, f, debugLevel, debugSection, writeAll = 0): 177179860b2SJed Brown '''Check whether the log line should be written 178179860b2SJed Brown - If writeAll is true, return true 179179860b2SJed Brown - If debugLevel >= current level, and debugSection in current section or sections is empty, return true''' 180179860b2SJed Brown if not isinstance(debugLevel, int): 181179860b2SJed Brown raise RuntimeError('Debug level must be an integer: '+str(debugLevel)) 182179860b2SJed Brown if f is None: 183179860b2SJed Brown return False 184179860b2SJed Brown if writeAll: 185179860b2SJed Brown return True 186179860b2SJed Brown if self.debugLevel >= debugLevel and (not len(self.debugSections) or debugSection in self.debugSections): 187179860b2SJed Brown return True 188179860b2SJed Brown return False 189179860b2SJed Brown 190179860b2SJed Brown def logIndent(self, debugLevel = -1, debugSection = None, comm = None): 191179860b2SJed Brown '''Write the proper indentation to the log streams''' 192179860b2SJed Brown import traceback 193179860b2SJed Brown 194179860b2SJed Brown indentLevel = len(traceback.extract_stack())-5 195179860b2SJed Brown for writeAll, f in enumerate([self.out, self.log]): 196179860b2SJed Brown if self.checkWrite(f, debugLevel, debugSection, writeAll): 197179860b2SJed Brown if not comm is None: 198179860b2SJed Brown f.write('[') 199179860b2SJed Brown f.write(str(comm.rank())) 200179860b2SJed Brown f.write(']') 201179860b2SJed Brown for i in range(indentLevel): 202179860b2SJed Brown f.write(self.debugIndent) 203179860b2SJed Brown return 204179860b2SJed Brown 205179860b2SJed Brown def logBack(self): 206179860b2SJed Brown '''Backup the current line if we are not scrolling output''' 207179860b2SJed Brown if not self.out is None and self.linewidth > 0: 208179860b2SJed Brown self.out.write('\r') 209179860b2SJed Brown return 210179860b2SJed Brown 211179860b2SJed Brown def logClear(self): 212179860b2SJed Brown '''Clear the current line if we are not scrolling output''' 213179860b2SJed Brown if not self.out is None and self.linewidth > 0: 214179860b2SJed Brown self.out.write('\r') 215179860b2SJed Brown self.out.write(''.join([' '] * self.linewidth)) 216179860b2SJed Brown self.out.write('\r') 217179860b2SJed Brown return 218179860b2SJed Brown 219179860b2SJed Brown def logPrintDivider(self, debugLevel = -1, debugSection = None, single = 0): 220179860b2SJed Brown if single: 221b5f71184SBarry Smith self.logPrint('---------------------------------------------------------------------------------------------', debugLevel = debugLevel, debugSection = debugSection) 222179860b2SJed Brown else: 223b5f71184SBarry Smith self.logPrint('=============================================================================================', debugLevel = debugLevel, debugSection = debugSection) 224179860b2SJed Brown return 225179860b2SJed Brown 226179860b2SJed Brown def logPrintBox(self,msg, debugLevel = -1, debugSection = 'screen', indent = 1, comm = None): 227179860b2SJed Brown self.logClear() 228179860b2SJed Brown self.logPrintDivider(debugLevel = debugLevel, debugSection = debugSection) 229179860b2SJed Brown [self.logPrint(' '+line, debugLevel = debugLevel, debugSection = debugSection) for line in msg.split('\n')] 230179860b2SJed Brown self.logPrintDivider(debugLevel = debugLevel, debugSection = debugSection) 231179860b2SJed Brown self.logPrint('', debugLevel = debugLevel, debugSection = debugSection) 232179860b2SJed Brown return 233179860b2SJed Brown 234179860b2SJed Brown def logClearRemoveDirectory(self): 235179860b2SJed Brown global RemoveDirectory 236179860b2SJed Brown global backupRemoveDirectory 237179860b2SJed Brown backupRemoveDirectory = RemoveDirectory 238179860b2SJed Brown RemoveDirectory = '' 239179860b2SJed Brown 240179860b2SJed Brown def logResetRemoveDirectory(self): 241179860b2SJed Brown global RemoveDirectory 242179860b2SJed Brown global backupRemoveDirectory 243179860b2SJed Brown RemoveDirectory = backupRemoveDirectory 244179860b2SJed Brown 245179860b2SJed Brown 246179860b2SJed Brown def logWrite(self, msg, debugLevel = -1, debugSection = None, forceScroll = 0): 247179860b2SJed Brown '''Write the message to the log streams''' 248b5f71184SBarry Smith '''Generally goes to the file but not the screen''' 249*dc0f114dSBarry Smith if not msg: return 250179860b2SJed Brown for writeAll, f in enumerate([self.out, self.log]): 251179860b2SJed Brown if self.checkWrite(f, debugLevel, debugSection, writeAll): 252179860b2SJed Brown if not forceScroll and not writeAll and self.linewidth > 0: 253179860b2SJed Brown global RemoveDirectory 254179860b2SJed Brown self.logBack() 255179860b2SJed Brown msg = msg.replace(RemoveDirectory,'') 256179860b2SJed Brown for ms in msg.split('\n'): 257179860b2SJed Brown f.write(ms[0:self.linewidth]) 258179860b2SJed Brown f.write(''.join([' '] * (self.linewidth - len(ms)))) 259179860b2SJed Brown else: 260179860b2SJed Brown if not debugSection is None and not debugSection == 'screen' and len(msg): 261179860b2SJed Brown f.write(str(debugSection)) 262179860b2SJed Brown f.write(': ') 263179860b2SJed Brown f.write(msg) 264179860b2SJed Brown if hasattr(f, 'flush'): 265179860b2SJed Brown f.flush() 266179860b2SJed Brown return 267179860b2SJed Brown 268179860b2SJed Brown def logPrint(self, msg, debugLevel = -1, debugSection = None, indent = 1, comm = None, forceScroll = 0): 269179860b2SJed Brown '''Write the message to the log streams with proper indentation and a newline''' 270b5f71184SBarry Smith '''Generally goes to the file and the screen''' 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