]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add colorize option to mod_console (see in-tree conf/console.conf.xml)
authorAnthony Minessale <anthony.minessale@gmail.com>
Wed, 28 Feb 2007 18:28:07 +0000 (18:28 +0000)
committerAnthony Minessale <anthony.minessale@gmail.com>
Wed, 28 Feb 2007 18:28:07 +0000 (18:28 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@4413 d0543943-73ff-0310-b7d9-9358b9ac24b2

conf/console.conf.xml
src/include/switch_types.h
src/mod/loggers/mod_console/mod_console.c

index 9ea7b65c5ec3965fa4ddb30be4205ae5b0ebfbe9..20960c9ea934c5045b0cb854e1431c8a4e30ecd2 100644 (file)
@@ -5,4 +5,8 @@
     <!-- <param name="log_event" value="DEBUG"/> -->
     <param name="all" value="DEBUG"/>
   </mappings>
+  <settings>
+    <!-- uncomment for color logging (for debugging) -->
+    <!--<param name="colorize" value="true"/>-->
+  </settings>
 </configuration>
index 0da1ed8b41d538c6925ed986a992efa46f3bad88..f7adb2290e9522bf4918bda5d17db5435e11a433 100644 (file)
 #include <ctype.h>
 SWITCH_BEGIN_EXTERN_C
 
+#define SWITCH_SEQ_ESC "\033["
+
+/* Ansi Control character suffixes */
+#define SWITCH_SEQ_HOME_CHAR 'H'
+#define SWITCH_SEQ_HOME_CHAR_STR "H"
+#define SWITCH_SEQ_CLEARLINE_CHAR '1'
+#define SWITCH_SEQ_CLEARLINE_CHAR_STR "1"
+#define SWITCH_SEQ_CLEARLINEEND_CHAR "K"
+#define SWITCH_SEQ_CLEARSCR_CHAR0 '2'
+#define SWITCH_SEQ_CLEARSCR_CHAR1 'J'
+#define SWITCH_SEQ_CLEARSCR_CHAR "2J"
+
+#define SWITCH_SEQ_DEFAULT_COLOR SWITCH_SEQ_ESC SWITCH_SEQ_END_COLOR /* Reset to Default fg/bg color */
+#define SWITCH_SEQ_AND_COLOR ";" /* To add multiple color definitions */
+#define SWITCH_SEQ_END_COLOR "m" /* To end color definitions */
+
+/* Foreground colors values */
+#define SWITCH_SEQ_F_BLACK "30"
+#define SWITCH_SEQ_F_RED "31"
+#define SWITCH_SEQ_F_GREEN "32"
+#define SWITCH_SEQ_F_YELLOW "33"
+#define SWITCH_SEQ_F_BLUE "34"
+#define SWITCH_SEQ_F_MAGEN "35"
+#define SWITCH_SEQ_F_CYAN "36"
+#define SWITCH_SEQ_F_WHITE "37"
+
+/* Background colors values */
+#define SWITCH_SEQ_B_BLACK "40"
+#define SWITCH_SEQ_B_RED "41"
+#define SWITCH_SEQ_B_GREEN "42"
+#define SWITCH_SEQ_B_YELLOW "43"
+#define SWITCH_SEQ_B_BLUE "44"
+#define SWITCH_SEQ_B_MAGEN "45"
+#define SWITCH_SEQ_B_CYAN "46"
+#define SWITCH_SEQ_B_WHITE "47"
+
+/* Preset escape sequences - Change foreground colors only */
+#define SWITCH_SEQ_FBLACK SWITCH_SEQ_ESC SWITCH_SEQ_F_BLACK SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_FRED SWITCH_SEQ_ESC SWITCH_SEQ_F_RED SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_FGREEN SWITCH_SEQ_ESC SWITCH_SEQ_F_GREEN SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_FYELLOW SWITCH_SEQ_ESC SWITCH_SEQ_F_YELLOW SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_FBLUE SWITCH_SEQ_ESC SWITCH_SEQ_F_BLUE SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_FMAGEN SWITCH_SEQ_ESC SWITCH_SEQ_F_MAGEN SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_FCYAN SWITCH_SEQ_ESC SWITCH_SEQ_F_CYAN SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_FWHITE SWITCH_SEQ_ESC SWITCH_SEQ_F_WHITE SWITCH_SEQ_END_COLOR
+
+#define SWITCH_SEQ_BBLACK SWITCH_SEQ_ESC SWITCH_SEQ_B_BLACK SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_BRED SWITCH_SEQ_ESC SWITCH_SEQ_B_RED SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_BGREEN SWITCH_SEQ_ESC SWITCH_SEQ_B_GREEN SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_BYELLOW SWITCH_SEQ_ESC SWITCH_SEQ_B_YELLOW SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_BBLUE SWITCH_SEQ_ESC SWITCH_SEQ_B_BLUE SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_BMAGEN SWITCH_SEQ_ESC SWITCH_SEQ_B_MAGEN SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_BCYAN SWITCH_SEQ_ESC SWITCH_SEQ_B_CYAN SWITCH_SEQ_END_COLOR
+#define SWITCH_SEQ_BWHITE SWITCH_SEQ_ESC SWITCH_SEQ_B_WHITE SWITCH_SEQ_END_COLOR
+
+/* Preset escape sequences */
+#define SWITCH_SEQ_HOME SWITCH_SEQ_ESC SWITCH_SEQ_HOME_CHAR_STR
+#define SWITCH_SEQ_CLEARLINE SWITCH_SEQ_ESC SWITCH_SEQ_CLEARLINE_CHAR_STR
+#define SWITCH_SEQ_CLEARLINEEND SWITCH_SEQ_ESC SWITCH_SEQ_CLEARLINEEND_CHAR
+#define SWITCH_SEQ_CLEARSCR SWITCH_SEQ_ESC SWITCH_SEQ_CLEARSCR_CHAR SWITCH_SEQ_HOME
+
+
 #ifdef WIN32
 #define SWITCH_PATH_SEPARATOR "\\"
 #else
index fed16c87b0ae312dd1e07de6cac1388bd5d05d89..f8f35fc5b8349d2615c909dec23e3b41f7700ba0 100644 (file)
@@ -33,6 +33,9 @@
 
 static const char modname[] = "mod_console";
 static const uint8_t STATIC_LEVELS[] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
+static int COLORIZE = 0;
+static const char *COLORS[] = 
+       { SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FRED, SWITCH_SEQ_FMAGEN, SWITCH_SEQ_FCYAN, SWITCH_SEQ_FGREEN, SWITCH_SEQ_FYELLOW, "" };
 
 static switch_loadable_module_interface_t console_module_interface = {
        /*.module_name */ modname,
@@ -100,6 +103,17 @@ static switch_status_t config_logger(void)
                }
        }
 
+       if ((settings = switch_xml_child(cfg, "settings"))) {
+               for (param = switch_xml_child(settings, "param"); param; param = param->next) {
+                       char *var = (char *) switch_xml_attr_soft(param, "name");
+                       char *val = (char *) switch_xml_attr_soft(param, "value");
+
+                       if (!strcasecmp(var, "colorize") && switch_true(val)) {
+                               COLORIZE = 1;
+                       }
+               }
+       }
+
        switch_xml_free(xml);
 
        return SWITCH_STATUS_SUCCESS;
@@ -128,7 +142,11 @@ static switch_status_t switch_console_logger(const switch_log_node_t *node, swit
                } 
 
                if (!log_hash || (((all_level > - 1) || lookup) && level >= node->level)) {
-                       fprintf(handle, "%s", node->data);
+                       if (COLORIZE) {
+                               fprintf(handle, "%s%s%s", COLORS[node->level], node->data, SWITCH_SEQ_DEFAULT_COLOR);
+                       } else {
+                               fprintf(handle, "%s", node->data);
+                       }
                }
        } else {
                fprintf(stderr, "HELP I HAVE NO CONSOLE TO LOG TO!\n");