Simple utilities sink - stuff that doesn't fit anywhere else / objects in plain C Snapshot
Defines | Enumerations | Functions | Variables
library.

Defines

#define MLOG_ERROR(...)   do { if (m_current_log_level >= MLOG_LEVEL_ERROR) { MLOG_printf( MLOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);
#define MLOG_WARN(...)   do { if (m_current_log_level >= MLOG_LEVEL_WARN) { MLOG_printf( MLOG_LEVEL_WARN, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);
#define MLOG_INFO(...)   do { if (m_current_log_level >= MLOG_LEVEL_INFO) { MLOG_printf( MLOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);
#define MLOG_DEBUG(...)   do { if (m_current_log_level >= MLOG_LEVEL_DEBUG) { MLOG_printf( MLOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);
#define MLOG_TRACE(...)   do { if (m_current_log_level >= MLOG_LEVEL_TRACE) { MLOG_printf( MLOG_LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);

Enumerations

enum  MLOG_LEVEL {
  MLOG_LEVEL_TURN_OFF_LOGGING, MLOG_LEVEL_ERROR, MLOG_LEVEL_WARN, MLOG_LEVEL_INFO,
  MLOG_LEVEL_DEBUG, MLOG_LEVEL_TRACE
}
 defined logging levels More...
enum  MLOG_ACTION { MLOG_ACTION_TO_FILE = 0x1, MLOG_ACTION_SYSLOG_TRACE = 0x2, MLOG_ACTION_CONSOLE = 0x4 }
 actions for logging stuff More...
enum  MLOG_PREFIX { MLOG_PREFIX_LOG_LEVEL = 0x1, MLOG_PREFIX_SOURCE_FILE = 0x2, MLOG_PREFIX_TIME = 0x4 }
 features to include in logging prefix More...
enum  MLOG_ALLOC { MLOG_ALLOC_STACK, MLOG_ALLOC_HEAP, MLOG_ALLOC_TLS_HEAP }
 how to allocation the scrach pad buffer for logging More...

Functions

int MLOG_init (MLOG_LEVEL current, MLOG_ACTION action, void *arg)
 explicitly initialises logging.
void MLOG_set_prefix_features (int prefix)
 sets level of detail added to prefix of log entry (A bitmask of MLOG_PREFIX values)
int MLOG_alloc_option (MLOG_ALLOC option, size_t size)
 determines memory allocation stragegy used here
void MLOG_dump_stack_level (MLOG_LEVEL stack_dump_level)
 set log level for which the current stack is dumped. By default this would happen for LOG_LEVEL_ERROR
int MLOG_printf (MLOG_LEVEL current, const char *file, int line, const char *format,...)
 create a log entry, do not use this function directly, use MLOG_<LOG_LEVEL_NAME> macros instead

Variables

MLOG_LEVEL m_current_log_level

Detailed Description

Not so simple logging subsystem - one logging category is supported (one global log level). Otherwise this is a very versatile logging subsystem.


Define Documentation

#define MLOG_DEBUG (   ...)    do { if (m_current_log_level >= MLOG_LEVEL_DEBUG) { MLOG_printf( MLOG_LEVEL_DEBUG, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);

Definition at line 105 of file logg.h.

#define MLOG_ERROR (   ...)    do { if (m_current_log_level >= MLOG_LEVEL_ERROR) { MLOG_printf( MLOG_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);

Definition at line 99 of file logg.h.

#define MLOG_INFO (   ...)    do { if (m_current_log_level >= MLOG_LEVEL_INFO) { MLOG_printf( MLOG_LEVEL_INFO, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);

Definition at line 103 of file logg.h.

#define MLOG_TRACE (   ...)    do { if (m_current_log_level >= MLOG_LEVEL_TRACE) { MLOG_printf( MLOG_LEVEL_TRACE, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);

Definition at line 107 of file logg.h.

#define MLOG_WARN (   ...)    do { if (m_current_log_level >= MLOG_LEVEL_WARN) { MLOG_printf( MLOG_LEVEL_WARN, __FILE__, __LINE__, __VA_ARGS__ ); } } while(0);

Definition at line 101 of file logg.h.


Enumeration Type Documentation

