Browse Source

ZuluSCSI_log: Reduce flash usage

Move the beginning and end logic of log messages to separate
functions so that they can be shared between calls.

The DaynaPORT build was running close to the ROM drive defined
flash size limit. This change frees up about 5 kB of flash and
ram.
Petteri Aimonen 6 months ago
parent
commit
48dc31afe7
2 changed files with 49 additions and 14 deletions
  1. 36 0
      src/ZuluSCSI_log.cpp
  2. 13 14
      src/ZuluSCSI_log.h

+ 36 - 0
src/ZuluSCSI_log.cpp

@@ -145,6 +145,42 @@ void log_raw(bytearray array)
     }
 }
 
+void logmsg_start()
+{
+    log_raw("[", (int)millis(), "ms] ");
+}
+
+void logmsg_end()
+{
+    log_raw("\r\n");
+}
+
+bool dbgmsg_start()
+{
+    if (g_log_debug)
+    {
+        // Check if log mask is not the default value, the selection was a success, and the selected ID was not match, then skip logging
+        if ( g_scsi_log_mask != 0xFF
+            && (SCSI_STS_SELECTION_SUCCEEDED & *SCSI_STS_SELECTED)
+            && (0 == (g_scsi_log_mask & (1 << (*SCSI_STS_SELECTED & 7))))
+           )
+        {
+            return false;
+        }
+
+        log_raw("[", (int)millis(), "ms] DBG ");
+
+        return true;
+    }
+
+    return false;
+}
+
+void dbgmsg_end()
+{
+    log_raw("\r\n");
+}
+
 uint32_t log_get_buffer_len()
 {
     return g_logpos;

+ 13 - 14
src/ZuluSCSI_log.h

@@ -73,6 +73,14 @@ inline void log_raw()
     // End of template recursion
 }
 
+// These functions contain the common prefix and end of each
+// log message, and check if debug messages are enabled.
+// Having them in separate functions avoids inlining the code
+// into every call.
+void logmsg_start();
+void logmsg_end();
+bool dbgmsg_start();
+void dbgmsg_end();
 
 extern "C" unsigned long millis();
 
@@ -89,28 +97,19 @@ inline void log_raw(T first, T2 second, Rest... rest)
 template<typename... Params>
 inline void logmsg(Params... params)
 {
-    log_raw("[", (int)millis(), "ms] ");
+    logmsg_start();
     log_raw(params...);
-    log_raw("\r\n");
+    logmsg_end();
 }
 
 // Format a complete debug message
 template<typename... Params>
 inline void dbgmsg(Params... params)
 {
-    if (g_log_debug)
+    if (g_log_debug && dbgmsg_start())
     {
-        // Check if log mask is not the default value, the selection was a success, and the selected ID was not match, then skip logging
-        if ( g_scsi_log_mask != 0xFF
-            && (SCSI_STS_SELECTION_SUCCEEDED & *SCSI_STS_SELECTED)
-            && (0 == (g_scsi_log_mask & (1 << (*SCSI_STS_SELECTED & 7))))
-           )
-        {
-            return;
-        }
-        log_raw("[", (int)millis(), "ms] DBG ");
         log_raw(params...);
-        log_raw("\r\n");
+        dbgmsg_end();
     }
 }
 
@@ -134,4 +133,4 @@ void dbgmsg_f(const char *format, ...);
 #ifdef __cplusplus
 }
 #endif
-#endif
+#endif