Bugfix: Import traceback for SQL sharelogger
[bitcoin:eloipool.git] / sharelogging / logfile.py
1 # Eloipool - Python Bitcoin pool server
2 # Copyright (C) 2011-2012  Luke Dashjr <luke-jr+eloipool@utopios.org>
3 # Copyright (C) 2012  Peter Leurs <kinlo@triplemining.com>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License as
7 # published by the Free Software Foundation, either version 3 of the
8 # License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU Affero General Public License for more details.
14 #
15 # You should have received a copy of the GNU Affero General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
19
20 from collections import deque
21 from datetime import date
22 from time import sleep, time
23 import threading
24 from util import shareLogFormatter
25 import logging
26 import traceback
27
28 _logger = logging.getLogger('sharelogging.logfile')
29
30 class logfile(threading.Thread):
31         def __init__(self, filename, **ka):
32                 super().__init__(**ka.get('thropts', {}))
33                 self.fn=filename
34                 if 'format' not in ka:
35                         _logger.warn('"format" not specified for logfile logger, but default may vary!')
36                         ka['format'] = "{time} {Q(remoteHost)} {username} {YN(not(rejectReason))} {dash(YN(upstreamResult))} {dash(rejectReason)} {solution} {target2pdiff(target)}\n"
37                 self.fmt = shareLogFormatter(ka['format'], '%s')
38                 self.queue = deque()
39                 self.start()
40         
41         def queueshare(self, line):
42                 self.queue.append(line)
43         
44         def flushlog(self):
45                 if len(self.queue) > 0:
46                         with open(self.fn, "a") as logfile:
47                                 while len(self.queue)>0:
48                                         logfile.write(self.queue.popleft())
49         
50         def run(self):
51                 while True:
52                         try:
53                                 sleep(0.2)
54                                 self.flushlog()
55                         except:
56                                 _logger.critical(traceback.format_exc())
57         
58         def logShare(self, share):
59                 logline = self.fmt.formatShare(share)
60                 self.queueshare(logline)