Changeset 16 in libcf


Ignore:
Timestamp:
02/01/13 09:37:19 (11 years ago)
Author:
cheese
Message:

#1 add function for non-context debugging message printing

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/cf_debug.h

    r14 r16  
    66
    77#include "cf_base.h"
     8
     9#include <stdio.h>
    810
    911#define CF_ERROR_DEBUG_INVALID_CTX          CF_ERROR_DEBUG - 1
     
    1921# define CF_UPDATE_CTX(__ctx)                                       \
    2022    CF_Debug_UpdateCtx (__ctx,__FILE__,__func__,__LINE__)
     23# define CF_DEBUG_PRINT(__fp,__fmt,...)                             \
     24    CF_Debug_Print (__fp,__FILE__,__func__,__LINE__,__fmt,##__VA_ARGS__)
     25# define CF_DEBUG_PRINT_BIN(__fp,__bin,__len,__fmt,...)             \
     26    CF_Debug_PrintBin (__fp,__FILE__,__func__,__LINE__,__bin,__len,__fmt,##__VA_ARGS__)
    2127# define CF_DEBUG_TRACE(__ctx,__fmt,...)                            \
    2228    do {                                                            \
     
    6369
    6470CF_EXPORT int
     71CF_Debug_Print          (FILE       * fp,
     72                         const char * file,
     73                         const char * func,
     74                         const int  line,
     75                         const char * fmt, ...);
     76
     77int
     78CF_Debug_PrintBin       (FILE                   * fp,
     79                         const char             * file,
     80                         const char             * func,
     81                         const int              line,
     82                         const unsigned char    * bin,
     83                         const int              len,
     84                         const char             * fmt, ...);
     85
     86CF_EXPORT int
    6587CF_Debug_UpdateCtx      (CF_Debug_Ctx   ctx,
    6688                         const char     * file,
  • trunk/include/cf_file.h

    r15 r16  
    1616#define CF_ERROR_FILE_GET_SIZE          CF_ERROR_FILE - 7
    1717
    18 #define RO      O_RDONLY
    19 #define WO      O_WRONLY
    20 #define WR      O_RDWR
    21 #define CR      O_CREAT
    22 #define TR      O_TRUNC
     18typedef enum{
     19    CF_FILE_RO = O_RDONLY,
     20    CF_FILE_WO = O_WRONLY,
     21    CF_FILE_WR = O_RDWR,
     22    CF_FILE_CR = O_CREAT,
     23    CF_FILE_TR = O_TRUNC
     24} E_CF_FILE_FLAG, CF_FILE_FLAG;
    2325
    2426#ifdef __cplusplus
     
    2729
    2830CF_EXPORT int
    29 CF_File_Open            (const char * path,
    30                          const int  flag);
     31CF_File_Open            (const char         * path,
     32                         const CF_FILE_FLAG flag);
    3133
    3234CF_EXPORT int
  • trunk/src/cf_debug.c

    r14 r16  
    55#include "cf_local.h"
    66
    7 #include <stdio.h>
    87#include <stdlib.h>
    98#include <ctype.h>
     
    5049
    5150int
    52 CF_Debug_Local_Print (CF_DEBUG_CTX  * ctx,
     51CF_Debug_Local_Print (FILE          * fp,
     52                      const char    * file,
     53                      const char    * func,
     54                      const int     line,
    5355                      const char    * fmt,
    5456                      va_list       valist)
    5557{
    56     fprintf (GET_CTX_OSTREAM (ctx), "[DEBUG][%s:%d][%s] ",
    57                                     ctx->file,
    58                                     ctx->line,
    59                                     ctx->func);
    60     vfprintf (GET_CTX_OSTREAM (ctx), fmt, valist);
    61 
    62     return CF_OK;
    63 }
    64 
    65 CF_Debug_Ctx
    66 CF_Debug_CreateCtx (void)
    67 {
    68     CF_DEBUG_CTX * ctx = NULL;
    69 
    70     ctx = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1);
    71 
    72     return (CF_Debug_Ctx) ctx;
    73 }
    74 
    75 int
    76 CF_Debug_DestroyCtx (CF_Debug_Ctx ctx)
    77 {
    78     CF_DEBUG_CTX        * context = (CF_DEBUG_CTX *) ctx;
    79     CF_DEBUG_CALLSTACK  * pop = NULL;
    80     CF_DEBUG_CALLSTACK  * next = NULL;
    81 
    82     CHECK_INVALID_CTX (ctx);
    83 
    84     if (context->fp != NULL)
    85         fclose (context->fp);
    86 
    87     for (pop = next = context->callstack.caller ; pop ; pop = next)
    88     {
    89         next = next->caller;
    90         free (pop);
    91     }
    92 
    93     free (context);
    94 
    95     return CF_OK;
    96 }
    97 
    98 int
    99 CF_Debug_SetOutputFD (CF_Debug_Ctx  ctx,
    100                       int           fd)
    101 {
    102     int result = 0;
    103     int dupfd = 0;
    104 
    105     CF_DEBUG_CTX    * context = (CF_DEBUG_CTX *) ctx;
    106     FILE            * fp = NULL;
    107 
    108     CHECK_INVALID_CTX (ctx);
    109 
    110     TRY
    111     {
    112         dupfd = dup (fd);
    113         if (dupfd < 0)
    114         {
    115             result = -1;
    116             TRY_BREAK;
    117         }
    118 
    119         fp = fdopen (dupfd, "a+");
    120         if (fp == NULL)
    121         {
    122             close (dupfd);
    123             result = -2;
    124             TRY_BREAK;
    125         }
    126 
    127         context->fp = fp;
    128     }
    129     CATCH_IF (result < 0)
    130     {
    131         return CF_ERROR_DEBUG_SET_OUTPUT_FD;
    132     }
    133 
    134     return CF_OK;
    135 }
    136 
    137 int
    138 CF_Debug_UpdateCtx (CF_Debug_Ctx    ctx,
    139                     const char      * file,
    140                     const char      * func,
    141                     const int       line)
    142 {
    143     CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
    144 
    145     CHECK_INVALID_CTX (ctx);
    146 
    147     strncpy (context->file, file, strlen (file));
    148     strncpy (context->func, func, strlen (func));
    149     context->line = line;
    150 
    151     return CF_OK;
    152 }
    153 
    154 int
    155 CF_Debug_Trace (CF_Debug_Ctx    ctx,
    156                 const char      * fmt, ...)
    157 {
    158     va_list         valist;
    159 
    160     CHECK_INVALID_CTX (ctx);
    161 
    162     va_start (valist, fmt);
    163     CF_Debug_Local_Print ((CF_DEBUG_CTX *) ctx, fmt, valist);
    164     va_end (valist);
    165 
    166     return CF_OK;
    167 }
    168 
    169 int
    170 CF_Debug_TraceBin (CF_Debug_Ctx         ctx,
    171                    const unsigned char  * bin,
    172                    const int            len,
    173                    const char           * fmt, ...)
    174 {
    175     int     i, j;
    176     va_list valist;
    177     FILE    * fp = NULL;
    178 
    179     CHECK_INVALID_CTX (ctx);
    180     fp = GET_CTX_OSTREAM (ctx);
    181 
    182     va_start (valist, fmt);
    183     CF_Debug_Local_Print ((CF_DEBUG_CTX *) ctx, fmt, valist);
     58    fprintf (fp, "[DEBUG][%s:%d][%s] ", file, line, func);
     59    vfprintf (fp, fmt, valist);
     60
     61    return CF_OK;
     62}
     63
     64int
     65CF_Debug_Local_PrintBin (FILE                   * fp,
     66                         const char             * file,
     67                         const char             * func,
     68                         const int              line,
     69                         const unsigned char    * bin,
     70                         const int              len)
     71{
     72    int i, j;
    18473
    18574    for (i = 0 ; i < len ; i += 16)
    18675    {
    187         fprintf (fp, "[DEBUG][%s %s(%d)] ", __FILE__, __func__, __LINE__);
     76        fprintf (fp, "[DEBUG][%s:%d][%s] ", file, line, func);
    18877        fprintf (fp, "%06x : ", i);
    18978
     
    20897}
    20998
     99CF_Debug_Ctx
     100CF_Debug_CreateCtx (void)
     101{
     102    CF_DEBUG_CTX * ctx = NULL;
     103
     104    ctx = (CF_DEBUG_CTX *) calloc (sizeof (CF_DEBUG_CTX), 1);
     105
     106    return (CF_Debug_Ctx) ctx;
     107}
     108
     109int
     110CF_Debug_DestroyCtx (CF_Debug_Ctx ctx)
     111{
     112    CF_DEBUG_CTX        * context = (CF_DEBUG_CTX *) ctx;
     113    CF_DEBUG_CALLSTACK  * pop = NULL;
     114    CF_DEBUG_CALLSTACK  * next = NULL;
     115
     116    CHECK_INVALID_CTX (ctx);
     117
     118    if (context->fp != NULL)
     119        fclose (context->fp);
     120
     121    for (pop = next = context->callstack.caller ; pop ; pop = next)
     122    {
     123        next = next->caller;
     124        free (pop);
     125    }
     126
     127    free (context);
     128
     129    return CF_OK;
     130}
     131
     132int
     133CF_Debug_SetOutputFD (CF_Debug_Ctx  ctx,
     134                      int           fd)
     135{
     136    int result = 0;
     137    int dupfd = 0;
     138
     139    CF_DEBUG_CTX    * context = (CF_DEBUG_CTX *) ctx;
     140    FILE            * fp = NULL;
     141
     142    CHECK_INVALID_CTX (ctx);
     143
     144    TRY
     145    {
     146        dupfd = dup (fd);
     147        if (dupfd < 0)
     148        {
     149            result = -1;
     150            TRY_BREAK;
     151        }
     152
     153        fp = fdopen (dupfd, "a+");
     154        if (fp == NULL)
     155        {
     156            close (dupfd);
     157            result = -2;
     158            TRY_BREAK;
     159        }
     160
     161        context->fp = fp;
     162    }
     163    CATCH_IF (result < 0)
     164    {
     165        return CF_ERROR_DEBUG_SET_OUTPUT_FD;
     166    }
     167
     168    return CF_OK;
     169}
     170
     171int
     172CF_Debug_Print (FILE        * fp,
     173                const char  * file,
     174                const char  * func,
     175                const int   line,
     176                const char  * fmt, ...)
     177{
     178    va_list valist;
     179
     180    va_start (valist, fmt);
     181    CF_Debug_Local_Print (fp, file, func, line, fmt, valist);
     182    va_end (valist);
     183
     184    return CF_OK;
     185}
     186
     187int
     188CF_Debug_PrintBin (FILE                 * fp,
     189                   const char           * file,
     190                   const char           * func,
     191                   const int            line,
     192                   const unsigned char  * bin,
     193                   const int            len,
     194                   const char           * fmt, ...)
     195{
     196    va_list valist;
     197
     198    va_start (valist, fmt);
     199    CF_Debug_Local_Print (fp, file, func, line, fmt, valist);
     200    va_end (valist);
     201
     202    CF_Debug_Local_PrintBin (fp, file, func, line, bin, len);
     203
     204    return CF_OK;
     205}
     206
     207int
     208CF_Debug_UpdateCtx (CF_Debug_Ctx    ctx,
     209                    const char      * file,
     210                    const char      * func,
     211                    const int       line)
     212{
     213    CF_DEBUG_CTX * context = (CF_DEBUG_CTX *) ctx;
     214
     215    CHECK_INVALID_CTX (ctx);
     216
     217    strncpy (context->file, file, strlen (file));
     218    strncpy (context->func, func, strlen (func));
     219    context->line = line;
     220
     221    return CF_OK;
     222}
     223
     224int
     225CF_Debug_Trace (CF_Debug_Ctx    ctx,
     226                const char      * fmt, ...)
     227{
     228    va_list         valist;
     229    CF_DEBUG_CTX    * context = (CF_DEBUG_CTX *) ctx;
     230
     231    CHECK_INVALID_CTX (ctx);
     232
     233    va_start (valist, fmt);
     234    CF_Debug_Local_Print (GET_CTX_OSTREAM (context),
     235                          context->file,
     236                          context->func,
     237                          context->line,
     238                          fmt, valist);
     239    va_end (valist);
     240
     241    return CF_OK;
     242}
     243
     244int
     245CF_Debug_TraceBin (CF_Debug_Ctx         ctx,
     246                   const unsigned char  * bin,
     247                   const int            len,
     248                   const char           * fmt, ...)
     249{
     250    va_list         valist;
     251    FILE            * fp = NULL;
     252    CF_DEBUG_CTX    * context = (CF_DEBUG_CTX *) ctx;
     253 
     254    CHECK_INVALID_CTX (ctx);
     255
     256    va_start (valist, fmt);
     257    CF_Debug_Local_Print (GET_CTX_OSTREAM (context),
     258                          context->file,
     259                          context->func,
     260                          context->line,
     261                          fmt, valist);
     262    va_end (valist);
     263
     264    CF_Debug_Local_PrintBin (context->fp,
     265                             context->file,
     266                             context->func,
     267                             context->line,
     268                             bin, len);
     269
     270    return CF_OK;
     271}
     272
    210273int
    211274CF_Debug_CallStackPush (CF_Debug_Ctx    ctx,
  • trunk/src/cf_file.c

    r15 r16  
    2525
    2626int
    27 CF_File_Open (const char    * path,
    28               const int     flag)
     27CF_File_Open (const char            * path,
     28              const CF_FILE_FLAG    flag)
    2929{
    3030    int result = open (path, flag);
     
    3939CF_File_Create (const char * path)
    4040{
    41     int result = open (path, CR|WO|TR, FILE_MODE);
     41    int result = open (path, CF_FILE_CR|CF_FILE_WO|CF_FILE_TR, FILE_MODE);
    4242
    4343    if (result < 0)
  • trunk/test/test.c

    r15 r16  
    2525    gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
    2626    if (gDebugCtx == NULL)
    27         CF_DEBUG_TRACE (gDebugCtx, "create debug ctx error\n");
     27        CF_DEBUG_PRINT (stderr, "create debug ctx error\n");
    2828
    2929    for (i = 0 ; i < 10000 ; i++)
     
    4949    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
    5050
    51     fd = CF_File_Open (file, RO);
     51    fd = CF_File_Open (file, CF_FILE_RO);
    5252    if (fd < 0)
    5353        CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
     
    101101
    102102    if (gLogCtx == NULL)
    103         CF_DEBUG_TRACE (gDebugCtx, "create log ctx error\n");
     103        CF_DEBUG_PRINT (stderr, "create log ctx error\n");
    104104
    105105    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
    106106
    107     CF_DEBUG_TRACE (gDebugCtx, " == LOG TEST ==\n");
     107    CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
    108108    log_test ("LOG_TEST");
    109109
    110     CF_DEBUG_TRACE (gDebugCtx, " == FILE READ TEST ==\n");
     110    CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
    111111    file_test ("FILE_READ_TEST");
    112112
    113     CF_DEBUG_TRACE (gDebugCtx, " == CALLSTACK TEST ==\n");
     113    CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
    114114    callee1 ();
    115115
    116116    CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
    117117
    118     CF_DEBUG_TRACE (gDebugCtx, " == END OF TEST ==\n");
     118    CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
    119119    CF_DEBUG_TRACE (gDebugCtx, "here is the end of function [file:%s line:%d func:%s]\n",
    120120                                gDebugCallstack.file,
Note: See TracChangeset for help on using the changeset viewer.