reimplemented logging based on log_path var

pull/11722/head
Brian Coca 9 years ago
parent ff49c5adac
commit 0089eb8102

@ -25,6 +25,8 @@ import random
import subprocess import subprocess
import sys import sys
import time import time
import logging
import getpass
from multiprocessing import Lock from multiprocessing import Lock
from ansible import constants as C from ansible import constants as C
@ -32,6 +34,19 @@ from ansible.errors import AnsibleError
from ansible.utils.color import stringc from ansible.utils.color import stringc
from ansible.utils.unicode import to_bytes from ansible.utils.unicode import to_bytes
if C.DEFAULT_LOG_PATH:
path = C.DEFAULT_LOG_PATH
if (os.path.exists(path) and not os.access(path, os.W_OK)) and not os.access(os.path.dirname(path), os.W_OK):
self._display.warning("log file at %s is not writeable, aborting\n" % path)
logging.basicConfig(filename=path, level=logging.DEBUG, format='%(asctime)s %(name)s %(message)s')
mypid = str(os.getpid())
user = getpass.getuser()
logger = logging.getLogger("p=%s u=%s | " % (mypid, user))
else:
logger = None
class Display: class Display:
def __init__(self, verbosity=0): def __init__(self, verbosity=0):
@ -48,6 +63,7 @@ class Display:
self.set_cowsay_info() self.set_cowsay_info()
#self.debug_lock = Lock() #self.debug_lock = Lock()
def set_cowsay_info(self): def set_cowsay_info(self):
if not C.ANSIBLE_NOCOWS: if not C.ANSIBLE_NOCOWS:
@ -61,34 +77,37 @@ class Display:
elif os.path.exists("/opt/local/bin/cowsay"): elif os.path.exists("/opt/local/bin/cowsay"):
# MacPorts path for cowsay # MacPorts path for cowsay
self.cowsay = "/opt/local/bin/cowsay" self.cowsay = "/opt/local/bin/cowsay"
if self.cowsay and self.noncow == 'random': if self.cowsay and self.noncow == 'random':
cmd = subprocess.Popen([self.cowsay, "-l"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) cmd = subprocess.Popen([self.cowsay, "-l"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, err) = cmd.communicate() (out, err) = cmd.communicate()
cows = out.split() cows = out.split()
cows.append(False) cows.append(False)
self.noncow = random.choice(cows) self.noncow = random.choice(cows)
def display(self, msg, color=None, stderr=False, screen_only=False, log_only=False): def display(self, msg, color=None, stderr=False, screen_only=False, log_only=False):
# FIXME: this needs to be implemented
#msg = utils.sanitize_output(msg)
msg2 = msg msg2 = msg
if color: if color:
msg2 = stringc(msg, color) msg2 = stringc(msg, color)
if not log_only: if not log_only:
b_msg2 = to_bytes(msg2) b_msg2 = to_bytes(msg2)
if not stderr: if not stderr:
print(b_msg2) print(b_msg2)
else: else:
print(b_msg2, file=sys.stderr) print(b_msg2, file=sys.stderr)
if C.DEFAULT_LOG_PATH != '':
if logger and not screen_only:
while msg.startswith("\n"): while msg.startswith("\n"):
msg = msg.replace("\n","") msg = msg.replace("\n","")
b_msg = to_bytes(msg) b_msg = to_bytes(msg)
# FIXME: logger stuff needs to be implemented if color == 'red':
#if not screen_only: logger.error(b_msg)
# if color == 'red': else:
# logger.error(b_msg) logger.info(b_msg)
# else:
# logger.info(b_msg)
def vv(self, msg, host=None): def vv(self, msg, host=None):
return self.verbose(msg, host=host, caplevel=1) return self.verbose(msg, host=host, caplevel=1)
@ -166,9 +185,9 @@ class Display:
self.banner_cowsay(msg) self.banner_cowsay(msg)
return return
except OSError: except OSError:
# somebody cleverly deleted cowsay or something during the PB run. heh. self.warning("somebody cleverly deleted cowsay or something during the PB run. heh.")
pass
#FIXME: make this dynamic on tty size (look and ansible-doc)
msg = msg.strip() msg = msg.strip()
star_len = (80 - len(msg)) star_len = (80 - len(msg))
if star_len < 0: if star_len < 0:

Loading…
Cancel
Save