瀏覽代碼

Updated re for clean output. Added zf_log.

Steve Thielemann 5 年之前
父節點
當前提交
3cff6c70e5
共有 3 個文件被更改,包括 152 次插入71 次删除
  1. 2 0
      CMakeLists.txt
  2. 134 69
      mystic.c
  3. 16 2
      try-re.c

+ 2 - 0
CMakeLists.txt

@@ -38,6 +38,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
 # set(BUILD_TESTING ON)
 # include(CTest)
 
+add_subdirectory(zf_log)
 
 
 # Example for how to define a test which uses gtest_gmock
@@ -46,6 +47,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
 
 add_executable(mystic mystic.c)
 target_link_libraries(mystic util)
+target_link_libraries(mystic zf_log)
 
 add_executable(try-re try-re.c)
 

+ 134 - 69
mystic.c

@@ -17,13 +17,114 @@
 #include <stdlib.h>     // random()
 #include <ctype.h>
 
+#include <regex.h>
+
+// LOGGING with file output
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "zf_log.h"
+
+FILE *g_log_file;
+
+static void file_output_callback(const zf_log_message *msg, void *arg)
+{
+	(void)arg;
+	*msg->p = '\n';
+	fwrite(msg->buf, msg->p - msg->buf + 1, 1, g_log_file);
+	fflush(g_log_file);
+}
+
+static void file_output_close(void)
+{
+	fclose(g_log_file);
+}
+
+static void file_output_open(const char *const log_path)
+{
+	g_log_file = fopen(log_path, "a");
+	if (!g_log_file)
+	{
+		ZF_LOGW("Failed to open log file %s", log_path);
+		return;
+	}
+	atexit(file_output_close);
+	zf_log_set_output_v(ZF_LOG_PUT_STD, 0, file_output_callback);
+}
+
+const char * repr( const char * data) {
+    static char buffer[1024];
+    char * cp;
+
+    strcpy( buffer, data );
+    cp = buffer;
+    while ( *cp != 0) {
+        char c = *cp;
+
+        if (isspace(c)) {
+            if (c == ' ') {
+                cp++;
+                continue;
+            };
+            /* Ok, it's form-feed ('\f'), newline ('\n'), carriage return ('\r'),  horizontal tab ('\t'), and vertical tab ('\v') */
+            memmove( cp + 1, cp, strlen(cp) + 1);
+            *cp = '\\';
+            cp++;
+            switch(c) {
+                case '\f':
+                    *cp = 'f';
+                    cp++;
+                    break;
+                case '\n':
+                    *cp = 'n';
+                    cp++;
+                    break;
+                case '\r':
+                    *cp = 'r';
+                    cp++;
+                    break;
+                case '\t':
+                    *cp = 't';
+                    cp++;
+                    break;
+                case '\v':
+                    *cp = 'v';
+                    cp++;
+                    break;
+                default:
+                    *cp = '?';
+                    cp++;
+                    break;
+            }
+            continue;
+        }
+
+        if (isprint(c)) {
+            cp++;
+            continue;
+        };
+        
+        // Ok, default to \xHH output.
+        memmove( cp + 3, cp, strlen(cp) + 1);
+        *cp = '\\'; cp++;
+        *cp = 'x'; cp++;
+        char buffer[3];
+        sprintf(buffer, "%02x", (int)c);
+        *cp = buffer[0]; cp++;
+        *cp = buffer[1]; cp++;
+        continue;
+    }
+
+    return buffer;
+}
+
+// END LOGGING
+
+// What is the name of the actual, real Mystic that 
+// we'll be executing and manglying?
+
 #define TARGET "./mySTIC"
 #define BSIZE 1024
