util/helper.h File Reference

#include <util/basicDefs.h>
#include <pthread.h>

Include dependency graph for helper.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  XmlBlasterReadFromSocketFuncHolder
 Holds a callback function pointer and its user pointer (the 'this' pointer). More...
struct  XmlBlasterWriteToSocketFuncHolder
 Holds a callback function pointer and its user pointer (the 'this' pointer, first argument). More...
struct  BlobHolder
 Holds arbitrary raw data and its length. More...
struct  ExceptionStruct
 Holds error text. More...

Defines

#define XB_USE_PTHREADS   1
#define EXCEPTIONSTRUCT_ERRORCODE_LEN   56
#define EXCEPTIONSTRUCT_MESSAGE_LEN   1024

Typedefs

typedef ssize_t(*) XmlBlasterWriteToSocketFunc (void *xb, const int fd, const char *ptr, const size_t nbytes)
 Declare function pointer to write to socket.
typedef ssize_t(*) XmlBlasterReadFromSocketFunc (void *xb, const int fd, char *ptr, const size_t nbytes, XmlBlasterNumReadFunc fpNumRead, void *userP2)
 Declare function pointer to read from socket.
typedef enum XMLBLASTER_LOG_LEVEL_ENUM XMLBLASTER_LOG_LEVEL
typedef void(*) XmlBlasterLogging (void *logUserP, XMLBLASTER_LOG_LEVEL currLevel, XMLBLASTER_LOG_LEVEL level, const char *location, const char *fmt,...)

Enumerations

enum  XMLBLASTER_LOG_LEVEL_ENUM {
  XMLBLASTER_LOG_ERROR = 1, XMLBLASTER_LOG_WARN = 2, XMLBLASTER_LOG_INFO = 3, XMLBLASTER_LOG_TRACE = 6,
  XMLBLASTER_LOG_DUMP = 7
}

Functions

void initializeExceptionStruct (ExceptionStruct *exception)
 Should be called on any ExceptionStruct before using it.
void embedException (ExceptionStruct *exception, const char *newErrorCode, const char *newMessage, const ExceptionStruct *embed)
const char * getExceptionStr (char *out, int outSize, const ExceptionStruct *exception)
void xmlBlasterDefaultLogging (void *logUserP, XMLBLASTER_LOG_LEVEL currLevel, XMLBLASTER_LOG_LEVEL level, const char *location, const char *fmt,...)
XMLBLASTER_LOG_LEVEL parseLogLevel (const char *logLevelStr)
const char * getLogLevelStr (XMLBLASTER_LOG_LEVEL logLevel)
 
Returns:
A human readable log level, e.g.

int doLog (XMLBLASTER_LOG_LEVEL currLevel, XMLBLASTER_LOG_LEVEL level)
 Check if logging is necessary.
char * getStackTrace (int maxNumOfLines)
 Add for GCC compilation: "-rdynamic -export-dynamic -D_ENABLE_STACK_TRACE_".
void sleepMillis (long millis)
 Sleep for given milliseconds, on none real time systems expect ~ 10 millisecs tolerance.
int64_t getTimestamp (void)
 Create a timestamp in nano seconds elapsed since 1970-01-01.
int getAbsoluteTime (long relativeTimeFromNow, struct timespec *abstime)
 Fills the given abstime with absolute time, using the given timeout relativeTimeFromNow in milliseconds On Linux < 2.5.64 does not support high resolution timers clock_gettime(), but patches are available at http://sourceforge.net/projects/high-res-timers.
void getCurrentTimeStr (char *timeStr, int bufSize)
 Get a human readable time string for logging.
char ** convertWcsArgv (wchar_t **argv_wcs, int argc)
 Converts the given wide char pwcs to multibyte argv.
void freeArgv (char **argv, int argc)
 Frees the allocated argv from convertWcsArgv().
char * strFromBlobAlloc (const char *blob, const size_t len)
char * strcpyAlloc (const char *src)
char * strcpyRealloc (char **dest, const char *src)
char * strcatAlloc (char **dest, const char *src)
char * strncpy0 (char *const to, const char *const from, const size_t maxLen)
char * strncat0 (char *const to, const char *const from, const size_t max)
int snprintf0 (char *buffer, size_t sizeOfBuffer, const char *format,...)
void trim (char *s)
 strip leading and trailing spaces of the given string
