Changeset 185 for trunk/lib/regfi.c


Ignore:
Timestamp:
03/22/10 22:22:07 (14 years ago)
Author:
tim
Message:

reworked logging API again to simplify interface

updated regfi-threadtest to work with more recent commits

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/regfi.c

    r184 r185  
    4545
    4646
     47/* Ensures regfi_init runs only once */
     48static pthread_once_t regfi_init_once = PTHREAD_ONCE_INIT;
     49
     50
    4751
    4852/******************************************************************************
    4953 ******************************************************************************/
    50 void regfi_log_start(uint16_t msg_mask)
    51 {
     54void regfi_log_free(void* ptr)
     55{
     56  REGFI_LOG* log_info = (REGFI_LOG*)ptr;
     57 
     58  if(log_info->messages != NULL)
     59    free(log_info->messages);
     60
     61  talloc_free(log_info);
     62}
     63
     64
     65/******************************************************************************
     66 ******************************************************************************/
     67void regfi_init()
     68{
     69  int err;
     70  if((err = pthread_key_create(&regfi_log_key, regfi_log_free)) != 0)
     71    fprintf(stderr, "ERROR: key_create: %s\n", strerror(err));
     72  errno = err;
     73}
     74
     75
     76/******************************************************************************
     77 ******************************************************************************/
     78REGFI_LOG* regfi_log_new()
     79{
     80  int err;
    5281  REGFI_LOG* log_info = talloc(NULL, REGFI_LOG);
    5382  if(log_info == NULL)
    54     return;
    55 
    56   log_info->msg_mask = msg_mask;
     83    return NULL;
     84
     85  log_info->msg_mask = REGFI_DEFAULT_LOG_MASK;
    5786  log_info->messages = NULL;
    5887
    59   /* XXX: Should we bother with a destructor here? */
    60   if(pthread_key_create(&REGFI_LOG_KEY, NULL) != 0)
    61   {
    62     fprintf(stderr, "ERROR: key_create\n");
    63     goto fail;
    64   }
    65 
    66   if(pthread_setspecific(REGFI_LOG_KEY, log_info) != 0)
    67   {
    68     fprintf(stderr, "ERROR: setspecific\n");
    69     goto fail;
    70   }
    71 
    72   return;
     88  pthread_once(&regfi_init_once, regfi_init);
     89
     90  if((err = pthread_setspecific(regfi_log_key, log_info)) != 0)
     91  {
     92    fprintf(stderr, "ERROR: setspecific: %s\n", strerror(err));
     93    goto fail;
     94  }
     95
     96  return log_info;
    7397
    7498 fail:
    7599  talloc_free(log_info);
     100  errno = err;
     101  return NULL;
    76102}
    77103
     
    93119  va_list args;
    94120
    95   log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
    96   if(log_info == NULL || (log_info->msg_mask & msg_type) == 0)
     121  log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key);
     122  if(log_info == NULL && (log_info = regfi_log_new()) == NULL)
     123    return;
     124
     125  if((log_info->msg_mask & msg_type) == 0)
    97126    return;
    98127
     
    138167{
    139168  char* ret_val;
    140   REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
    141   if(log_info == NULL)
    142     return NULL;
    143 
     169  REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key);
     170  if(log_info == NULL && (log_info = regfi_log_new()) == NULL)
     171    return NULL;
     172 
    144173  ret_val = log_info->messages;
    145174  log_info->messages = NULL;
     
    151180/******************************************************************************
    152181 ******************************************************************************/
    153 void regfi_log_set_mask(uint16_t msg_mask)
    154 {
    155   REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
    156   if(log_info == NULL)
    157     return;
     182bool regfi_log_set_mask(uint16_t msg_mask)
     183{
     184  REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(regfi_log_key);
     185  if(log_info == NULL && (log_info = regfi_log_new()) == NULL)
     186  {
     187      return false;
     188  }
    158189
    159190  log_info->msg_mask = msg_mask;
    160 }
    161 
    162 
    163 /******************************************************************************
    164  ******************************************************************************/
    165 void regfi_log_stop()
    166 {
    167   REGFI_LOG* log_info = (REGFI_LOG*)pthread_getspecific(REGFI_LOG_KEY);
    168   if(log_info == NULL)
    169     return;
    170  
    171   if(log_info->messages != NULL)
    172     free(log_info->messages);
    173 
    174   pthread_key_delete(REGFI_LOG_KEY);
    175   talloc_free(log_info);
     191  return true;
    176192}
    177193
Note: See TracChangeset for help on using the changeset viewer.