-#define LOGGING
-
-#ifdef LOGGING
-FILE * fp;
-#endif
 
 const char * it_is_now(void) {
     static char buffer[100];
@@ -110,9 +211,8 @@ const char * process_trigger(int fd, const char * cp) {
             };
 
             if ((i > 0) && (i < 80)) {
-#ifdef BUGGER
-                fprintf(fp, "DEL %02d\n", i);
-#endif                
+                ZF_LOGI("DEL %02d", i);
+
                 for (x = 0; x < i; x++ ) {
                     write(fd, "\b \b", 3);
                 }
@@ -163,10 +263,7 @@ const char * process_trigger(int fd, const char * cp) {
                 cp++;
             };
             if ( ( i > 0 ) && ( i < 10) ) {
-#ifdef BUGGER
-                fprintf(fp, "RENDER %d\n", i);
-#endif                
-
+                ZF_LOGI("RENDER %d", i);
                 current_render.effect = i;
             } else {
                 current_render.effect = 0;
@@ -179,10 +276,7 @@ const char * process_trigger(int fd, const char * cp) {
                 cp++;
             };
             if ( ( i > 0 ) && ( i < 10) ) {
-#ifdef BUGGER
-                fprintf(fp, "SPEED %d\n", i);
-#endif                
-
+                ZF_LOGI( "SPEED %d", i);
                 current_render.speed = i;
             } else {
                 current_render.speed = 0;
@@ -195,11 +289,8 @@ const char * process_trigger(int fd, const char * cp) {
                 cp++;
             };
             if ( ( i > 0 ) && ( i < 10) ) {
-#ifdef BUGGER
-                fprintf(fp, "PAWS %d\n", i);
-#endif                
+                ZF_LOGI( "PAWS %d", i);
                 // sleep(i);
-
                 if (!render_overlimit) {
                     sleep(i);
                 };
@@ -257,10 +348,7 @@ void render( int fd, const char * string_out ) {
 
     reset_render();
 
-#ifdef BUGGER
-    fprintf(fp, "render(%d, %s)\n", fd, string_out);
-    fflush(fp);
-#endif
+    ZF_LOGD( "render(%d, %s)", fd, string_out);
 
     // Check our time from time to time.
     // If we start running long, kill sleeps.
@@ -274,30 +362,19 @@ void render( int fd, const char * string_out ) {
                 current_render.speed = 0;
             };
 
-#ifdef BUGGER
-            fprintf(fp, "re(%c)\n", *cp);
-            fflush(fp);
-#endif
             // write(fd, cp, 1 );
             render_effect(fd, *cp);
             cp++;
         };
 
-#ifdef BUGGER
-        fprintf(fp, "at trigger: (%s)\n", cp);
-        fflush(fp);
-#endif
+        // ZF_LOGI( "at trigger: (%s)", cp);
 
         cp += strlen(TRIGGER);
 
         // Ok, we're pointing at the trigger -- do something.
         cp = process_trigger(fd, cp);
 
-#ifdef BUGGER
-        fprintf(fp, "after trigger: (%s)\n", cp);
-        fflush(fp);
-#endif
-
+        // ZF_LOGI( "after trigger: (%s)", cp);
     };
 
     // We still might be under a rendering effect.
@@ -336,10 +413,7 @@ void harry_event(int fd) {
 
     sprintf(buffer, "^S2%s^P2^D%02d", cp, (int)strlen(cp));
 
-#ifdef LOGGING
-    fprintf(fp, "harry_event: render(%d, \"%s\")\n", fd, buffer);
-    fflush(fp);
-#endif
+    ZF_LOGD( "harry_event: render(%d, \"%s\")", fd, buffer);
 
     render(fd, buffer);
 }
@@ -401,15 +475,9 @@ int main(int argc, char * argv[])
 {
     int master;
     pid_t pid;
-    
-#ifdef LOGGING    
-    // FILE * fp;
+    int node = -1;
 
-    fp = fopen("mystic_pipe.data", "wb");
-    if ( fp == NULL ) {
-        return 2;
-    }
-#endif
+    file_output_open("horrible_harry.log");
 
     srandom(time(NULL));
 
@@ -434,18 +502,17 @@ int main(int argc, char * argv[])
     for (int x = 0; x < argc; x++) {
         if (strncmp("-U", argv[x], 2) == 0) {
             username = argv[x] + 2;
-#ifdef LOGGING
-            fprintf(fp, "Username: [%s]\n", username);
-#endif            
-            break;
+            ZF_LOGI( "Username: [%s]", username);
+        };
+        if (strncmp("-SL", argv[x], 3) == 0) {
+            node = argv[x][3] - '0' + 1;
+            ZF_LOGI( "Node: %d", node);
         }
     }
 
     if (username != NULL) {
         locate_user(username);
-#ifdef LOGGING
-        fprintf(fp, "Username: [%s] A.K.A. [%s]\n", username, fullname);
-#endif        
+        ZF_LOGD( "Username: [%s] A.K.A. [%s]", username, fullname);
     }
 
     // With IGNBRK  I don't think I need this anymore.  (Nope!)
@@ -524,10 +591,8 @@ int main(int argc, char * argv[])
             if ( select(master+1, &read_fd, &write_fd, &except_fd, &timeout) == 0 ) {
                 // This means timeout!
                 harry_event(STDOUT_FILENO);
-#ifdef BUGGER
-                fprintf(fp, "%s : TICK\n", it_is_now());
-                fprintf(fp, "STDOUT is %d\n", STDOUT_FILENO);
-#endif                
+                ZF_LOGI( "%s : TICK", it_is_now());
+
             }
 
             char input[BSIZE + 1];
@@ -549,9 +614,9 @@ int main(int argc, char * argv[])
                     write(STDOUT_FILENO, &output, total);
 
                     // This is OUTPUT from the BBS
-#ifdef LOGGING
-                    fprintf(fp, ">> [%s]\n", output);
-#endif
+                    ZF_LOGI( ">> %s", repr(output));
+                    // ZF_LOGI_MEM( output, strlen(output), ">> ");
+
                 } else
                     break;
             }
@@ -563,21 +628,21 @@ int main(int argc, char * argv[])
                 total = read(STDIN_FILENO, &input, BSIZE);
                 input[total] = 0;
                 // e escreva no bc
+                ZF_LOGI( "<< %s", repr(input));
+
                 write(master, &input, total);
 
                 // This is INPUT from the USER
-#ifdef LOGGING
-                fprintf(fp, "<< [%s]\n", input);
-#endif
+                // ZF_LOGI_MEM( input, strlen(input), "<< ");
+                
              }
         }
 
         // Restore terminal
         tcsetattr(1, TCSAFLUSH, &orig1);
-#ifdef LOGGING
-        fclose(fp);
-#endif        
+        ZF_LOGD("exit");
     }
