Add debug_set_threshold() and thresholding of messages
diff --git a/ChangeLog b/ChangeLog
index 53cc592..7b14ce8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,7 +2,8 @@
 
 = PolarSSL 1.3 branch
 Features
-   * debug_set_log_mode() function added to determine raw or full logging
+   * debug_set_log_mode() added to determine raw or full logging
+   * debug_set_threshold() added to ignore messages over threshold level
 
 Changes
    * POLARSSL_CONFIG_OPTIONS has been removed. All values are individually
diff --git a/include/polarssl/debug.h b/include/polarssl/debug.h
index 96e08f3..599ce43 100644
--- a/include/polarssl/debug.h
+++ b/include/polarssl/debug.h
@@ -52,14 +52,6 @@
 
 /* \} name SECTION: Module settings */
 
-/**
- * \brief   Set the log mode for the debug functions globally
- *          (Default value: POLARSSL_DEBUG_DFL_MODE)
- *
- * \param log_mode  The log mode to use (POLARSSL_DEBUG_LOG_FULL or
- *                                       POLARSSL_DEBUG_LOG_RAW)
- */
-void debug_set_log_mode( int log_mode );
 
 #define SSL_DEBUG_MSG( level, args )                    \
     debug_print_msg( ssl, level, __FILE__, __LINE__, debug_fmt args );
