##// 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 49 db = None
50 50 # The number of the current session in the history database
51 51 session_number = None
52 # Number of lines to cache before writing to the database (to save power)
53 # - if 0, lines will be written instantly.
52 # Should we log output to the database? (default no)
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 56 db_cache_size = 0
55 # The line cache
56 db_cache = None
57 # The input and output caches
58 db_input_cache = None
59 db_output_cache = None
57 60
58 61 # Private interface
59 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 116 def init_db(self):
114 117 self.db = sqlite3.connect(self.hist_file)
115 self.db.execute("""CREATE TABLE IF NOT EXISTS history (session integer,
116 line integer, source text, source_raw text,
118 self.db.execute("""CREATE TABLE IF NOT EXISTS history
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 125 PRIMARY KEY (session, line))""")
118 126 cur = self.db.execute("""SELECT name FROM sqlite_master WHERE
119 127 type='table' AND name='singletons'""")
@@ -131,7 +139,8 b' class HistoryManager(object):'
131 139 self.db.execute("""UPDATE singletons SET value=? WHERE
132 140 name='session_number'""", (self.session_number+1,))
133 141 self.db.commit()
134 self.db_cache = []
142 self.db_input_cache = []
143 self.db_output_cache = []
135 144
136 145 def get_db_history(self, session, start=1, stop=None, raw=True):
137 146 """Retrieve input history from the database by session.
@@ -259,14 +268,11 b' class HistoryManager(object):'
259 268 self.input_hist_parsed.append(source.rstrip())
260 269 self.input_hist_raw.append(source_raw.rstrip())
261 270
262 db_row = (self.session_number, line_num, source, source_raw)
263 if self.db_cache_size: # Cache before writing
264 self.db_cache.append(db_row)
265 if len(self.db_cache) > self.db_cache_size:
266 self.writeout_cache()
267 else: # Instant write
268 with self.db:
269 self.db.execute("INSERT INTO history VALUES (?, ?, ?, ?)", db_row)
271 self.db_input_cache.append((self.session_number, line_num,
272 source, source_raw))
273 # Trigger to flush cache and write to DB.
274 if len(self.db_input_cache) >= self.db_cache_size:
275 self.writeout_cache()
270 276
271 277 # update the auto _i variables
272 278 self._iii = self._ii
@@ -282,11 +288,24 b' class HistoryManager(object):'
282 288 new_i : self._i00 }
283 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 301 def writeout_cache(self):
286 302 with self.db:
287 303 self.db.executemany("INSERT INTO history VALUES (?, ?, ?, ?)",
288 self.db_cache)
289 self.db_cache = []
304 self.db_input_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 310 def sync_inputs(self):
292 311 """Ensure raw and translated histories have same length."""
General Comments 0
You need to be logged in to leave comments. Login now