Skip to content

Commit

Permalink
Merge pull request #345 from wdpypere/logger_aff
Browse files Browse the repository at this point in the history
cleanup affinity and fancylogger
  • Loading branch information
alvarosimon authored Nov 10, 2023
2 parents 3950b25 + 932803e commit 1b56df4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
24 changes: 12 additions & 12 deletions lib/vsc/utils/affinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class cpu_set_t(ctypes.Structure):
_fields_ = [('__bits', cpu_mask_t * NMASKBITS)]

def __init__(self, *args, **kwargs):
super(cpu_set_t, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self.cpus = None

def __str__(self):
Expand All @@ -126,13 +126,13 @@ def convert_hr_bits(self, txt):
# sanity check
if indices[1] < indices[0]:
logging.error("convert_hr_bits: end is lower then start in '%s'", rng)
raise Exception("convert_hr_bits: end is lower then start")
raise ValueError("convert_hr_bits: end is lower then start")
elif indices[0] < 0:
logging.error("convert_hr_bits: negative start in '%s'", rng)
raise Exception("convert_hr_bits: negative start")
raise ValueError("convert_hr_bits: negative start")
elif indices[1] > CPU_SETSIZE + 1: # also covers start, since end > start
logging.error("convert_hr_bits: end larger then max %s in '%s'", CPU_SETSIZE, rng)
raise Exception("convert_hr_bits: end larger then max")
raise ValueError("convert_hr_bits: end larger then max")

self.cpus[indices[0]:indices[1] + 1] = [1] * (indices[1] + 1 - indices[0])
logging.debug("convert_hr_bits: converted %s into cpus %s", txt, self.cpus)
Expand All @@ -146,10 +146,10 @@ def convert_bits_hr(self):
parsed_idx = []
for idx in cpus_index:
if prev + 1 < idx:
parsed_idx.append("%s" % idx)
parsed_idx.append(f"{idx}")
else:
first_idx = parsed_idx[-1].split("-")[0]
parsed_idx[-1] = "%s-%s" % (first_idx, idx)
parsed_idx[-1] = f"{first_idx}-{idx}"
prev = idx
return ",".join(parsed_idx)

Expand Down Expand Up @@ -193,13 +193,13 @@ def set_bits(self, cpus=None):
# get_cpus() rescans
logging.error("set_bits: something went wrong: previous cpus %s; current ones %s",
prev_cpus[:20], self.cpus[:20])
raise Exception("set_bits: something went wrong: previous cpus / current ones")
raise ValueError("set_bits: something went wrong: previous cpus / current ones")

def str_cpus(self):
"""Return a string representation of the cpus"""
if self.cpus is None:
self.get_cpus()
return "".join(["%d" % x for x in self.cpus])
return "".join([f"{int(x)}" for x in self.cpus])


# /* Get the CPU affinity for a task */
Expand Down Expand Up @@ -292,7 +292,7 @@ def getpriority(which=None, who=None):
which = PRIO_PROCESS
elif which not in (PRIO_PROCESS, PRIO_PGRP, PRIO_USER,):
logging.error("getpriority: which %s not in correct range", which)
raise Exception("getpriority: which not in correct range")
raise ValueError("getpriority: which not in correct range")
if who is None:
who = 0 # current which-ever
prio = _libc.getpriority(priority_which_t(which),
Expand All @@ -318,19 +318,19 @@ def setpriority(prio, which=None, who=None):
which = PRIO_PROCESS
elif which not in (PRIO_PROCESS, PRIO_PGRP, PRIO_USER,):
logging.error("setpriority: which %s not in correct range", which)
raise Exception("setpriority: which not in correct range")
raise ValueError("setpriority: which not in correct range")
if who is None:
who = 0 # current which-ever

try:
prio = int(prio)
except ValueError:
logging.error("setpriority: failed to convert priority %s into int", prio)
raise Exception("setpriority: failed to convert priority into int")
raise ValueError("setpriority: failed to convert priority into int")

if prio < PRIO_MIN or prio > PRIO_MAX:
logging.error("setpriority: prio not in allowed range MIN %s MAX %s", PRIO_MIN, PRIO_MAX)
raise Exception("setpriority: prio not in allowed range MIN MAX")
raise ValueError("setpriority: prio not in allowed range MIN MAX")

ec = _libc.setpriority(priority_which_t(which),
id_t(who),
Expand Down
34 changes: 17 additions & 17 deletions lib/vsc/utils/fancylogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ def _env_to_boolean(varname, default=False):
# logging.addLevelName is not a real replacement (it overwrites existing level names)
levelnames = logging._nameToLevel

for key in LOG_LEVEL_ALIASES:
levelnames[key] = LOG_LEVEL_ALIASES[key]
for key, name in LOG_LEVEL_ALIASES.items():
levelnames[key] = name


# mpi rank support
Expand All @@ -212,11 +212,11 @@ class MissingLevelName(KeyError):
def getLevelInt(level_name):
"""Given a level name, return the int value"""
if not isinstance(level_name, str):
raise TypeError('Provided name %s is not a string (type %s)' % (level_name, type(level_name)))
raise TypeError(f'Provided name {level_name} is not a string (type {type(level_name)})')

level = logging.getLevelName(level_name)
if not isinstance(level, int):
raise MissingLevelName('Unknown loglevel name %s' % level_name)
raise MissingLevelName(f'Unknown loglevel name {level_name}')

return level

Expand Down Expand Up @@ -316,8 +316,8 @@ def raiseException(self, message, exception=None, catch=False):
# extend the message with the traceback and some more details
# or use self.exception() instead of self.warning()?
tb_text = "\n".join(traceback.format_tb(tb))
message += " (%s)" % detail
fullmessage += " (%s\n%s)" % (detail, tb_text)
message += f" ({detail})"
fullmessage += f" ({detail}\n{tb_text})"

if exception is None:
exception = self.RAISE_EXCEPTION_CLASS
Expand Down Expand Up @@ -346,9 +346,9 @@ def deprecated(self, msg, cur_ver, max_ver, depth=2, exception=None, log_callbac
loose_mv.version = loose_mv.version[:depth]

if loose_cv >= loose_mv:
self.raiseException("DEPRECATED (since v%s) functionality used: %s" % (max_ver, msg), exception=exception)
self.raiseException(f"DEPRECATED (since v{max_ver}) functionality used: {msg}", exception=exception)
else:
deprecation_msg = "Deprecated functionality, will no longer work in v%s: %s" % (max_ver, msg)
deprecation_msg = f"Deprecated functionality, will no longer work in v{max_ver}: {msg}"
log_callback(deprecation_msg)

def _handleFunction(self, function, levelno, **kwargs):
Expand Down Expand Up @@ -423,7 +423,7 @@ def info(x):
def get_parent_info(self, prefix, verbose=True):
"""Return pretty text version"""
rev_parent_info = self._get_parent_info(verbose=verbose)
return ["%s %s%s" % (prefix, " " * 4 * idx, info) for idx, info in enumerate(rev_parent_info)]
return [f"{prefix} {' ' * 4 * idx}{info}" for idx, info in enumerate(rev_parent_info)]

def __copy__(self):
"""Return shallow copy, in this case reference to current logger"""
Expand All @@ -438,7 +438,7 @@ def thread_name():
"""
returns the current threads name
"""
return threading.currentThread().getName()
return threading.current_thread().name


def getLogger(name=None, fname=False, clsname=False, fancyrecord=None):
Expand Down Expand Up @@ -479,8 +479,8 @@ def getLogger(name=None, fname=False, clsname=False, fancyrecord=None):
l.fancyrecord = fancyrecord
if _env_to_boolean('FANCYLOGGER_GETLOGGER_DEBUG'):
print('FANCYLOGGER_GETLOGGER_DEBUG')
print('name %s fname %s fullname %s' % (name, fname, fullname))
print("getRootLoggerName: %s" % getRootLoggerName())
print(f'name {name} fname {fname} fullname {fullname}')
print(f"getRootLoggerName: {getRootLoggerName()}")
if hasattr(l, 'get_parent_info'):
print('parent_info verbose')
print("\n".join(l.get_parent_info("FANCYLOGGER_GETLOGGER_DEBUG")))
Expand Down Expand Up @@ -553,7 +553,7 @@ def logToScreen(enable=True, handler=None, name=None, stdout=False, colorize=Non

return _logToSomething(FancyStreamHandler,
handleropts,
loggeroption='logtoscreen_stdout_%s' % str(stdout),
loggeroption=f'logtoscreen_stdout_{str(stdout)}',
name=name,
enable=enable,
handler=handler,
Expand Down Expand Up @@ -586,12 +586,12 @@ def logToFile(filename, enable=True, filehandler=None, name=None, max_bytes=MAX_
os.makedirs(directory)
except Exception as ex:
exc, detail, tb = sys.exc_info()
raise(exc("Cannot create logdirectory %s: %s \n detail: %s" % (directory, ex, detail))).with_traceback(tb)
raise(exc(f"Cannot create logdirectory {directory}: {ex} \n detail: {detail}")).with_traceback(tb)

return _logToSomething(
logging.handlers.RotatingFileHandler,
handleropts,
loggeroption='logtofile_%s' % filename,
loggeroption=f'logtofile_{filename}',
name=name,
enable=enable,
handler=filehandler,
Expand All @@ -611,7 +611,7 @@ def logToUDP(hostname, port=5005, enable=True, datagramhandler=None, name=None):
handleropts = {'hostname': hostname, 'port': port}
return _logToSomething(logging.handlers.DatagramHandler,
handleropts,
loggeroption='logtoudp_%s:%s' % (hostname, str(port)),
loggeroption=f'logtoudp_{hostname}:{str(port)}',
name=name,
enable=enable,
handler=datagramhandler,
Expand Down Expand Up @@ -702,7 +702,7 @@ def _getSysLogFacility(name=None):
name = 'user'

facility = getattr(logging.handlers.SysLogHandler,
"LOG_%s" % name.upper(), logging.handlers.SysLogHandler.LOG_USER)
f"LOG_{name.upper()}", logging.handlers.SysLogHandler.LOG_USER)

return facility

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from vsc.install.shared_setup import ag, kh, jt, sdw

PACKAGE = {
'version': '3.5.6',
'version': '3.5.7',
'author': [sdw, jt, ag, kh],
'maintainer': [sdw, jt, ag, kh],
'install_requires': [
Expand Down

0 comments on commit 1b56df4

Please sign in to comment.