View Single Post
Old 04-08-2025, 12:44 PM   #96
KevinH
Sigil Developer
KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.KevinH ought to be getting tired of karma fortunes by now.
 
Posts: 8,906
Karma: 6120478
Join Date: Nov 2009
Device: many
@BeckyEbook

Here is a proof of concept adapting your approach to using just an environment variable to determine the full absolute path to the desired log file.

Code:
diff --git a/src/Resource_Files/python3lib/functionrep.py b/src/Resource_Files/python3lib/functionrep.py
index 3a0aca2ca..d61f85e3a 100644
--- a/src/Resource_Files/python3lib/functionrep.py
+++ b/src/Resource_Files/python3lib/functionrep.py
@@ -1,4 +1,5 @@
 import re
+import os
 
 from fr_utils import unescapeit, toUpper, toLower, swapcase, capitalize
 
@@ -114,3 +115,10 @@ def replace_titlecase_ignore_tags(match, number, file_name, metadata, data, *arg
 def replace_swapcase_ignore_tags(match, number, file_name, metadata, data, *args, **kwargs):
     '''Swap the case of the matched text, ignoring the text inside tag definitions.'''
     return apply_func_to_html_text(match, swapcase)
+
+
+def replace_debug_log(message):
+    logfile = os.environ.get('SIGIL_FUNCTION_REPLACE_LOG_FILE', None);
+    if logfile:
+        with open(logfile, "a", encoding="utf-8") as f:
+            f.write(message)
diff --git a/src/Resource_Files/python3lib/functionsearch.py b/src/Resource_Files/python3lib/functionsearch.py
index 958b0787f..104211341 100644
--- a/src/Resource_Files/python3lib/functionsearch.py
+++ b/src/Resource_Files/python3lib/functionsearch.py
@@ -124,6 +124,7 @@ class FunctionSearch(object):
         global replace_capitalize_ignore_tags
         global replace_titlecase_ignore_tags
         global replace_swapcase_ignore_tags
+        global replace_debug_log
 
         self.metadataxml = metadataxml
         self.function_name = function_name
@@ -157,6 +158,7 @@ class FunctionSearch(object):
             try:
                 result=self.replace(match,self.number,self.bookpath,self.metadataxml,self.funcData)
             except Exception as e:
+                replace_debug_log(str(e))
                 print(e)
             return result
 
@@ -175,6 +177,7 @@ class FunctionSearch(object):
         try:
             result=self.replace(match,self.number,self.bookpath,self.metadataxml,self.funcData)
         except Exception as e:
+            replace_debug_log(str(e))
             print(e)
         return result

Then to use it inside your replace function first set the environment variable:

export SIGIL_FUNCTION_REPLACE_LOG_FILE=/Users/kbhend/Desktop/junk.txt

Then in your replace routine you can now use the replace_debug_log routine.

Code:
def replace(match, number, file_name, metadata, data):
    if match:
        replace_debug_log("I am here with number " + str(number) + "\n")
        return match.group(0)

Please give this a try in your own build of Sigil. If that works, I will try to refine this approach using redirecting of sys.stdout and sys.stderr behind the scenes if I can.
KevinH is offline   Reply With Quote