void trimStart (char *s)
 strip leading spaces of the given string
void trimEnd (char *s)
 strip trailing spaces of the given string
void xb_strerror (char *errnoStr, size_t sizeInBytes, int errnum)
 Convert the errnum to a human readable errnoStr.
char * toReadableDump (char *data, size_t len)
 Converts the given binary data to a more readable string, the zero bytes are replaced by '*'.
const char * int64ToStr (char *const buf, int64_t val)
 Convert a 64 bit integer to a string.
int strToInt64 (int64_t *val, const char *const str)
int strToLong (long *val, const char *const str)
int strToULong (unsigned long *val, const char *const str)
int strToInt (int *val, const char *const str)
BlobHolderblobcpyAlloc (BlobHolder *blob, const char *data, size_t dataLen)
BlobHolderfreeBlobHolderContent (BlobHolder *blob)
 free()'s the data in the given blob, does not free the blob itself.
char * blobDump (BlobHolder *blob)
 Converts the given binary data to a more readable string, the zero bytes are replaced by '*'.
void freeBlobDump (char *blobDump)
long get_pthread_id (pthread_t t)
 Cast the thread identifier to an long value.


Define Documentation

#define EXCEPTIONSTRUCT_ERRORCODE_LEN   56

Definition at line 80 of file helper.h.

Referenced by checkArgs(), compilePreparedQuery(), embedException(), execSilent(), interceptUpdate(), parseCacheInfo(), parseQueueEntryArr(), persistentQueueDestroy(), persistentQueueInitialize(), persistentQueuePeekWithSamePriority(), and persistentQueuePut().

#define EXCEPTIONSTRUCT_MESSAGE_LEN   1024

Definition at line 81 of file helper.h.

Referenced by checkArgs(), compilePreparedQuery(), createCallbackServer(), embedException(), execSilent(), interceptUpdate(), parseCacheInfo(), parseQueueEntryArr(), parseSocketData(), persistentQueueDestroy(), persistentQueueInitialize(), persistentQueuePeekWithSamePriority(), and persistentQueuePut().

#define XB_USE_PTHREADS   1

Definition at line 25 of file helper.h.


Typedef Documentation

typedef enum XMLBLASTER_LOG_LEVEL_ENUM XMLBLASTER_LOG_LEVEL

typedef void( * ) XmlBlasterLogging(void *logUserP, XMLBLASTER_LOG_LEVEL currLevel, XMLBLASTER_LOG_LEVEL level, const char *location, const char *fmt,...)

Definition at line 107 of file helper.h.

typedef ssize_t( * ) XmlBlasterReadFromSocketFunc(void *xb, const int fd, char *ptr, const size_t nbytes, XmlBlasterNumReadFunc fpNumRead, void *userP2)

Declare function pointer to read from socket.

Definition at line 37 of file helper.h.

typedef ssize_t( * ) XmlBlasterWriteToSocketFunc(void *xb, const int fd, const char *ptr, const size_t nbytes)

Declare function pointer to write to socket.

Definition at line 35 of file helper.h.


Enumeration Type Documentation

enum XMLBLASTER_LOG_LEVEL_ENUM

Enumerator:
XMLBLASTER_LOG_ERROR  supported, use for programming errors
XMLBLASTER_LOG_WARN  supported, use for user errors and wrong configurations
XMLBLASTER_LOG_INFO  supported, use for success information only
XMLBLASTER_LOG_TRACE  supported, use for debugging purposes
XMLBLASTER_LOG_DUMP  supported, use for debugging purposes

Definition at line 96 of file helper.h.


Function Documentation

BlobHolder* blobcpyAlloc ( BlobHolder blob,
const char *  data,
size_t  dataLen 
)

char* blobDump ( BlobHolder blob  ) 

Converts the given binary data to a more readable string, the zero bytes are replaced by '*'.

Parameters:
blob The binary data
Returns:
readable is returned, it must be free()'d

Definition at line 984 of file helper.c.

References BlobHolder::data, BlobHolder::dataLen, and toReadableDump().

