Check-in [f55c6a1b62]
Not logged in
Overview

SHA1 Hash:f55c6a1b62ccda5987d8273290443ce329edd958
Date: 2008-10-24 18:23:23
User: drh
Comment:Begin adding a TH1 script trace mechanism actived by the --th_trace option on the "server" and "ui" commands. The implementation is incomplete, but the plane is landing....
Timelines: ancestors | descendants | both | trunk
Other Links: files | ZIP archive | manifest

Tags And Properties
Changes
[hide diffs]

Modified src/main.c from [c1173878a4] to [99156e48f6].

@@ -124,10 +124,12 @@
   /* For defense against Cross-site Request Forgery attacks */
   char zCsrfToken[12];    /* Value of the anti-CSRF token */
   int okCsrf;             /* Anti-CSRF token is present and valid */
 
   FILE *fDebug;           /* Write debug information here, if the file exists */
+  int thTrace;            /* True to enable TH1 debugging output */
+  Blob thLog;             /* Text of the TH1 debugging output */
 
   /* Storage for the aux() and/or option() SQL function arguments */
   int nAux;                    /* Number of distinct aux() or option() values */
   const char *azAuxName[MX_AUX]; /* Name of each aux() or option() value */
   char *azAuxParam[MX_AUX];      /* Param of each aux() or option() value */
@@ -706,10 +708,14 @@
   int iPort;
   const char *zPort;
   char *zBrowser;
   char *zBrowserCmd = 0;
 
+  g.thTrace = find_option("th_trace", 0, 0)!=0;
+  if( g.thTrace ){
+    blob_zero(&g.thLog);
+  }
   zPort = find_option("port", "P", 1);
   if( zPort ){
     iPort = atoi(zPort);
   }else{
     iPort = 8080;

Modified src/style.c from [89fb0defc5] to [41b3e7ff54].

@@ -89,10 +89,12 @@
   zTitle = vmprintf(zTitleFormat, ap);
   va_end(ap);
 
   cgi_destination(CGI_HEADER);
 
+  if( g.thTrace ) Th_Trace("BEGIN_HEADER<br />\n", -1);
+
   /* Generate the header up through the main menu */
   Th_Store("project_name", db_get("project-name","Unnamed Fossil Project"));
   Th_Store("title", zTitle);
   Th_Store("baseurl", g.zBaseURL);
   Th_Store("index_page", db_get("index-page","/home"));
@@ -100,11 +102,13 @@
   Th_Store("manifest_version", MANIFEST_VERSION);
   Th_Store("manifest_date", MANIFEST_DATE);
   if( g.zLogin ){
     Th_Store("login", g.zLogin);
   }
+  if( g.thTrace ) Th_Trace("BEGIN_HEADER_SCRIPT<br />\n", -1);
   Th_Render(zHeader);
+  if( g.thTrace ) Th_Trace("END_HEADER<br />\n", -1);
   Th_Unstore("title");   /* Avoid collisions with ticket field names */
   cgi_destination(CGI_BODY);
   g.cgiPanic = 1;
   headerHasBeenGenerated = 1;
 }
@@ -141,11 +145,20 @@
 
   /* Put the footer at the bottom of the page.
   */
   @ </div>
   zFooter = db_get("footer", (char*)zDefaultFooter);
+  if( g.thTrace ) Th_Trace("BEGIN_FOOTER<br />\n", -1);
   Th_Render(zFooter);
+  if( g.thTrace ) Th_Trace("END_FOOTER<br />\n", -1);
+
+  /* Render trace log if TH1 tracing is enabled. */
+  if( g.thTrace ){
+    cgi_append_content("<font color=\"red\"><hr>\n", -1);
+    cgi_append_content(blob_str(&g.thLog), blob_size(&g.thLog));
+    cgi_append_content("</font>\n", -1);
+  }
 }
 
 /* @-comment: // */
 /*
 ** The default page header.

Modified src/th_main.c from [a10cec9ea6] to [7b39ee91da].

@@ -50,10 +50,20 @@
   }
   free(p);
 }
 static Th_Vtab vtab = { xMalloc, xFree };
 
+/*
+** Generate a TH1 trace message if debugging is enabled.
+*/
+void Th_Trace(const char *zFormat, ...){
+  va_list ap;
+  va_start(ap, zFormat);
+  blob_vappendf(&g.thLog, zFormat, ap);
+  va_end(ap);
+}
+
 
 /*
 ** True if output is enabled.  False if disabled.
 */
 static int enableOutput = 1;