+
     return 0;
 }
 

+ 16 - 2
try-re.c

@@ -30,10 +30,24 @@ int regmatch( regex_t *preg, const char * string, size_t nmatch, regmatch_t pmat
     return matches;
 } 
 
+const char * clean_string(const char * badansi) {
+    static char buffer[1024];
+    char * cp;
+
+    strcpy(buffer, badansi);
+    cp = buffer;
+    while ( (cp = strstr(cp, "\x1b")) != NULL )
+    {
+        *cp = '~';
+    };
+    return buffer;
+}
+
 void test(const char * trythis) {
     regmatch_t regmatches[MAX_MATCH];
     int matches, x;
-    printf("TEST (%s)\n", trythis);
+    const char * clean = clean_string(trythis);
+    printf("TEST (%s)\n", clean);
 
     matches = regmatch(&regex, trythis, MAX_MATCH, regmatches, 0);
     if (matches == 0) {
@@ -42,7 +56,7 @@ void test(const char * trythis) {
         printf("%d matches:\n", matches);
         for( x = 0; x < matches; x++) {
             printf("%d (%d - %d)\n", x, regmatches[x].rm_so, regmatches[x].rm_eo);
-            printf("    %*s [%.*s]\n", regmatches[x].rm_so, "", regmatches[x].rm_eo - regmatches[x].rm_so, trythis + regmatches[x].rm_so);
+            printf("    %*s [%.*s]\n", regmatches[x].rm_so, "", regmatches[x].rm_eo - regmatches[x].rm_so, clean + regmatches[x].rm_so);
         }
     }
 }