about summary refs log tree commit diff stats
path: root/src/teliva.c
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2022-02-12 15:45:04 -0800
committerKartik K. Agaram <vc@akkartik.com>2022-02-12 15:45:04 -0800
commit6a485ae4b4317221c119c76632d4c52dabaf3376 (patch)
treea41749373562cbaaa8cc92f63cd1a4b4385888f3 /src/teliva.c
parentbbd47aaa5b0b6535fa967b4d397a7a9015f3d53c (diff)
downloadteliva-6a485ae4b4317221c119c76632d4c52dabaf3376.tar.gz
stop aborting if audit log fills up
When I started logging getch() events (which are just to help the reader
orient on the log), this suddenly became more urgent.

Now the log is larger, and it's also a circular buffer that rolls back
to the start when it fills up.

The next failure mode will be if we see the buffer filled up with just
getch() calls, reducing visibility over real file and network
operations. In which case we'll need to start coalescing getch() events.
Diffstat (limited to 'src/teliva.c')
-rw-r--r--src/teliva.c42
1 files changed, 32 insertions, 10 deletions
diff --git a/src/teliva.c b/src/teliva.c
index a430676..eb79285 100644
--- a/src/teliva.c
+++ b/src/teliva.c
@@ -1676,8 +1676,10 @@ typedef struct {
   char* func;
 } AuditEvent;
 
-AuditEvent audit_event[1024];
+#define NAUDIT 8192
+AuditEvent audit_event[NAUDIT];
 int naudit = 0;
+int iaudit = 0;
 
 void append_to_audit_log(lua_State* L, const char* buffer) {
   lua_Debug ar;
@@ -1687,7 +1689,13 @@ void append_to_audit_log(lua_State* L, const char* buffer) {
   audit_event[naudit].line = strdup(buffer);
   audit_event[naudit].func = strdup(ar.name);
   ++naudit;
-  assert(naudit < 1024);
+  if (naudit >= NAUDIT)
+    naudit = 0;
+  if (naudit == iaudit) {
+    ++iaudit;
+    if (iaudit >= NAUDIT)
+      iaudit = 0;
+  }
 }
 
 static void events_menu() {
@@ -1701,19 +1709,33 @@ static void events_menu() {
   attrset(A_NORMAL);
 }
 
+static void render_event(int i, int y, int cursor) {
+  mvaddstr(y, 2, "");
+  if (i == cursor)
+    draw_highlighted_definition_name(audit_event[i].func);
+  else
+    draw_definition_name(audit_event[i].func);
+  mvaddstr(y, 16, audit_event[i].line);
+}
+
 static void render_events(int cursor) {
   clear();
   attrset(A_BOLD);
   mvaddstr(1, 0, "Recent events");
   attrset(A_NORMAL);
-  for (int i = 0, y = 3; i < naudit; ++i, ++y) {
-    if (i >= LINES-1) break;
-    mvaddstr(y, 2, "");
-    if (i == cursor)
-      draw_highlighted_definition_name(audit_event[i].func);
-    else
-      draw_definition_name(audit_event[i].func);
-    mvaddstr(y, 16, audit_event[i].line);
+  if (iaudit == 0) {
+    /* circular buffer might not be full */
+    for (int i = 0, y = 3; i < naudit; ++i, ++y) {
+      if (i >= LINES-1) break;
+      render_event(i, y, cursor);
+    }
+  }
+  else {
+    /* circular buffer guaranteed to be full */
+    for (int i = 0, y = 3; i < NAUDIT; ++i, ++y) {
+      if (i >= LINES-1) break;
+      render_event((iaudit+i)%NAUDIT, y, cursor);
+    }
   }
   events_menu();
   refresh();