@@ -191,12 +201,17 @@
   void *p,
   int argc,
   const char **argv,
   int *argl
 ){
+  int rc;
   if( argc!=2 ){
     return Th_WrongNumArgs(interp, "hascap STRING");
+  }
+  rc = login_has_capability((char*)argv[1],argl[1]);
+  if( g.thTrace ){
+    Th_Trace("[hascap %.*h] => %d<br />\n", argl[1], argv[1], rc);
   }
   Th_SetResultInt(interp, login_has_capability((char*)argv[1],argl[1]));
   return TH_OK;
 }
 
@@ -292,11 +307,14 @@
   Th_SetResultInt(interp, n);
   return TH_OK;
 }
 
 /*
-** Make sure the interpreter has been initialized.
+** Make sure the interpreter has been initialized.  Initialize it if
+** it has not been already.
+**
+** The interpreter is stored in the g.interp global variable.
 */
 void Th_FossilInit(void){
   static struct _Command {
     const char *zName;
     Th_CommandProc xProc;
@@ -327,11 +345,14 @@
 ** Store a string value in a variable in the interpreter.
 */
 void Th_Store(const char *zName, const char *zValue){
   Th_FossilInit();
   if( zValue ){
-    Th_SetVar(g.interp, (char*)zName, -1, (char*)zValue, strlen(zValue));
+    if( g.thTrace ){
+      Th_Trace("set %h {%h}<br />\n", zName, zValue);
+    }
+    Th_SetVar(g.interp, zName, -1, zValue, strlen(zValue));
   }
 }
 
 /*
 ** Unset a variable.

Modified src/tkt.c from [4d8a2f78f1] to [ba64402245].

@@ -330,14 +330,17 @@
         "%s/tkthistory/%T", g.zTop, zUuid);
     style_submenu_element("Timeline", "Timeline Of This Ticket",
         "%s/tkttimeline/%T", g.zTop, zUuid);
   }
   style_header("View Ticket");
+  if( g.thTrace ) Th_Trace("BEGIN_TKTVIEW<br />\n", -1);
   ticket_init();
   initializeVariablesFromDb();
   zScript = ticket_viewpage_code();
+  if( g.thTrace ) Th_Trace("BEGIN_TKTVIEW_SCRIPT<br />\n", -1);
   Th_Render(zScript);
+  if( g.thTrace ) Th_Trace("END_TKTVIEW<br />\n", -1);
   style_footer();
 }
 
 /*
 ** TH command:   append_field FIELD STRING
@@ -349,20 +352,24 @@
 */
 static int appendRemarkCmd(
   Th_Interp *interp,
   void *p,
   int argc,
-  const unsigned char **argv,
+  const char **argv,
   int *argl
 ){
   int idx;
 
   if( argc!=3 ){
     return Th_WrongNumArgs(interp, "append_field FIELD STRING");
   }
+  if( g.thTrace ){
+    Th_Trace("append_field %#h {%#h}<br />\n",
+              argl[1], argv[1], argl[2], argv[2]);
+  }
   for(idx=0; idx<nField; idx++){
-    if( strncmp(azField[idx], (const char*)argv[1], argl[1])==0
+    if( strncmp(azField[idx], argv[1], argl[1])==0
         && azField[idx][argl[1]]==0 ){
       break;
     }
   }
   if( idx>=nField ){
@@ -384,11 +391,11 @@
 */
 static int submitTicketCmd(
   Th_Interp *interp,
   void *pUuid,
   int argc,
-  const unsigned char **argv,
+  const char **argv,
   int *argl
 ){
   char *zDate;
   const char *zUuid;
   int i;
@@ -432,24 +439,21 @@
   *(const char**)pUuid = zUuid;
   blob_appendf(&tktchng, "K %s\n", zUuid);
   blob_appendf(&tktchng, "U %F\n", g.zLogin ? g.zLogin : "");
   md5sum_blob(&tktchng, &cksum);
   blob_appendf(&tktchng, "Z %b\n", &cksum);
-
-  if( strncmp(g.zPath,"debug_",6)==0 ){
-    @ <hr><pre>
-    @ %h(blob_str(&tktchng))
-    @ </pre><hr>
-    blob_zero(&tktchng);
-    return TH_OK;
-  }
-
-  rid = content_put(&tktchng, 0, 0);
-  if( rid==0 ){
-    fossil_panic("trouble committing ticket: %s", g.zErrMsg);
-  }
-  manifest_crosslink(rid, &tktchng);
+  if( g.thTrace ){
+    Th_Trace("submit_ticket {\n<blockquote><pre>\n%h\n</pre></blockquote>\n"
+             "}<br />\n",
+       blob_str(&tktchng));
+  }else{
+    rid = content_put(&tktchng, 0, 0);
+    if( rid==0 ){
+      fossil_panic("trouble committing ticket: %s", g.zErrMsg);
+    }
+    manifest_crosslink(rid, &tktchng);
+  }
   return TH_RETURN;
 }
 
 
 /*
@@ -472,10 +476,11 @@
   if( !g.okNewTkt ){ login_needed(); return; }
   if( P("cancel") ){
     cgi_redirect("home");
   }
   style_header("New Ticket");
+  if( g.thTrace ) Th_Trace("BEGIN_TKTNEW<br />\n", -1);
   ticket_init();
   getAllTicketFields();
   initializeVariablesFromDb();
   initializeVariablesFromCGI();
   @ <form method="POST" action="%s(g.zBaseURL)/%s(g.zPath)">
@@ -483,15 +488,17 @@
   zScript = ticket_newpage_code();
   Th_Store("login", g.zLogin);
   Th_Store("date", db_text(0, "SELECT datetime('now')"));
   Th_CreateCommand(g.interp, "submit_ticket", submitTicketCmd,
                    (void*)&zNewUuid, 0);
-  if( Th_Render(zScript)==TH_RETURN && zNewUuid ){
+  if( g.thTrace ) Th_Trace("BEGIN_TKTNEW_SCRIPT<br />\n", -1);
+  if( Th_Render(zScript)==TH_RETURN && !g.thTrace && zNewUuid ){
     cgi_redirect(mprintf("%s/tktview/%s", g.zBaseURL, zNewUuid));
     return;
   }
   @ </form>
+  if( g.thTrace ) Th_Trace("END_TKTVIEW<br />\n", -1);
   style_footer();
 }
 
 /*
 ** WEBPAGE: tktedit
@@ -533,10 +540,11 @@
   if( nRec>1 ){
     @ <font color="red"><b>%d(nRec) tickets begin with: \"%h(zName)\"</b></font>
     style_footer();
     return;
   }
+  if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT<br />\n", -1);
   ticket_init();
   getAllTicketFields();
   initializeVariablesFromCGI();
   initializeVariablesFromDb();
   @ <form method="POST" action="%s(g.zBaseURL)/%s(g.zPath)">
@@ -545,15 +553,17 @@
   zScript = ticket_editpage_code();
   Th_Store("login", g.zLogin);
   Th_Store("date", db_text(0, "SELECT datetime('now')"));
   Th_CreateCommand(g.interp, "append_field", appendRemarkCmd, 0, 0);
   Th_CreateCommand(g.interp, "submit_ticket", submitTicketCmd, (void*)&zName,0);
-  if( Th_Render(zScript)==TH_RETURN && zName ){
+  if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT_SCRIPT<br />\n", -1);
+  if( Th_Render(zScript)==TH_RETURN && !g.thTrace && zName ){
     cgi_redirect(mprintf("%s/tktview/%s", g.zBaseURL, zName));
     return;
   }
   @ </form>
+  if( g.thTrace ) Th_Trace("BEGIN_TKTEDIT<br />\n", -1);
   style_footer();
 }
 
 /*
 ** Check the ticket table schema in zSchema to see if it appears to