@@ -100,6 +92,24 @@
 extern "C" {
 #endif
 
+/**
+ * \brief   Set the log mode for the debug functions globally
+ *          (Default value: POLARSSL_DEBUG_DFL_MODE)
+ *
+ * \param log_mode      The log mode to use (POLARSSL_DEBUG_LOG_FULL or
+ *                                           POLARSSL_DEBUG_LOG_RAW)
+ */
+void debug_set_log_mode( int log_mode );
+
+/**
+ * \brief   Set the level threshold to handle globally. Messages that have a
+ *          level over the threshold value are ignored.
+ *          (Default value: 0 (No debug))
+ *
+ * \param threshold     maximum level of messages to pass on
+ */
+void debug_set_threshold( int threshold );
+
 char *debug_fmt( const char *format, ... );
 
 void debug_print_msg( const ssl_context *ssl, int level,
diff --git a/library/debug.c b/library/debug.c
index 40a4399..60e39ed 100644
--- a/library/debug.c
+++ b/library/debug.c
@@ -47,12 +47,18 @@
 #endif /* _MSC_VER */
 
 static int debug_log_mode = POLARSSL_DEBUG_DFL_MODE;
+static int debug_threshold = 0;
 
 void debug_set_log_mode( int log_mode )
 {
     debug_log_mode = log_mode;
 }
 
+void debug_set_threshold( int threshold )
+{
+    debug_threshold = threshold;
+}
+
 char *debug_fmt( const char *format, ... )
 {
     va_list argp;
@@ -73,7 +79,7 @@
     char str[512];
     int maxlen = sizeof( str ) - 1;
 
-    if( ssl->f_dbg == NULL )
+    if( ssl->f_dbg == NULL || level > debug_threshold )
         return;
 
     if( debug_log_mode == POLARSSL_DEBUG_LOG_RAW )
@@ -95,7 +101,7 @@
     int maxlen = sizeof( str ) - 1;
     size_t idx = 0;
 
-    if( ssl->f_dbg == NULL )
+    if( ssl->f_dbg == NULL || level > debug_threshold )
         return;
 
     if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
@@ -115,7 +121,7 @@
     char str[512];
     size_t i, maxlen = sizeof( str ) - 1, idx = 0;
 
-    if( ssl->f_dbg == NULL )
+    if( ssl->f_dbg == NULL || level > debug_threshold )
         return;
 
     if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
@@ -169,6 +175,9 @@
     char str[512];
     int maxlen = sizeof( str ) - 1;
 
+    if( ssl->f_dbg == NULL || level > debug_threshold )
+        return;
+
     snprintf( str, maxlen, "%s(X)", text );
     str[maxlen] = '\0';
     debug_print_mpi( ssl, level, file, line, str, &X->X );
@@ -188,7 +197,7 @@
     int j, k, maxlen = sizeof( str ) - 1, zeros = 1;
     size_t i, n, idx = 0;
 
-    if( ssl->f_dbg == NULL || X == NULL )
+    if( ssl->f_dbg == NULL || X == NULL || level > debug_threshold )
         return;
 
     for( n = X->n - 1; n > 0; n-- )
@@ -301,7 +310,7 @@
     char str[1024], prefix[64];
     int i = 0, maxlen = sizeof( prefix ) - 1, idx = 0;
 
-    if( ssl->f_dbg == NULL || crt == NULL )
+    if( ssl->f_dbg == NULL || crt == NULL || level > debug_threshold )
         return;
 
     if( debug_log_mode == POLARSSL_DEBUG_LOG_FULL )
diff --git a/programs/ssl/ssl_client1.c b/programs/ssl/ssl_client1.c
index 270bb62..a61ae64 100644
--- a/programs/ssl/ssl_client1.c
+++ b/programs/ssl/ssl_client1.c
@@ -29,6 +29,7 @@
 #include <stdio.h>
 
 #include "polarssl/net.h"
+#include "polarssl/debug.h"
 #include "polarssl/ssl.h"
 #include "polarssl/entropy.h"
 #include "polarssl/ctr_drbg.h"
@@ -61,11 +62,10 @@
 
 static void my_debug( void *ctx, int level, const char *str )
 {
-    if( level < DEBUG_LEVEL )
-    {
-        fprintf( (FILE *) ctx, "%s", str );
-        fflush(  (FILE *) ctx  );
-    }
+    ((void) level);
+
+    fprintf( (FILE *) ctx, "%s", str );
+    fflush(  (FILE *) ctx  );
 }
 
 int main( int argc, char *argv[] )
@@ -82,6 +82,10 @@
     ((void) argc);
     ((void) argv);
 
+#if defined(POLARSSL_DEBUG_C)
+    debug_set_threshold( DEBUG_LEVEL );
+#endif
+
     /*
      * 0. Initialize the RNG and the session data
      */
diff --git a/programs/ssl/ssl_client2.c b/programs/ssl/ssl_client2.c
index 7c043eb..012403a 100644
--- a/programs/ssl/ssl_client2.c
+++ b/programs/ssl/ssl_client2.c
@@ -36,6 +36,7 @@
 #include "polarssl/certs.h"
 #include "polarssl/x509.h"
 #include "polarssl/error.h"
+#include "polarssl/debug.h"
 
 #if defined(POLARSSL_TIMING_C)
 #include "polarssl/timing.h"
@@ -112,11 +113,10 @@
 
 static void my_debug( void *ctx, int level, const char *str )
 {
-    if( level < opt.debug_level )
-    {
-        fprintf( (FILE *) ctx, "%s", str );
-        fflush(  (FILE *) ctx  );
-    }
+    ((void) level);
+
+    fprintf( (FILE *) ctx, "%s", str );
+    fflush(  (FILE *) ctx  );
 }
 
 /*
@@ -589,6 +589,10 @@
             goto usage;
     }
 
+#if defined(POLARSSL_DEBUG_C)
+    debug_set_threshold( opt.debug_level );
+#endif
+
     if( opt.force_ciphersuite[0] > 0 )
     {
         const ssl_ciphersuite_t *ciphersuite_info;
diff --git a/programs/ssl/ssl_server.c b/programs/ssl/ssl_server.c
index 49c3965..5b86f9b 100644
--- a/programs/ssl/ssl_server.c
+++ b/programs/ssl/ssl_server.c
@@ -40,6 +40,7 @@
 #include "polarssl/ssl.h"
 #include "polarssl/net.h"
 #include "polarssl/error.h"
+#include "polarssl/debug.h"
 
 #if defined(POLARSSL_SSL_CACHE_C)
 #include "polarssl/ssl_cache.h"
@@ -73,11 +74,10 @@
 
 static void my_debug( void *ctx, int level, const char *str )
 {
-    if( level < DEBUG_LEVEL )
-    {
-        fprintf( (FILE *) ctx, "%s", str );
-        fflush(  (FILE *) ctx  );
-    }
+    ((void) level);
+
+    fprintf( (FILE *) ctx, "%s", str );
+    fflush(  (FILE *) ctx  );
 }
 
 int main( int argc, char *argv[] )
@@ -108,6 +108,10 @@
     pk_init( &pkey );
     entropy_init( &entropy );
 
+#if defined(POLARSSL_DEBUG_C)
+    debug_set_threshold( DEBUG_LEVEL );
+#endif
+
     /*
      * 1. Load the certificates and private RSA key
      */
diff --git a/programs/ssl/ssl_server2.c b/programs/ssl/ssl_server2.c
index c578c5b..44f04a4 100644
--- a/programs/ssl/ssl_server2.c
+++ b/programs/ssl/ssl_server2.c
@@ -51,6 +51,7 @@
 #include "polarssl/certs.h"
 #include "polarssl/x509.h"
 #include "polarssl/error.h"
+#include "polarssl/debug.h"
 
 #if defined(POLARSSL_SSL_CACHE_C)
 #include "polarssl/ssl_cache.h"
@@ -137,11 +138,10 @@
 
 static void my_debug( void *ctx, int level, const char *str )
 {
-    if( level < opt.debug_level )
-    {
-        fprintf( (FILE *) ctx, "%s", str );
-        fflush(  (FILE *) ctx  );
-    }
+    ((void) level);
+
+    fprintf( (FILE *) ctx, "%s", str );
+    fflush(  (FILE *) ctx  );
 }
 
 /*
@@ -721,6 +721,10 @@
             goto usage;
     }
 
+#if defined(POLARSSL_DEBUG_C)
+    debug_set_threshold( opt.debug_level );
+#endif
+
     if( opt.force_ciphersuite[0] > 0 )
     {
         const ssl_ciphersuite_t *ciphersuite_info;
diff --git a/tests/suites/test_suite_debug.data b/tests/suites/test_suite_debug.data
index d51445d..9b49f6a 100644
--- a/tests/suites/test_suite_debug.data
+++ b/tests/suites/test_suite_debug.data
@@ -1,3 +1,18 @@
+Debug print msg (threshold 1, level 0)
+debug_print_msg_threshold:1:0:"MyFile":999:"MyFile(0999)\: Text message, 2 == 2\n"
+
+Debug print msg (threshold 1, level 1)
+debug_print_msg_threshold:1:1:"MyFile":999:"MyFile(0999)\: Text message, 2 == 2\n"
+
+Debug print msg (threshold 1, level 2)
+debug_print_msg_threshold:1:2:"MyFile":999:""
+
+Debug print msg (threshold 0, level 1)
+debug_print_msg_threshold:0:1:"MyFile":999:""
+
+Debug print msg (threshold 0, level 5)
+debug_print_msg_threshold:0:5:"MyFile":999:""
+
 Debug print return value #1
 debug_print_ret:POLARSSL_DEBUG_LOG_FULL:"MyFile":999:"Test return value":0:"MyFile(0999)\: Test return value() returned 0 (-0x0000)\n"
 
diff --git a/tests/suites/test_suite_debug.function b/tests/suites/test_suite_debug.function
index 3a5adaf..7717a61 100644
--- a/tests/suites/test_suite_debug.function
+++ b/tests/suites/test_suite_debug.function
@@ -30,6 +30,28 @@
  */
 
 /* BEGIN_CASE */
+void debug_print_msg_threshold( int threshold, int level, char *file, int line,
+                                char *result_str )
+{
+    ssl_context ssl;
+    struct buffer_data buffer;
+
+    memset( &ssl, 0, sizeof( ssl_context ) );
+    memset( buffer.buf, 0, 2000 );
+    buffer.ptr = buffer.buf;
+
+    debug_set_log_mode( POLARSSL_DEBUG_LOG_FULL );
+    debug_set_threshold( threshold );
+    ssl_set_dbg(&ssl, string_debug, &buffer);
+
+    debug_print_msg( &ssl, level, file, line,
+                     debug_fmt("Text message, 2 == %d", 2 ) );
+
+    TEST_ASSERT( strcmp( buffer.buf, result_str ) == 0 );
+}
+/* END_CASE */
+
+/* BEGIN_CASE */
 void debug_print_ret( int mode, char *file, int line, char *text, int value,
                       char *result_str )
 {