OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 # | |
5 # -*- test-case-name: twisted.test.test_journal -*- | |
6 | |
7 """Logging that uses pickles. | |
8 | |
9 TODO: add log that logs to a file. | |
10 """ | |
11 | |
12 # twisted imports | |
13 from twisted.persisted import dirdbm | |
14 from twisted.internet import defer | |
15 from zope.interface import implements | |
16 | |
17 # sibling imports | |
18 import base | |
19 | |
20 | |
21 class DirDBMLog: | |
22 """Log pickles to DirDBM directory.""" | |
23 | |
24 implements(base.ICommandLog) | |
25 | |
26 def __init__(self, logPath): | |
27 self.db = dirdbm.Shelf(logPath) | |
28 indexs = map(int, self.db.keys()) | |
29 if indexs: | |
30 self.currentIndex = max(indexs) | |
31 else: | |
32 self.currentIndex = 0 | |
33 | |
34 def logCommand(self, command, time): | |
35 """Log a command.""" | |
36 self.currentIndex += 1 | |
37 self.db[str(self.currentIndex)] = (time, command) | |
38 return defer.succeed(1) | |
39 | |
40 def getCurrentIndex(self): | |
41 """Return index of last command logged.""" | |
42 return self.currentIndex | |
43 | |
44 def getCommandsSince(self, index): | |
45 result = [] | |
46 for i in range(index, self.currentIndex + 1): | |
47 result.append(self.db[str(i)]) | |
48 return result | |
OLD | NEW |