import repository from arizona
[raven.git] / apps / gacks / gackslog.py
1 import sha
2 import os
3 import time
4 import traceback
5
6 """
7 import gackslog_mysql
8 l = gackslog_mysql.GacksMysqlLogger()\r
9 l.log("foo", {"a": "b"}, "result", [1,2,3])
10 l.log("set_allocator", {"callerGID_str": "callergid", "handle_strs": ["h1", "h2"], "allocatorGID_str": "allocatorgid", "which": 1, "append": 0, "reqhash": "somehash"}, "receipt_list", ["r1","r2"])
11 """
12
13 class GacksLogger:
14     def __init__(self, directory = "./log", name = "gacks.log", enable_stdout=False, readonly=False):
15         self.directory = directory
16         self.name = name
17         self.enable_stdout = enable_stdout
18         self.readonly = readonly
19
20         if not os.path.exists(directory):
21             os.makedirs(self.directory)
22
23         # too much junk in the log due to mod_python reloading the module
24         #self.log("GacksLogger.__init__", {}, "result", None)
25
26     def kind(self, name):
27         if name in ["callerGID_str", "allocatorGID_str"]:
28             return "gid"
29         elif name in ["which", "append"]:
30             return "int"
31         elif name in ["cred_str"]:
32             return "cred"
33         elif name == "receipt_list":
34             return "receipt"
35         elif name == "handle_list":
36             return "handle"
37
38     def is_big(self, kind):
39         if kind in ["gid", "cred", "receipt"]:
40             return True
41         else:
42             return False
43
44     def get_big_id(self, kind, val):
45         return sha.new(str(val)).hexdigest() + "." + kind
46
47     def store_big(self, id, kind, val):
48         pathname = os.path.join(self.directory, id)
49         if not os.path.exists(pathname):
50             f = open(pathname, "w")
51             f.write(str(val))
52             f.close()
53
54     def format_big(self, kind, val):
55         id = self.get_big_id(kind, val)
56         self.store_big(id, kind, val)
57
58         return "'^" + id + "'"
59
60     def format_list(self, kind, val):
61         l = []
62         for item in val:
63             l.append(self.format_item(kind, item))
64
65         return "[" + ",".join(l) + "]"
66
67     def format_int(self, kind, val):
68         return str(val)
69
70     def format_str(self, kind, val):
71         return "'" + str(val) + "'"
72
73     def format_item(self, kind, val):
74         if isinstance(val, list):
75             return self.format_list(kind, val)
76
77         elif self.is_big(kind):
78             return self.format_big(kind, val)
79
80         elif kind == "int":
81             return self.format_int(kind, val)
82
83         else:
84             return self.format_str(kind, val)
85
86     def log(self, func_name, arg_dict, result_name, result_val):
87         if "self" in arg_dict:
88             del arg_dict["self"]
89
90         arg_strs = []
91         for arg_name in arg_dict.keys():
92             arg_val = arg_dict[arg_name]
93             arg_kind = self.kind(arg_name)
94             arg_strs.append(arg_name + "=" + self.format_item(arg_kind, arg_val))
95
96         result_kind = self.kind(result_name)
97
98         self.log_line(time.time(), "call", func_name, arg_strs, self.format_item(result_kind, result_val))
99
100     def log_exception(self, func_name=None):
101         if not func_name:
102             func_name = "unknown_func"
103         self.log_line(time.time(), "exception", func_name, msg=str(traceback.format_exc()))
104
105     def log_msg(self, func_name, msg):
106         if not func_name:
107             func_name = "unknown_func"
108         self.log_line(time.time(), "msg", func_name, msg=msg)
109
110     def log_line(self, timestamp, kind, func_name, argStrs=None, resultStr=None, msg=None):
111         logLine = str(timestamp) + " " + str(kind) + " " + str(func_name)
112
113         if argStrs:
114             logLine = logLine + " (" + ",".join(argStrs) + ")"
115
116         if resultStr:
117             logLine = logLine + " --> " + resultStr
118
119         if msg:
120             logLine = logLine + " : " + msg
121
122         self.write(logLine)
123
124     def write(self, line):
125         if self.readonly:
126             return
127
128         f = open(os.path.join(self.directory, self.name), "a")
129         f.write(line + "\n")
130         f.close()
131         if self.enable_stdout:
132             print line
133
134
135