Referenced by sendData().

Here is the call graph for this function:

char** convertWcsArgv ( wchar_t **  argv_wcs,
int  argc 
)

Converts the given wide char pwcs to multibyte argv.

Call freeWcsArgv() to free the memory again.

Parameters:
pwcs In parameter: Wide char command line arguments
argc The number of strings in pwcs
Returns:
argv Is allocated with malloc and holds all given pwcs strings

Definition at line 224 of file helper.c.

References argv.

int doLog ( XMLBLASTER_LOG_LEVEL  currLevel,
XMLBLASTER_LOG_LEVEL  level 
)

Check if logging is necessary.

Parameters:
currLevel The actual log level of the client
level The level of this log entry
Returns:
true If logging is desired

Definition at line 811 of file helper.c.

void embedException ( ExceptionStruct exception,
const char *  newErrorCode,
const char *  newMessage,
const ExceptionStruct embed 
)

void freeArgv ( char **  argv,
int  argc 
)

Frees the allocated argv from convertWcsArgv().

Parameters:
argv The main(argv)
argc The number of strings in argv

Definition at line 249 of file helper.c.

Referenced by main().

void freeBlobDump ( char *  blobDump  ) 

Definition at line 989 of file helper.c.

Referenced by sendData().

BlobHolder* freeBlobHolderContent ( BlobHolder blob  ) 

free()'s the data in the given blob, does not free the blob itself.

Parameters:
blob if NULL we return NULL
Returns:
The given blob

Definition at line 967 of file helper.c.

References BlobHolder::data, and BlobHolder::dataLen.

Referenced by handleMessage(), postSendEvent(), sendData(), xmlBlasterConnect(), xmlBlasterDisconnect(), xmlBlasterErase(), xmlBlasterGet(), xmlBlasterPing(), xmlBlasterPublish(), xmlBlasterSubscribe(), and xmlBlasterUnSubscribe().

long get_pthread_id ( pthread_t  t  ) 

Cast the thread identifier to an long value.

Parameters:
t The pthread_t type
Returns:
A uniquue long, usually the pointer address

Definition at line 648 of file helper.c.

Referenced by freeXmlBlasterAccessUnparsed(), interceptUpdate(), runUpdate(), and xmlBlasterDefaultLogging().

int getAbsoluteTime ( long  relativeTimeFromNow,
struct timespec *  abstime 
)

Fills the given abstime with absolute time, using the given timeout relativeTimeFromNow in milliseconds On Linux < 2.5.64 does not support high resolution timers clock_gettime(), but patches are available at http://sourceforge.net/projects/high-res-timers.

Parameters:
relativeTimeFromNow the relative time from now in milliseconds
abstime 
Returns:
true If implemented

Definition at line 266 of file helper.c.

References NANO_SECS_PER_SECOND.

Referenced by getTimestamp(), and postSendEvent().

void getCurrentTimeStr ( char *  timeStr,
int  bufSize 
)

Get a human readable time string for logging.

Parameters:
timeStr out parameter, e.g. "2006-11-14 12:34:46"
bufSize The size of timeStr

Definition at line 663 of file helper.c.

References snprintf0(), and strncpy0().

Referenced by xmlBlasterDefaultLogging().

Here is the call graph for this function:

const char* getExceptionStr ( char *  out,
int  outSize,
const ExceptionStruct exception 
)

const char* getLogLevelStr ( XMLBLASTER_LOG_LEVEL  logLevel  ) 

Returns:
A human readable log level, e.g.

"ERROR"

Definition at line 800 of file helper.c.

References LOG_TEXT.

Referenced by getXmlBlasterAccessUnparsed(), and myLogger().

char* getStackTrace ( int  maxNumOfLines  ) 

Add for GCC compilation: "-rdynamic -export-dynamic -D_ENABLE_STACK_TRACE_".

Returns:
The stack trace, you need to free() it. Returns NULL if out of memory.

Definition at line 94 of file helper.c.

References strcatAlloc(), and strcpyAlloc().

Referenced by checkArgs(), freeXmlBlasterAccessUnparsed(), and xmlBlasterDefaultLogging().

Here is the call graph for this function:

int64_t getTimestamp ( void   ) 

