forked from riptano/ComboAMI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
executable file
·89 lines (68 loc) · 3.24 KB
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
### Script provided by DataStax.
import urllib2
import os
import re
import shlex
import subprocess
import sys
import time
from exceptions import SystemExit
configfile = '/home/ubuntu/datastax_ami/ami.log'
def appendLog(text):
with open(configfile, "a") as f:
f.write(text + "\n")
print text
def exe(command, log=True, expectError=False):
# Helper function to execute commands and print traces of the command and output for debugging/logging purposes
process = subprocess.Popen(shlex.split(command), stderr=subprocess.PIPE, stdout=subprocess.PIPE)
read = process.communicate()
if log:
# Print output on next line if it exists
if len(read[0]) > 0:
appendLog('[EXEC] ' + time.strftime("%m/%d/%y-%H:%M:%S", time.localtime()) + ' ' + command + ":\n" + read[0])
elif len(read[1]) > 0:
if expectError:
appendLog('[EXEC:E] ' + time.strftime("%m/%d/%y-%H:%M:%S", time.localtime()) + ' ' + command + ":\n" + read[1])
else:
appendLog('[ERROR] ' + time.strftime("%m/%d/%y-%H:%M:%S", time.localtime()) + ' ' + command + ":\n" + read[1])
if not log or (len(read[0]) == 0 and len(read[1]) == 0):
appendLog('[EXEC] ' + time.strftime("%m/%d/%y-%H:%M:%S", time.localtime()) + ' ' + command)
return read
def pipe(command1, command2, log=True):
# Helper function to execute piping commands and print traces of the commands and output for debugging/logging purposes
p1 = subprocess.Popen(shlex.split(command1), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p2 = subprocess.Popen(shlex.split(command2), stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
read = p2.stdout.read()
if not log:
read = ""
# Print output on next line if it exists
if len(read) > 0:
appendLog('[PIPE] ' + time.strftime("%m/%d/%y-%H:%M:%S", time.localtime()) + ' ' + command1 + ' | ' + command2 + ":\n" + read)
else:
appendLog('[PIPE] ' + time.strftime("%m/%d/%y-%H:%M:%S", time.localtime()) + ' ' + command1 + ' | ' + command2)
output = p2.communicate()[0]
if log:
if read and len(read) > 0:
appendLog('[PIPE] ' + time.strftime("%m/%d/%y-%H:%M:%S", time.localtime()) + ' ' + command1 + ' | ' + command2 + ":\n" + read)
if output and len(output[0]) > 0:
appendLog('[PIPE] ' + time.strftime("%m/%d/%y-%H:%M:%S", time.localtime()) + ' ' + command1 + ' | ' + command2 + ":\n" + output[0])
if output and len(output[1] > 0):
appendLog('[PIPE] ' + time.strftime("%m/%d/%y-%H:%M:%S", time.localtime()) + ' ' + command1 + ' | ' + command2 + ":\n" + output[1])
return output
def debug(infotext):
appendLog('[DEBUG] ' + str(infotext))
def info(infotext):
appendLog('[INFO] ' + str(infotext))
def warn(infotext):
appendLog('[WARN] ' + str(infotext))
def error(infotext):
appendLog('[ERROR] ' + str(infotext))
def exception(filename):
if type(sys.exc_info()[1]) == SystemExit:
return
appendLog("[ERROR] Exception seen in %s:" % filename)
import traceback
appendLog(traceback.format_exc())
sys.exit(1)