Merge branch 'bugfix_YN_None_2' into bugfix_YN_None_3
[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 YN
25 import logging
26 import traceback
27
28 _logger = logging.getLogger('logshares_files')
29
30 class logfile(threading.Thread):
31         def __init__(self, filename, *a, **k):
32                 super().__init__(*a, **k)
33                 self.fn=filename
34                 self.queue = deque()
35                 self.start()
36         
37         def queueshare(self, line):
38                 self.queue.append(line)
39         
40         def flushlog(self):
41                 if len(self.queue) > 0:
42                         with open(self.fn, "a") as logfile:
43                                 while len(self.queue)>0:
44                                         logfile.write(self.queue.popleft())
45         
46         def run(self):
47                 while True:
48                         try:
49                                 sleep(0.2)
50                                 self.flushlog()
51                         except:
52                                 _logger.critical(traceback.format_exc())
53         
54         def logShare(self, share):
55                 timestamp = time()
56                 address = share.get('remoteHost','?')
57                 username = share['username']
58                 ourresult = YN(not share.get('rejectReason', None))
59                 upstreamresult = YN(share.get('upstreamResult', None)) or '-'
60                 reason = share.get('rejectReason','-')
61                 solution = share['solution']
62                 
63                 logline = "{} {} {} {} {} {} {}\n".format(timestamp, address, username, ourresult, upstreamresult, reason, solution)
64                 self.queueshare(logline)