Changeset 3752


Ignore:
Timestamp:
Sep 18, 2011 2:38:46 PM (13 years ago)
Author:
bennylp
Message:

Implemented re #1372: New log features: indentation and thread switching indication

Location:
pjproject/trunk/pjlib
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/include/pj/config.h

    r3740 r3752  
    409409#endif 
    410410 
     411/** 
     412 * Enable log indentation feature. 
     413 * 
     414 * Default: 1 
     415 */ 
     416#ifndef PJ_LOG_ENABLE_INDENT 
     417#   define PJ_LOG_ENABLE_INDENT        1 
     418#endif 
     419 
     420/** 
     421 * Number of PJ_LOG_INDENT_CHAR to put every time pj_log_push_indent() 
     422 * is called. 
     423 * 
     424 * Default: 1 
     425 */ 
     426#ifndef PJ_LOG_INDENT_SIZE 
     427#   define PJ_LOG_INDENT_SIZE        1 
     428#endif 
     429 
     430/** 
     431 * Log indentation character. 
     432 * 
     433 * Default: space 
     434 */ 
     435#ifndef PJ_LOG_INDENT_CHAR 
     436#   define PJ_LOG_INDENT_CHAR       '.' 
     437#endif 
    411438 
    412439/** 
  • pjproject/trunk/pjlib/include/pj/log.h

    r3553 r3752  
    8282    PJ_LOG_HAS_COLOR      = 1024, /**< Colorize logs [yes on win32]           */ 
    8383    PJ_LOG_HAS_LEVEL_TEXT = 2048, /**< Include level text string [no]         */ 
    84     PJ_LOG_HAS_THREAD_ID  = 4096  /**< Include thread identification [no]     */ 
     84    PJ_LOG_HAS_THREAD_ID  = 4096, /**< Include thread identification [no]     */ 
     85    PJ_LOG_HAS_THREAD_SWC = 8192, /**< Add mark when thread has switched [yes]*/ 
     86    PJ_LOG_HAS_INDENT     =16384  /**< Indentation. Say yes! [yes]            */ 
    8587}; 
    8688 
     
    204206PJ_DECL(unsigned) pj_log_get_decor(void); 
    205207 
     208/** 
     209 * Add indentation to log message. Indentation will add PJ_LOG_INDENT_CHAR 
     210 * before the message, and is useful to show the depth of function calls. 
     211 * 
     212 * @param indent    The indentation to add or substract. Positive value 
     213 *                  adds current indent, negative value subtracts current 
     214 *                  indent. 
     215 */ 
     216PJ_DECL(void) pj_log_add_indent(int indent); 
     217 
     218/** 
     219 * Push indentation to the right by default value (PJ_LOG_INDENT). 
     220 */ 
     221PJ_DECL(void) pj_log_push_indent(void); 
     222 
     223/** 
     224 * Pop indentation (to the left) by default value (PJ_LOG_INDENT). 
     225 */ 
     226PJ_DECL(void) pj_log_pop_indent(void); 
    206227 
    207228/** 
  • pjproject/trunk/pjlib/src/pj/log.c

    r3553 r3752  
    3232#endif 
    3333 
     34static void *g_last_thread; 
     35 
    3436#if PJ_HAS_THREADS 
    3537static long thread_suspended_tls_id = -1; 
     38#  if PJ_LOG_ENABLE_INDENT 
     39static long thread_indent_tls_id = -1; 
     40#  endif 
     41#endif 
     42 
     43#if !PJ_LOG_ENABLE_INDENT || !PJ_HAS_THREADS 
     44static int log_indent; 
    3645#endif 
    3746 
     
    3948static unsigned log_decor = PJ_LOG_HAS_TIME | PJ_LOG_HAS_MICRO_SEC | 
    4049                            PJ_LOG_HAS_SENDER | PJ_LOG_HAS_NEWLINE | 
    41                             PJ_LOG_HAS_SPACE 
     50                            PJ_LOG_HAS_SPACE | PJ_LOG_HAS_THREAD_SWC | 
     51                            PJ_LOG_HAS_INDENT 
    4252#if defined(PJ_WIN32) && PJ_WIN32!=0 
    4353                            | PJ_LOG_HAS_COLOR 
     
    7282#endif 
    7383 
     84#define LOG_MAX_INDENT          80 
     85 
    7486#if PJ_HAS_THREADS 
    7587static void logging_shutdown(void) 
     
    7991        thread_suspended_tls_id = -1; 
    8092    } 
    81 } 
    82 #endif 
     93#  if PJ_LOG_ENABLE_INDENT 
     94    if (thread_indent_tls_id != -1) { 
     95        pj_thread_local_free(thread_indent_tls_id); 
     96        thread_indent_tls_id = -1; 
     97    } 
     98#  endif 
     99} 
     100#endif  /* PJ_HAS_THREADS */ 
     101 
     102#if PJ_LOG_ENABLE_INDENT && PJ_HAS_THREADS 
     103static void log_set_indent(int indent) 
     104{ 
     105    if (indent < 0) indent = 0; 
     106    pj_thread_local_set(thread_indent_tls_id, (void*)(long)indent); 
     107} 
     108 
     109static int log_get_raw_indent() 
     110{ 
     111    return (long)pj_thread_local_get(thread_indent_tls_id); 
     112} 
     113 
     114#else 
     115static void log_set_indent(int indent) 
     116{ 
     117    log_indent = indent; 
     118    if (log_indent < 0) log_indent = 0; 
     119} 
     120 
     121static int log_get_raw_indent() 
     122{ 
     123    return log_indent; 
     124} 
     125#endif  /* PJ_LOG_ENABLE_INDENT && PJ_HAS_THREADS */ 
     126 
     127static int log_get_indent() 
     128{ 
     129    int indent = log_get_raw_indent(); 
     130    return indent > LOG_MAX_INDENT ? LOG_MAX_INDENT : indent; 
     131} 
     132 
     133PJ_DEF(void) pj_log_add_indent(int indent) 
     134{ 
     135    log_set_indent(log_get_raw_indent() + indent); 
     136} 
     137 
     138PJ_DEF(void) pj_log_push_indent(void) 
     139{ 
     140    pj_log_add_indent(PJ_LOG_INDENT_SIZE); 
     141} 
     142 
     143PJ_DEF(void) pj_log_pop_indent(void) 
     144{ 
     145    pj_log_add_indent(-PJ_LOG_INDENT_SIZE); 
     146} 
    83147 
    84148pj_status_t pj_log_init(void) 
     
    86150#if PJ_HAS_THREADS 
    87151    if (thread_suspended_tls_id == -1) { 
    88         pj_thread_local_alloc(&thread_suspended_tls_id); 
     152        pj_status_t status; 
     153        status = pj_thread_local_alloc(&thread_suspended_tls_id); 
     154        if (status != PJ_SUCCESS) 
     155            return status; 
     156 
     157#  if PJ_LOG_ENABLE_INDENT 
     158        status = pj_thread_local_alloc(&thread_indent_tls_id); 
     159        if (status != PJ_SUCCESS) { 
     160            pj_thread_local_free(thread_suspended_tls_id); 
     161            thread_suspended_tls_id = -1; 
     162            return status; 
     163        } 
     164#  endif 
    89165        pj_atexit(&logging_shutdown); 
    90166    } 
    91167#endif 
     168    g_last_thread = NULL; 
    92169    return PJ_SUCCESS; 
    93170} 
     
    243320    char log_buffer[PJ_LOG_MAX_SIZE]; 
    244321#endif 
    245     int saved_level, len, print_len; 
     322    int saved_level, len, print_len, indent; 
    246323 
    247324    PJ_CHECK_STACK(); 
     
    277354    } 
    278355    if (log_decor & PJ_LOG_HAS_YEAR) { 
    279         *pre++ = ' '; 
     356        if (pre!=log_buffer) *pre++ = ' '; 
    280357        pre += pj_utoa(ptime.year, pre); 
    281358    } 
     
    289366    } 
    290367    if (log_decor & PJ_LOG_HAS_TIME) { 
    291         *pre++ = ' '; 
     368        if (pre!=log_buffer) *pre++ = ' '; 
    292369        pre += pj_utoa_pad(ptime.hour, pre, 2, '0'); 
    293370        *pre++ = ':'; 
     
    303380        enum { SENDER_WIDTH = 14 }; 
    304381        int sender_len = strlen(sender); 
    305         *pre++ = ' '; 
     382        if (pre!=log_buffer) *pre++ = ' '; 
    306383        if (sender_len <= SENDER_WIDTH) { 
    307384            while (sender_len < SENDER_WIDTH) 
     
    335412        *pre++ = ' '; 
    336413 
    337     if (log_decor & PJ_LOG_HAS_SPACE) { 
     414    if (log_decor & PJ_LOG_HAS_THREAD_SWC) { 
     415        void *current_thread = (void*)pj_thread_this(); 
     416        if (current_thread != g_last_thread) { 
     417            *pre++ = '!'; 
     418            g_last_thread = current_thread; 
     419        } else { 
     420            *pre++ = ' '; 
     421        } 
     422    } else if (log_decor & PJ_LOG_HAS_SPACE) { 
    338423        *pre++ = ' '; 
    339424    } 
     425 
     426#if PJ_LOG_ENABLE_INDENT 
     427    if (log_decor & PJ_LOG_HAS_INDENT) { 
     428        indent = log_get_indent(); 
     429        if (indent > 0) { 
     430            pj_memset(pre, PJ_LOG_INDENT_CHAR, indent); 
     431            pre += indent; 
     432        } 
     433    } 
     434#endif 
    340435 
    341436    len = pre - log_buffer; 
Note: See TracChangeset for help on using the changeset viewer.