actions for logging stuff

Enumerator:
MLOG_ACTION_TO_FILE 
MLOG_ACTION_SYSLOG_TRACE 

log entries are dumped to file

MLOG_ACTION_CONSOLE 

log entries are dumped to one syslog level

Definition at line 31 of file logg.h.

             {
  MLOG_ACTION_TO_FILE  = 0x1, /** log entries are dumped to file */
  MLOG_ACTION_SYSLOG_TRACE = 0x2, /** log entries are dumped to one syslog level */
  MLOG_ACTION_CONSOLE = 0x4, /** log entries are dumped to console */

} MLOG_ACTION;
enum MLOG_ALLOC

how to allocation the scrach pad buffer for logging

Enumerator:
MLOG_ALLOC_STACK 
MLOG_ALLOC_HEAP 

buffer for logging allocated on stack (alloca)

MLOG_ALLOC_TLS_HEAP 

buffer for logging allocated on heap (pre log request)

Definition at line 53 of file logg.h.

             {
  MLOG_ALLOC_STACK,     /** buffer for logging allocated on stack (alloca) */
  MLOG_ALLOC_HEAP,      /** buffer for logging allocated on heap (pre log request) */
  MLOG_ALLOC_TLS_HEAP,  /** buffer for logging allocated on heap once for each operating system thread, stored in thread local storage */

} MLOG_ALLOC;
enum MLOG_LEVEL

defined logging levels

Enumerator:
MLOG_LEVEL_TURN_OFF_LOGGING 
MLOG_LEVEL_ERROR 

setting this level as currrent level will turn off logging

MLOG_LEVEL_WARN 

logging level for errors

MLOG_LEVEL_INFO 

logging level for warnings

MLOG_LEVEL_DEBUG 

logging level for info messages

MLOG_LEVEL_TRACE 

logging level for debug messages

Definition at line 18 of file logg.h.

             {
  MLOG_LEVEL_TURN_OFF_LOGGING,  /** setting this level as currrent level will turn off logging */
  MLOG_LEVEL_ERROR, /** logging level for errors */
  MLOG_LEVEL_WARN,  /** logging level for warnings */
  MLOG_LEVEL_INFO, /** logging level for info messages */
  MLOG_LEVEL_DEBUG, /** logging level for debug messages */
  MLOG_LEVEL_TRACE, /** logging level for trace messages */

} MLOG_LEVEL;

features to include in logging prefix

Enumerator:
MLOG_PREFIX_LOG_LEVEL 
MLOG_PREFIX_SOURCE_FILE 

add log level to entry

MLOG_PREFIX_TIME 

add source file / line number of log statement to entry

Definition at line 42 of file logg.h.

             {
  MLOG_PREFIX_LOG_LEVEL = 0x1,    /** add log level to entry */
  MLOG_PREFIX_SOURCE_FILE = 0x2,  /** add source file / line number of log statement to entry */
  MLOG_PREFIX_TIME = 0x4,         /** add HOUR:MINUTE:SECOND:MICROSECOND to log entry */

} MLOG_PREFIX;

Function Documentation

int MLOG_alloc_option ( MLOG_ALLOC  option,
size_t  size 
)

determines memory allocation stragegy used here

Parameters:
option- how to allocate buffer for logging. options are: use stack (MLOG_ALLOC_STACK), heap (MLOG_ALLOC_HEAP), heap and store in tls (MLOG_ALLOC_TLS_HEAP)
size- maximum length of a log entry allowed here.

Definition at line 57 of file logg.c.

{
  alloc_strategy = option;
  alloc_size = size;


  if (alloc_strategy == MLOG_ALLOC_TLS_HEAP) {
    if (log_tls_entry == 0) {
      if (pthread_key_create( &log_tls_entry, tls_free )) {
        return -1;
      }
      pthread_setspecific( log_tls_entry, 0 );
    }
  }
  return 0;
}
void MLOG_dump_stack_level ( MLOG_LEVEL  stack_dump_level)

set log level for which the current stack is dumped. By default this would happen for LOG_LEVEL_ERROR

Definition at line 74 of file logg.c.

