mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-04 23:02:02 +01:00
log to file even if --debug not on
This commit is contained in:
parent
c31bc3c0eb
commit
434096046b
1 changed files with 28 additions and 18 deletions
|
@ -6,11 +6,16 @@ import argparse, datetime, json, os, random, shutil, signal, \
|
||||||
g_NAMES = ['Brynn', 'Ariela', 'Kati', 'Eric']
|
g_NAMES = ['Brynn', 'Ariela', 'Kati', 'Eric']
|
||||||
gDone = False
|
gDone = False
|
||||||
gGamesMade = 0
|
gGamesMade = 0
|
||||||
|
g_LOGFILE = None
|
||||||
|
|
||||||
def log(args, msg):
|
def log(args, msg):
|
||||||
if args.VERBOSE:
|
|
||||||
now = datetime.datetime.strftime(datetime.datetime.now(), '%X.%f')
|
now = datetime.datetime.strftime(datetime.datetime.now(), '%X.%f')
|
||||||
print('{} {}'.format(now, msg))
|
msg = '{} {}'.format(now, msg)
|
||||||
|
if args.VERBOSE:
|
||||||
|
print(msg)
|
||||||
|
global g_LOGFILE
|
||||||
|
if g_LOGFILE:
|
||||||
|
print(msg, file=g_LOGFILE)
|
||||||
|
|
||||||
def pick_ndevs(args):
|
def pick_ndevs(args):
|
||||||
RNUM = random.randint(0, 99)
|
RNUM = random.randint(0, 99)
|
||||||
|
@ -131,16 +136,8 @@ class Device():
|
||||||
_nSteps = 0
|
_nSteps = 0
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def setup():
|
def setup(logdir):
|
||||||
logdir = os.path.splitext(os.path.basename(sys.argv[0]))[0] + '_logs'
|
|
||||||
Device._logdir = logdir
|
Device._logdir = logdir
|
||||||
# Move an existing logdir aside
|
|
||||||
if os.path.exists(logdir):
|
|
||||||
shutil.rmtree(logdir)
|
|
||||||
# shutil.move(logdir, '/tmp/' + logdir + '_' + str(random.randint(0, 100000)))
|
|
||||||
os.mkdir(logdir)
|
|
||||||
for d in ['done', 'dead',]:
|
|
||||||
os.mkdir(logdir + '/' + d)
|
|
||||||
|
|
||||||
def __init__(self, args, host):
|
def __init__(self, args, host):
|
||||||
self.args = args
|
self.args = args
|
||||||
|
@ -656,12 +653,6 @@ def parseArgs():
|
||||||
|
|
||||||
def assignDefaults(args):
|
def assignDefaults(args):
|
||||||
if len(args.DICTS) == 0: args.DICTS.append('CollegeEng_2to8.xwd')
|
if len(args.DICTS) == 0: args.DICTS.append('CollegeEng_2to8.xwd')
|
||||||
args.LOGDIR = os.path.splitext(os.path.basename(sys.argv[0]))[0] + '_logs'
|
|
||||||
# Move an existing logdir aside
|
|
||||||
if os.path.exists(args.LOGDIR):
|
|
||||||
shutil.move(args.LOGDIR, '/tmp/' + args.LOGDIR + '_' + str(random.randint(0, 100000)))
|
|
||||||
for d in ['', 'done', 'dead',]:
|
|
||||||
os.mkdir(args.LOGDIR + '/' + d)
|
|
||||||
|
|
||||||
def termHandler(signum, frame):
|
def termHandler(signum, frame):
|
||||||
global gDone
|
global gDone
|
||||||
|
@ -670,18 +661,35 @@ def termHandler(signum, frame):
|
||||||
|
|
||||||
def printError(msg): print( 'ERROR: {}'.format(msg))
|
def printError(msg): print( 'ERROR: {}'.format(msg))
|
||||||
|
|
||||||
|
def initLogs():
|
||||||
|
scriptName = os.path.splitext(os.path.basename(sys.argv[0]))[0]
|
||||||
|
logdir = scriptName + '_logs'
|
||||||
|
if os.path.exists(logdir):
|
||||||
|
shutil.rmtree(logdir)
|
||||||
|
# shutil.move(logdir, '/tmp/' + logdir + '_' + str(random.randint(0, 100000)))
|
||||||
|
os.mkdir(logdir)
|
||||||
|
|
||||||
|
logfilepath = '{}/{}_log.txt'.format(logdir, scriptName)
|
||||||
|
global g_LOGFILE
|
||||||
|
g_LOGFILE = open(logfilepath, 'w')
|
||||||
|
|
||||||
|
return logdir
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
startTime = datetime.datetime.now()
|
startTime = datetime.datetime.now()
|
||||||
signal.signal(signal.SIGINT, termHandler)
|
signal.signal(signal.SIGINT, termHandler)
|
||||||
|
|
||||||
|
logdir = initLogs()
|
||||||
|
|
||||||
args = parseArgs()
|
args = parseArgs()
|
||||||
|
|
||||||
if args.SEED: random.seed(args.SEED)
|
if args.SEED: random.seed(args.SEED)
|
||||||
# Hack: old files confuse things. Remove is simple fix good for now
|
# Hack: old files confuse things. Remove is simple fix good for now
|
||||||
if args.WITH_SMS:
|
if args.WITH_SMS:
|
||||||
try: rmtree('/tmp/xw_sms')
|
try: rmtree('/tmp/xw_sms')
|
||||||
except: None
|
except: None
|
||||||
|
|
||||||
Device.setup()
|
Device.setup(logdir) # deletes old logdif
|
||||||
devs = build_devs(args)
|
devs = build_devs(args)
|
||||||
for dev in devs:
|
for dev in devs:
|
||||||
dev.init()
|
dev.init()
|
||||||
|
@ -697,6 +705,8 @@ def main():
|
||||||
elapsed = datetime.datetime.now() - startTime
|
elapsed = datetime.datetime.now() - startTime
|
||||||
print('played {} games in {}'.format(gGamesMade, elapsed))
|
print('played {} games in {}'.format(gGamesMade, elapsed))
|
||||||
|
|
||||||
|
if g_LOGFILE: g_LOGFILE.close()
|
||||||
|
|
||||||
if args.OPEN_ON_EXIT: openOnExit(args)
|
if args.OPEN_ON_EXIT: openOnExit(args)
|
||||||
|
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
Loading…
Reference in a new issue