Create a timestamp in nano seconds elapsed since 1970-01-01.

The timestamp is guaranteed to be ascending and unique.

Definition at line 199 of file helper.c.

References getAbsoluteTime(), and NANO_SECS_PER_SECOND.

Referenced by myUpdate().

Here is the call graph for this function:

void initializeExceptionStruct ( ExceptionStruct exception  ) 

Should be called on any ExceptionStruct before using it.

Nulls all fields

Definition at line 843 of file helper.c.

References ExceptionStruct::errorCode, ExceptionStruct::message, and ExceptionStruct::remote.

Referenced by checkArgs().

const char* int64ToStr ( char *  buf,
int64_t  val 
)

Convert a 64 bit integer to a string.

This helper concentrates this conversion to one place to simplify porting to compilers with no int64_t = long long support

Parameters:
buf You need to pass this buffer with at least INT64_STRLEN_MAX=22 bytes of size
Returns:
buf

< Size of a max int64_t dumped to a string including an optional LL and termination '': LLONG_MAX from limits.h 9223372036854775807LL

Definition at line 874 of file helper.c.

References INT64_STRLEN_MAX, PRINTF_PREFIX_INT64_T, and snprintf0().

Referenced by fillCache(), parseQueueEntryArr(), persistentQueuePeekWithSamePriority(), persistentQueuePut(), persistentQueueRandomRemove(), queueEntryToXml(), test_overflow(), and test_queue().

Here is the call graph for this function:

XMLBLASTER_LOG_LEVEL parseLogLevel ( const char *  logLevelStr  ) 

void sleepMillis ( long  millis  ) 

Sleep for given milliseconds, on none real time systems expect ~ 10 millisecs tolerance.

Definition at line 150 of file helper.c.

Referenced by compilePreparedQuery(), freeXmlBlasterAccessUnparsed(), getResultRows(), main(), and myUpdate().

int snprintf0 ( char *  buffer,
size_t  sizeOfBuffer,
const char *  format,
  ... 
)

char* strcatAlloc ( char **  dest,
const char *  src 
)

char* strcpyAlloc ( const char *  src  ) 

char* strcpyRealloc ( char **  dest,
const char *  src 
)

char* strFromBlobAlloc ( const char *  blob,
const size_t  len 
)

char* strncat0 ( char *const   to,
const char *const  from,
const size_t  max 
)

char* strncpy0 ( char *const   to,
const char *const  from,
const size_t  maxLen 
)

int strToInt ( int *  val,
const char *const  str 
)

int strToInt64 ( int64_t *  val,
const char *const  str 
)

int strToLong ( long *  val,
const char *const  str 
)

int strToULong ( unsigned long *  val,
const char *const  str 
)

char* toReadableDump ( char *  data,
size_t  len 
)

Converts the given binary data to a more readable string, the zero bytes are replaced by '*'.

Parameters:
data The data to convert
len The length of the binary data
Returns:
readable is returned, it must be free()'d. If allocation fails NULL is returned

Definition at line 623 of file helper.c.

Referenced by blobDump(), encodeSocketMessage(), and parseSocketData().

void trim ( char *  s  ) 

strip leading and trailing spaces of the given string

Definition at line 540 of file helper.c.

Referenced by convertToXmlBlasterException(), parseMsgUnitArr(), and parseSocketData().

void trimEnd ( char *  s  ) 

strip trailing spaces of the given string

Definition at line 604 of file helper.c.

void trimStart ( char *  s  ) 

strip leading spaces of the given string

Definition at line 576 of file helper.c.

void xb_strerror ( char *  errnoStr,
size_t  sizeInBytes,
int  errnum 
)

Convert the errnum to a human readable errnoStr.

Parameters:
errnoStr Out parameter holding the string
sizeInBytes Size of the buffer
errnum The error number (errno)

Definition at line 437 of file helper.c.

References snprintf0(), and strncpy0().

Referenced by initConnection().

Here is the call graph for this function:

void xmlBlasterDefaultLogging ( void *  logUserP,
XMLBLASTER_LOG_LEVEL  currLevel,
XMLBLASTER_LOG_LEVEL  level,
const char *  location,
const char *  fmt,
  ... 
)