{
  stack_dump_level = level;
}
int MLOG_init ( MLOG_LEVEL  current,
MLOG_ACTION  action,
void *  arg 
)

explicitly initialises logging.

Definition at line 43 of file logg.c.

{
  m_current_log_level = current;
 
  log_action |= action;

  if (action & MLOG_ACTION_TO_FILE) {
    log_fd = open( (const char *) arg , O_CREAT | O_RDWR, S_IWUSR  | S_IRUSR );
    return log_fd == -1;
  }
  return 0;
}
int MLOG_printf ( MLOG_LEVEL  current,
const char *  file,
int  line,
const char *  format,
  ... 
)

create a log entry, do not use this function directly, use MLOG_<LOG_LEVEL_NAME> macros instead

Definition at line 80 of file logg.c.

{
  char *sbuf,*pos;
  size_t buf_size, msg_len;
  va_list vlist;
  struct timeval now_time_val;
  struct tm now_time;
  int rt;

  switch(alloc_strategy) {
    case MLOG_ALLOC_STACK:
      sbuf = alloca( alloc_size );
      break;
    case MLOG_ALLOC_HEAP:
      sbuf = (char *) malloc( alloc_size );
      if (!sbuf) {
        return -1;
      }
      break;
    case MLOG_ALLOC_TLS_HEAP:
      sbuf = pthread_getspecific( log_tls_entry );
      if (!sbuf) {
        sbuf = malloc( alloc_size );
        if (!sbuf) {
          return -1;
        }
        pthread_setspecific( log_tls_entry, sbuf );
      }
      break;
  }
  
  buf_size = alloc_size - 2;
  pos = sbuf;

  if (log_prefix_level & MLOG_PREFIX_LOG_LEVEL && buf_size > 0) {
    rt = snprintf( pos, buf_size, "%s : ", log_level_names[ current ] );
    if (rt < 0) {
      return -1;
    }
    pos += rt;
    buf_size -= rt;
  }
  
  if (log_prefix_level & MLOG_PREFIX_SOURCE_FILE && buf_size > 0) {
    rt = snprintf( pos, buf_size, "[%s:%d] ", file, line  );
    if (rt < 0) {
      return -1;
    }
    pos += rt;
    buf_size -= rt;
   }
  
  if (log_prefix_level & MLOG_PREFIX_TIME && buf_size > 0) {
    gettimeofday( &now_time_val, 0 );
    localtime_r(  &now_time_val.tv_sec, &now_time);
          
    rt = snprintf( pos, buf_size, "%02d:%02d:%02d:%06ld ",
                                        (int)  now_time.tm_hour,
                                        (int)  now_time.tm_min,
                                        (int)  now_time.tm_sec,
                                        now_time_val.tv_usec );
    if (rt < 0) {
      return -1;
    }
    pos += rt;
    buf_size -= rt;
  }

  if (buf_size > 0) {
    va_start( vlist, format );
    rt = vsnprintf( pos, buf_size, format, vlist );
    va_end( vlist );
    if (rt < 0) {
      return -1;
    }
    pos += rt;
    buf_size -= rt;
  }
  
  strcpy( pos, "\n");
  msg_len = alloc_size - buf_size - 1;
  
  if (log_action & MLOG_ACTION_CONSOLE) {
    write( 2, sbuf, msg_len );
  } 


  if (log_action & MLOG_ACTION_TO_FILE) {
    write( log_fd, sbuf, msg_len );
  }

  if (log_action & MLOG_ACTION_SYSLOG_TRACE) {
    syslog( LOG_USER, "%s", sbuf );
  }
  
  if (current <= stack_dump_level) {
    if (!errorp_is_file_open()) {
       errorp_open_file( "err.log" );
    }
    error_dump_string( sbuf, sbuf, alloc_size );
  }

  switch(alloc_strategy) {
    case MLOG_ALLOC_TLS_HEAP:
    case MLOG_ALLOC_STACK:
      break;
    case MLOG_ALLOC_HEAP:
      free(sbuf);
      break;
  }
  return 0;
} 
void MLOG_set_prefix_features ( int  prefix)

sets level of detail added to prefix of log entry (A bitmask of MLOG_PREFIX values)


Variable Documentation

Definition at line 14 of file logg.c.