PYTHON   11

log.py

Guest on 26th May 2021 07:43:20 PM

  1. #
  2. #  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  3. #  SLEPc - Scalable Library for Eigenvalue Problem Computations
  4. #  Copyright (c) 2002-2021, Universitat Politecnica de Valencia, Spain
  5. #
  6. #  This file is part of SLEPc.
  7. #  SLEPc is distributed under a 2-clause BSD license (see LICENSE).
  8. #  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  9. #
  10.  
  11. from __future__ import print_function
  12. import os, sys
  13.  
  14. class Log:
  15.  
  16.   def __init__(self):
  17.     self.lastfailed = False
  18.  
  19.   def Open(self,slepcdir,confdir,fname):
  20.     filename = os.path.join(confdir,fname)
  21.     self.fd = open(filename,'w')
  22.     try:
  23.       self.filename = os.path.relpath(filename,slepcdir)  # needs python-2.6
  24.     except AttributeError:
  25.       self.filename = filename
  26.     try: # symbolic link to log file in current directory
  27.       if os.path.isfile(fname) or os.path.islink(fname): os.remove(fname)
  28.       os.symlink(self.filename,fname)
  29.     except: pass
  30.  
  31.   def Println(self,string):
  32.     print(string)
  33.     if hasattr(self,'fd'):
  34.       self.fd.write(string+'\n')
  35.  
  36.   def Print(self,string):
  37.     print(string, end=' ')
  38.     if hasattr(self,'fd'):
  39.       self.fd.write(string+' ')
  40.  
  41.   def NewSection(self,string):
  42.     if self.lastfailed:
  43.       colorfail = '\033[91m'
  44.       colornorm = '\033[0m'
  45.       print(colorfail+'failed'+colornorm+'\n'+string, end=' ')
  46.     else:
  47.       print('done\n'+string, end=' ')
  48.     sys.stdout.flush()
  49.     self.fd.write('='*80+'\n'+string+'\n')
  50.     self.lastfailed = False
  51.  
  52.   def write(self,string):
  53.     self.fd.write(string+'\n')
  54.  
  55.   def Warn(self,string):
  56.     msg = '\nxxx'+'='*74+'xxx\nWARNING: '+string+'\nxxx'+'='*74+'xxx'
  57.     print(msg)
  58.     if hasattr(self,'fd'):
  59.       self.fd.write(msg+'\n')
  60.  
  61.   def Exit(self,string):
  62.     msg = '\nERROR: '+string
  63.     print(msg)
  64.     if hasattr(self,'fd'):
  65.       self.fd.write(msg+'\n')
  66.       self.fd.close()
  67.       msg = 'ERROR: See "' + self.filename + '" file for details'
  68.     else:
  69.       msg = 'ERROR during configure (log file not open yet)'
  70.     sys.exit(msg)
  71.  
  72.   def setLastFailed(self):
  73.     self.lastfailed = True

Raw Paste


Login or Register to edit or fork this paste. It's free.