From 2655029d5ede9c5f01a8c07e98bb3856ddb0ee55 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 18 Nov 2012 23:29:04 -0500 Subject: [PATCH 1/3] Make NcursesLogger only show last set of lines You would think ncurses would already do this for the infobox, but apparently this is not true. You would also think we wouldn't have to write any more word-wrap code after 1980, but apparently this too is not true. --- trustify/client/logger.py | 49 ++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/trustify/client/logger.py b/trustify/client/logger.py index 76dcf2752..a989776b2 100644 --- a/trustify/client/logger.py +++ b/trustify/client/logger.py @@ -51,19 +51,49 @@ class FileLogger(Logger): import dialog class NcursesLogger(Logger): - def __init__(self, firstmessage="", height=18, width=70): - self.content = firstmessage + def __init__(self, firstmessage="", height=16, width=66): + self.lines = [] + self.all_content = "" self.d = dialog.Dialog() self.height = height self.width = width - self.show() + self.add(firstmessage) + ''' + Only show the last (self.height) lines; + note that lines can wrap at self.width, so + a single line could actually be multiple lines + ''' def add(self, s): - self.content += s + self.all_content += s + + for line in s.splitlines(): + # check for lines that would wrap + cur_out = line + while len(cur_out) > self.width: + + # find first space before self.width chars into cur_out + last_space_pos = cur_out.rfind(' ', 0, self.width) + + if (last_space_pos == -1): + # no spaces, just cut them off at whatever + self.lines.append(cur_out[0:self.width]) + cur_out = cur_out[self.width:] + else: + # cut off at last space + self.lines.append(cur_out[0:last_space_pos]) + cur_out = cur_out[last_space_pos+1:] + if cur_out != '': + self.lines.append(cur_out) + + + # show last 16 lines + self.content = '\n'.join(self.lines[-self.height:]) self.show() def show(self): - self.d.infobox(self.content, self.height, self.width) + # add the padding around the box + self.d.infobox(self.content, self.height+2, self.width+4) def log(self, level, data): self.add(data + "\n") @@ -117,9 +147,16 @@ if __name__ == "__main__": logger.setLogLevel(logger.TRACE) # Log a message: - logger.log(logger.INFO, "logger!") + #logger.log(logger.INFO, "logger!") time.sleep(0.01) + logger.info("This is a long line, it's pretty long, butitalso hasbig wordsthat areprobably hardtobreak oninan easywayforthe ncurseslib, sowhatdoes itdo then?") + logger.info("aa " + "a"*70 + "B") + + for i in range(20): + logger.info("iteration #%d/20" % i) + time.sleep(0.3) + # Alternatively, use logger.error("errrrr") From ef8d3b661e905fe12a5677e79123455c495d8c17 Mon Sep 17 00:00:00 2001 From: Seth Schoen Date: Sun, 18 Nov 2012 20:31:58 -0800 Subject: [PATCH 2/3] add logging function --- server-ca/daemon_common.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/server-ca/daemon_common.py b/server-ca/daemon_common.py index 42c1f8e1d..80a0cb146 100644 --- a/server-ca/daemon_common.py +++ b/server-ca/daemon_common.py @@ -2,9 +2,6 @@ # functions common to the various kinds of daemon -# TODO: define a log function that sends a pubsub message to the -# logger daemon - import time, binascii from Crypto import Random @@ -27,3 +24,9 @@ def random(): def random_raw(): """Return 32 random bytes.""" return Random.get_random_bytes(32) + +def log(msg, session = None): + if session: + r.publish("logs", "%s: %s" % (short(session), msg)) + else: + r.publish("logs", "%s" % session) From 8da998b4aeaec23d5572ebdafd108c282b8a5ced Mon Sep 17 00:00:00 2001 From: Seth Schoen Date: Sun, 18 Nov 2012 20:34:19 -0800 Subject: [PATCH 3/3] sample use of logger :-) --- server-ca/issue-daemon.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/server-ca/issue-daemon.py b/server-ca/issue-daemon.py index c94b90988..36dbb8738 100755 --- a/server-ca/issue-daemon.py +++ b/server-ca/issue-daemon.py @@ -42,22 +42,23 @@ def issue(session): # session nonetheless died for some reason unrelated to failing # challenges before the cert could be issued. Normally, this # should never happen. - if debug: print "removing expired (issue-state!?) session", short(session) + log("removing expired (issue-state!?) session", session) r.lrem("pending-requests", session) return if r.hget(session, "state") != "issue": return csr = r.hget(session, "csr") names = r.lrange("%s:names" % session, 0, -1) + log("attempting to issue certificate for names: %s" % join(names), session) with issue_lock: cert = CSR.issue(csr, names) r.hset(session, "cert", cert) if cert: # once issuing cert succeeded - if debug: print "%s: issued certificate for names: %s" % (short(session), ", ".join(names)) + log("issued certificate for names: %s" % join(names), session) r.hset(session, "state", "done") # r.lpush("pending-done", session) else: # should not be reached in deployed version - if debug: print "issuing for", short(session), "failed" + log("issuing cert failed!?", session) r.lpush("pending-issue", session) while True: