##// END OF EJS Templates
Implement optional logging of output as suggested by Robert Kern.
Thomas Kluyver -
Show More
@@ -49,11 +49,14 b' class HistoryManager(object):'
49 db = None
49 db = None
50 # The number of the current session in the history database
50 # The number of the current session in the history database
51 session_number = None
51 session_number = None
52 # Number of lines to cache before writing to the database (to save power)
52 # Should we log output to the database? (default no)
53 # - if 0, lines will be written instantly.
53 db_log_output = False
54 # Write to database every x commands (higher values save disk access & power)
55 # Values of 1 or less effectively disable caching.
54 db_cache_size = 0
56 db_cache_size = 0
55 # The line cache
57 # The input and output caches
56 db_cache = None
58 db_input_cache = None
59 db_output_cache = None
57
60
58 # Private interface
61 # Private interface
59 # Variables used to store the three last inputs from the user. On each new
62 # Variables used to store the three last inputs from the user. On each new
@@ -112,8 +115,13 b' class HistoryManager(object):'
112
115
113 def init_db(self):
116 def init_db(self):
114 self.db = sqlite3.connect(self.hist_file)
117 self.db = sqlite3.connect(self.hist_file)
115 self.db.execute("""CREATE TABLE IF NOT EXISTS history (session integer,
118 self.db.execute("""CREATE TABLE IF NOT EXISTS history
116 line integer, source text, source_raw text,
119 (session integer, line integer, source text, source_raw text,
120 PRIMARY KEY (session, line))""")
121 # Output history is optional, but ensure the table's there so it can be
122 # enabled later.
123 self.db.execute("""CREATE TABLE IF NOT EXISTS output_history
124 (session integer, line integer, output text,
117 PRIMARY KEY (session, line))""")
125 PRIMARY KEY (session, line))""")
118 cur = self.db.execute("""SELECT name FROM sqlite_master WHERE
126 cur = self.db.execute("""SELECT name FROM sqlite_master WHERE
119 type='table' AND name='singletons'""")
127 type='table' AND name='singletons'""")
@@ -131,7 +139,8 b' class HistoryManager(object):'
131 self.db.execute("""UPDATE singletons SET value=? WHERE
139 self.db.execute("""UPDATE singletons SET value=? WHERE
132 name='session_number'""", (self.session_number+1,))
140 name='session_number'""", (self.session_number+1,))
133 self.db.commit()
141 self.db.commit()
134 self.db_cache = []
142 self.db_input_cache = []
143 self.db_output_cache = []
135
144
136 def get_db_history(self, session, start=1, stop=None, raw=True):
145 def get_db_history(self, session, start=1, stop=None, raw=True):
137 """Retrieve input history from the database by session.
146 """Retrieve input history from the database by session.
@@ -259,14 +268,11 b' class HistoryManager(object):'
259 self.input_hist_parsed.append(source.rstrip())
268 self.input_hist_parsed.append(source.rstrip())
260 self.input_hist_raw.append(source_raw.rstrip())
269 self.input_hist_raw.append(source_raw.rstrip())
261
270
262 db_row = (self.session_number, line_num, source, source_raw)
271 self.db_input_cache.append((self.session_number, line_num,
263 if self.db_cache_size: # Cache before writing
272 source, source_raw))
264 self.db_cache.append(db_row)
273 # Trigger to flush cache and write to DB.
265 if len(self.db_cache) > self.db_cache_size:
274 if len(self.db_input_cache) >= self.db_cache_size:
266 self.writeout_cache()
275 self.writeout_cache()
267 else: # Instant write
268 with self.db:
269 self.db.execute("INSERT INTO history VALUES (?, ?, ?, ?)", db_row)
270
276
271 # update the auto _i variables
277 # update the auto _i variables
272 self._iii = self._ii
278 self._iii = self._ii
@@ -282,11 +288,24 b' class HistoryManager(object):'
282 new_i : self._i00 }
288 new_i : self._i00 }
283 self.shell.user_ns.update(to_main)
289 self.shell.user_ns.update(to_main)
284
290
291 def store_output(self, line_num, output):
292 if not self.db_log_output:
293 return
294 db_row = (self.session_number, line_num, output)
295 if self.db_cache_size > 1:
296 self.db_output_cache.append(db_row)
297 else:
298 with self.db:
299 self.db.execute("INSERT INTO output_history VALUES (?,?,?)", db_row)
300
285 def writeout_cache(self):
301 def writeout_cache(self):
286 with self.db:
302 with self.db:
287 self.db.executemany("INSERT INTO history VALUES (?, ?, ?, ?)",
303 self.db.executemany("INSERT INTO history VALUES (?, ?, ?, ?)",
288 self.db_cache)
304 self.db_input_cache)
289 self.db_cache = []
305 self.db.executemany("INSERT INTO output_history VALUES (?, ?, ?)",
306 self.db_output_cache)
307 self.db_input_cache = []
308 self.db_output_cache = []
290
309
291 def sync_inputs(self):
310 def sync_inputs(self):
292 """Ensure raw and translated histories have same length."""
311 """Ensure raw and translated histories have same length."""
General Comments 0
You need to be logged in to leave comments. Login now