Changeset 16 in libcf for trunk/src


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/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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)
Note: See TracChangeset for help on using the changeset viewer.