From a63b147a93c00a949b2e59ebdcac4b2c878ccc47 Mon Sep 17 00:00:00 2001
From: Paul Kulchenko <paul@kulchenko.com>
Date: Thu, 19 Aug 2021 09:32:33 -0700
Subject: [PATCH] Add file/line reporting to redbean LuaLog (#250)

---
 tool/net/redbean.c | 22 +++++++---------------
 1 file changed, 7 insertions(+), 15 deletions(-)

diff --git a/tool/net/redbean.c b/tool/net/redbean.c
index 3922728c1..8748cd6fd 100644
--- a/tool/net/redbean.c
+++ b/tool/net/redbean.c
@@ -4816,28 +4816,20 @@ static int LuaHidePath(lua_State *L) {
 }
 
 static int LuaLog(lua_State *L) {
-  int level;
+  int level, line;
   lua_Debug ar;
   const char *msg, *module;
   level = luaL_checkinteger(L, 1);
   if (LOGGABLE(level)) {
-    /*
-     * TODO: There needs to be some reasonable way to get the source
-     *       filename and line number.
-     */
     msg = luaL_checkstring(L, 2);
-    lua_getstack(L, 0, &ar);
-    lua_getinfo(L, "nSl", &ar);
-    if (!strcmp(ar.name, "main")) {
-      if (ar.source) {
-        module = ar.source;
-      } else {
-        module = gc(strndup(effectivepath.p, effectivepath.n));
-      }
+    if (lua_getstack(L, 1, &ar) && lua_getinfo(L, "Sl", &ar)) {
+      module = ar.short_src;
+      line = ar.currentline;
     } else {
-      module = ar.name;
+      module = gc(strndup(effectivepath.p, effectivepath.n));
+      line = -1;
     }
-    flogf(level, module, ar.currentline, NULL, "%s", msg);
+    flogf(level, module, line, NULL, "%s", msg);
   }
   return 0;
 }