log.py 4.3 KB
Newer Older
Sam Moore's avatar
Sam Moore committed
1
log_files = []
Sam Moore's avatar
Sam Moore committed
2 3 4 5
import datetime
import urllib2

class LogFile():
6
	def __init__(self, log):	
Sam Moore's avatar
Sam Moore committed
7
		
8 9
		self.log = log
		self.logged = []
Sam Moore's avatar
Sam Moore committed
10
		self.log.write("# Log starts " + str(datetime.datetime.now()) + "\n")
Sam Moore's avatar
Sam Moore committed
11 12

	def write(self, s):
13 14 15
		now = datetime.datetime.now()
		self.log.write(str(now) + " : " + s + "\n")
		self.logged.append((now, s))
Sam Moore's avatar
Sam Moore committed
16 17

	def setup(self, board, players):
Sam Moore's avatar
Sam Moore committed
18
		
Sam Moore's avatar
Sam Moore committed
19
		for p in players:
Sam Moore's avatar
Sam Moore committed
20
			self.log.write("# " + str(p.colour) + " : " + str(p.name) + "\n")
Sam Moore's avatar
Sam Moore committed
21 22 23 24 25 26 27 28 29
		
		self.log.write("# Initial board\n")
		for x in range(0, w):
			for y in range(0, h):
				if board.grid[x][y] != None:
					self.log.write(str(board.grid[x][y]) + "\n")

		self.log.write("# Start game\n")

30 31
	def close(self):
		self.log.write("# EOF\n")
Sam Moore's avatar
Sam Moore committed
32 33
		if self.log != sys.stdout:
			self.log.close()
34

Sam Moore's avatar
Sam Moore committed
35
class ShortLog(LogFile):
Sam Moore's avatar
Sam Moore committed
36
	def __init__(self, file_name):
Sam Moore's avatar
Sam Moore committed
37 38 39 40 41
		if file_name == "":
			self.log = sys.stdout
		else:
			self.log = open(file_name, "w", 0)
		LogFile.__init__(self, self.log)
Sam Moore's avatar
Sam Moore committed
42
		self.file_name = file_name
43
		self.phase = 0
Sam Moore's avatar
Sam Moore committed
44

45 46 47 48 49
	def write(self, s):
		now = datetime.datetime.now()
		self.logged.append((now, s))
		
		if self.phase == 0:
Sam Moore's avatar
Sam Moore committed
50 51 52
			if self.log != sys.stdout:
				self.log.close()
				self.log = open(self.file_name, "w", 0)
Sam Moore's avatar
Sam Moore committed
53
			self.log.write("# Short log updated " + str(datetime.datetime.now()) + "\n")	
54 55 56 57 58
			LogFile.setup(self, game.board, game.players)

		elif self.phase == 1:
			for message in self.logged[len(self.logged)-2:]:
				self.log.write(str(message[0]) + " : " + message[1] + "\n")
Sam Moore's avatar
Sam Moore committed
59

60 61 62
		self.phase = (self.phase + 1) % 2		
		
	def close(self):
Sam Moore's avatar
Sam Moore committed
63 64 65
		if self.phase == 1:
			ending = self.logged[len(self.logged)-1]
			self.log.write(str(ending[0]) + " : " + ending[1] + "\n")
66
		self.log.write("# EOF\n")
Sam Moore's avatar
Sam Moore committed
67 68
		if self.log != sys.stdout:
			self.log.close()
Sam Moore's avatar
Sam Moore committed
69 70 71 72 73
		

class HeadRequest(urllib2.Request):
	def get_method(self):
		return "HEAD"
Sam Moore's avatar
Sam Moore committed
74 75

class HttpGetter(StoppableThread):
Sam Moore's avatar
Sam Moore committed
76
	def __init__(self, address):
Sam Moore's avatar
Sam Moore committed
77
		StoppableThread.__init__(self)
Sam Moore's avatar
Sam Moore committed
78
		self.address = address
Sam Moore's avatar
Sam Moore committed
79 80 81 82
		self.log = urllib2.urlopen(address)
		self.lines = []
		self.lock = threading.RLock() #lock for access of self.state
		self.cond = threading.Condition() # conditional
Sam Moore's avatar
Sam Moore committed
83

Sam Moore's avatar
Sam Moore committed
84 85 86 87 88 89 90
	def run(self):
		while not self.stopped():
			line = self.log.readline()
			if line == "":
				date_mod = datetime.datetime.strptime(self.log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
				self.log.close()
	
Sam Moore's avatar
Sam Moore committed
91 92
				next_log = urllib2.urlopen(HeadRequest(self.address))
				date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
Sam Moore's avatar
Sam Moore committed
93 94 95 96 97
				while date_new <= date_mod and not self.stopped():
					next_log = urllib2.urlopen(HeadRequest(self.address))
					date_new = datetime.datetime.strptime(next_log.headers['last-modified'], "%a, %d %b %Y %H:%M:%S GMT")
				if self.stopped():
					break
Sam Moore's avatar
Sam Moore committed
98

Sam Moore's avatar
Sam Moore committed
99 100 101 102 103 104 105
				self.log = urllib2.urlopen(self.address)
				line = self.log.readline()

			self.cond.acquire()
			self.lines.append(line)
			self.cond.notifyAll()
			self.cond.release()
Sam Moore's avatar
Sam Moore committed
106

Sam Moore's avatar
Sam Moore committed
107
			#sys.stderr.write(" HttpGetter got \'" + str(line) + "\'\n")
Sam Moore's avatar
Sam Moore committed
108

Sam Moore's avatar
Sam Moore committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		self.log.close()
				
				
	
		
		
class HttpReplay():
	def __init__(self, address):
		self.getter = HttpGetter(address)
		self.getter.start()
		
	def readline(self):
		self.getter.cond.acquire()
		while len(self.getter.lines) == 0:
			self.getter.cond.wait()
			
		result = self.getter.lines[0]
		self.getter.lines = self.getter.lines[1:]
		self.getter.cond.release()

		return result
			
Sam Moore's avatar
Sam Moore committed
131
			
132
	def close(self):
Sam Moore's avatar
Sam Moore committed
133
		self.getter.stop()
Sam Moore's avatar
Sam Moore committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

class FileReplay():
	def __init__(self, filename):
		self.f = open(filename, "r", 0)
		self.filename = filename
		self.mod = os.path.getmtime(filename)
		self.count = 0
	
	def readline(self):
		line = self.f.readline()
		
		while line == "":
			mod2 = os.path.getmtime(self.filename)
			if mod2 > self.mod:
				#sys.stderr.write("File changed!\n")
				self.mod = mod2
				self.f.close()
				self.f = open(self.filename, "r", 0)
				
				new_line = self.f.readline()
				
				if " ".join(new_line.split(" ")[0:3]) != "# Short log":
					for i in range(self.count):
						new_line = self.f.readline()
						#sys.stderr.write("Read back " + str(i) + ": " + str(new_line) + "\n")
					new_line = self.f.readline()
				else:
					self.count = 0
				
				line = new_line

		self.count += 1
		return line

	def close(self):
		self.f.close()
		
Sam Moore's avatar
Sam Moore committed
171 172
						
def log(s):
Sam Moore's avatar
Sam Moore committed
173 174
	for l in log_files:
		l.write(s)
Sam Moore's avatar
Sam Moore committed
175 176 177
		

def log_init(board, players):
Sam Moore's avatar
Sam Moore committed
178 179
	for l in log_files:
		l.setup(board, players)
Sam Moore's avatar
Sam Moore committed
180