logging extend level set api to allow setting emission function 81/2881/1
authorAndy Green <andy.green@linaro.org>
Sat, 12 Jan 2013 01:17:42 +0000 (09:17 +0800)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 7 Mar 2013 21:01:23 +0000 (13:01 -0800)
Signed-off-by: Andy Green <andy.green@linaro.org>
README-test-server
lib/libwebsockets.c
lib/libwebsockets.h
libwebsockets-api-doc.html
test-server/test-client.c
test-server/test-fraggle.c
test-server/test-ping.c
test-server/test-server-extpoll.c
test-server/test-server.c

index 234f43d..a8d9999 100644 (file)
@@ -262,6 +262,10 @@ available are (OR together the numbers to select multiple)
  64  EXTENSION
  128 CLIENT
 
+Also using lws_set_log_level api you may provide a custom callback to actually
+emit the log string.  By default, this points to an internal emit function
+that sends to stderr.  Setting it to NULL leaves it as it is instead.
+
 
 Websocket version supported
 ---------------------------
index b0e7a6d..53e71e0 100644 (file)
@@ -44,6 +44,8 @@ int openssl_websocket_private_data_index;
 #endif
 
 static int log_level = LLL_ERR | LLL_WARN;
+static void lwsl_emit_stderr(const char *line);
+static void (*lwsl_emit)(const char *line) = lwsl_emit_stderr;
 static const char *log_level_names[] = {
        "ERR",
        "WARN",
@@ -3273,35 +3275,50 @@ libwebsocket_ensure_user_space(struct libwebsocket *wsi)
        return wsi->user_space;
 }
 
+
+static void lwsl_emit_stderr(const char *line)
+{
+       fprintf(stderr, "%s", line);
+}
+
 void _lws_log(int filter, const char *format, ...)
 {
+       char buf[256];
        va_list ap;
        int n;
+       int pos = 0;
 
        if (!(log_level & filter))
                return;
 
        for (n = 0; n < LLL_COUNT; n++)
                if (filter == (1 << n)) {
-                       fprintf(stderr, "%s: ", log_level_names[n]);
+                       pos = sprintf(buf, "%s: ", log_level_names[n]);
                        break;
                }
 
        va_start(ap, format);
-       vfprintf(stderr, format, ap);
-       va_end(ap);         
+       vsnprintf(buf + pos, (sizeof buf) - pos, format, ap);
+       buf[(sizeof buf) - 1] = '\0';
+       va_end(ap);
+
+       lwsl_emit(buf);
 }
 
 /**
  * lws_set_log_level() - Set the logging bitfield
  * @level:     OR together the LLL_ debug contexts you want output from
+ * @log_emit_function: NULL to leave it as it is, or a user-supplied
+ *                     function to perform log string emission instead of
+ *                     the default stderr one.
  *
- *
- *     defaults to err and warn contexts enabled
+ *     log level defaults to "err" and "warn" contexts enabled only and
+ *     emission on stderr.
  */
 
-void lws_set_log_level(int level)
+void lws_set_log_level(int level, void (*log_emit_function)(const char *line))
 {
        log_level = level;
+       if (log_emit_function)
+               lwsl_emit = log_emit_function;
 }
-
index 25aa554..ef951d6 100644 (file)
@@ -655,7 +655,7 @@ struct libwebsocket_extension {
 };
 
 LWS_EXTERN
-void lws_set_log_level(int level);
+void lws_set_log_level(int level, void (*log_emit_function)(const char *line));
 
 LWS_EXTERN struct libwebsocket_context *
 libwebsocket_create_context(int port, const char * interf,
index b41b4c7..acda2b5 100644 (file)
@@ -353,16 +353,21 @@ having to take any care about data visibility between the processes, it'll
 <h2>lws_set_log_level - Set the logging bitfield</h2>
 <i>void</i>
 <b>lws_set_log_level</b>
-(<i>int</i> <b>level</b>)
+(<i>int</i> <b>level</b>,
+<i>void (*</i><b>log_emit_function</b>) <i>(const char *line)</i>)
 <h3>Arguments</h3>
 <dl>
 <dt><b>level</b>
 <dd>OR together the LLL_ debug contexts you want output from
+<dt><b>log_emit_function</b>
+<dd>NULL to leave it as it is, or a user-supplied
+function to perform log string emission instead of
+the default stderr one.
 </dl>
 <h3>Description</h3>
 <blockquote>
-<p>
-defaults to err and warn contexts enabled
+log level defaults to "err" and "warn" contexts enabled only and
+emission on stderr.
 </blockquote>
 <hr>
 <h2>libwebsocket_write - Apply protocol then write data to client</h2>
index 4d8d474..9f65269 100644 (file)
@@ -222,7 +222,7 @@ int main(int argc, char **argv)
                        continue;
                switch (n) {
                case 'd':
-                       lws_set_log_level(atoi(optarg));
+                       lws_set_log_level(atoi(optarg), NULL);
                        break;
                case 's':
                        use_ssl = 2; /* 2 = allow selfsigned */
index 14cc3cd..2e899de 100644 (file)
@@ -264,7 +264,7 @@ int main(int argc, char **argv)
                        continue;
                switch (n) {
                case 'd':
-                       lws_set_log_level(atoi(optarg));
+                       lws_set_log_level(atoi(optarg), NULL);
                        break;
                case 's':
                        use_ssl = 1;
index 93df59e..bc03100 100644 (file)
@@ -339,7 +339,7 @@ int main(int argc, char **argv)
                        continue;
                switch (n) {
                case 'd':
-                       lws_set_log_level(atoi(optarg));
+                       lws_set_log_level(atoi(optarg), NULL);
                        break;
                case 'm':
                        use_mirror = 1;
index 470997c..6266e9f 100644 (file)
@@ -460,7 +460,7 @@ int main(int argc, char **argv)
                        continue;
                switch (n) {
                case 'd':
-                       lws_set_log_level(atoi(optarg));
+                       lws_set_log_level(atoi(optarg), NULL);
                        break;
                case 's':
                        use_ssl = 1;
index 110b475..06bfd6e 100644 (file)
@@ -417,7 +417,7 @@ int main(int argc, char **argv)
                        continue;
                switch (n) {
                case 'd':
-                       lws_set_log_level(atoi(optarg));
+                       lws_set_log_level(atoi(optarg), NULL);
                        break;
                case 's':
                        use_ssl = 1;