-# Copyright 2011-2012 GRNET S.A. All rights reserved.
+# Copyright 2013-2014 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
-import logging
-
-
-def get_log_filename(filename=(
- '/var/log/kamaki.log',
- '/var/log/kamaki/clients.log',
- '/tmp/kamaki.log',
- 'kamaki.log')):
- if not (isinstance(filename, list) or isinstance(filename, tuple)):
- filename = (filename,)
- for logfile in filename:
- try:
- with open(logfile) as f:
- f.seek(0)
- except IOError:
- continue
- return logfile
- print('Failed to open any logging locations')
-
-
-def add_file_logger(
- name, caller,
- level=logging.DEBUG, prefix='', filename='/tmp/kamaki.log'):
- try:
- assert caller and filename
- logger = logging.getLogger(name)
- h = logging.FileHandler(filename)
- fmt = logging.Formatter(
- '%(asctime)s ' + caller + ' %(name)s-%(levelname)s: %(message)s')
- h.setFormatter(fmt)
- logger.addHandler(h)
- logger.setLevel(level)
- except Exception:
- pass
-
-
-def get_logger(name):
- return logging.getLogger(name)
-
def _matches(val1, val2, exactMath=True):
"""Case Insensitive match"""
while '//' in r:
r = r.replace('//', '/')
return ('/%s' % r.strip('/')) if r else ''
+
+
+def readall(openfile, size, retries=7):
+ """Read a file until size is reached"""
+ remains = size if size > 0 else 0
+ buf = ''
+ for i in range(retries):
+ tmp_buf = openfile.read(remains)
+ if tmp_buf:
+ buf += tmp_buf
+ remains -= len(tmp_buf)
+ if remains > 0:
+ continue
+ return buf
+ raise IOError('Failed to read %s bytes from file' % size)