regify utility  2.0.0-0
Logging Facility

This section contains logging specific APIs. More...

Macros

#define RU_LOG_NONE   0
 This level disables logging altogether. More...
 
#define RU_LOG_CRIT   1
 This level reduces logging to fatal errors and generally unexpected serious stuff. More...
 
#define RU_LOG_WARN   2
 This level includes RU_LOG_CRIT and general warning messages. More...
 
#define RU_LOG_INFO   3
 This level includes RU_LOG_WARN and general information. Usual low quantity. More...
 
#define RU_LOG_VERB   4
 This is the verbose logging level used for debugging and usually high quantity. More...
 
#define RU_LOG_DBUG   5
 This is a debugging logging level used for debugging things that are normally assumed to be working. It is usually off because it litters the logs with often redundant data. More...
 
#define ruCritLog(msg)   ruLog_(RU_LOG_CRIT, "%s", msg)
 Emits a CRIT level log message. More...
 
#define ruCritLogf(fmt, ...)   ruLog_(RU_LOG_CRIT, fmt, __VA_ARGS__)
 Emits a CRIT level log message. More...
 
#define ruWarnLog(msg)   ruLog_(RU_LOG_WARN, "%s", msg)
 Emits a WARN level log message. More...
 
#define ruWarnLogf(fmt, ...)   ruLog_(RU_LOG_WARN, fmt, __VA_ARGS__)
 Emits a WARN level log message. More...
 
#define ruInfoLog(msg)   ruLog_(RU_LOG_INFO, "%s", msg)
 Emits a INFO level log message. More...
 
#define ruInfoLogf(fmt, ...)   ruLog_(RU_LOG_INFO, fmt, __VA_ARGS__)
 Emits a INFO level log message. More...
 
#define ruVerbLog(msg)   ruLog_(RU_LOG_VERB, "%s", msg)
 Emits a VERB level log message. More...
 
#define ruVerbLogf(fmt, ...)   ruLog_(RU_LOG_VERB, fmt, __VA_ARGS__)
 Emits a VERB level log message. More...
 
#define ruDbgLog(msg)   ruLog_(RU_LOG_DBUG, "%s", msg)
 Emits a DBUG level log message. More...
 
#define ruDbgLogf(fmt, ...)   ruLog_(RU_LOG_DBUG, fmt, __VA_ARGS__)
 Emits a DBUG level log message. More...
 
#define ruDump8(msg, start)
 Debug logs the 8 bytes following start preceded by given message. More...
 
#define ruDump16(msg, start)
 Debug logs the 16 bytes following start preceded by given message. More...
 

Typedefs

typedef void(* ruLogFunc) (perm_ptr userData, uint32_t logLevel, trans_chars msg)
 The type of function to pass to ruSetLogger in order to hook up logging. More...
 
typedef void(* ruCloseFunc) (perm_ptr userData)
 The type of function to pass to ruSinkCtxNew which will be called when ruFileLogSink has closed its logfile. More...
 
typedef ptr ruPreCtx
 Opaque pointer to ruPreLogSink object. More...
 
typedef ptr ruSinkCtx
 Opaque pointer to ruFileLogSink object. More...
 

Functions

RUAPI ruPreCtx ruPreCtxNew (void)
 Creates a new pre logging context for use with ruPreLogSink. More...
 
RUAPI ruPreCtx ruPreCtxFree (ruPreCtx rpc, bool flush)
 Frees the given ruPreCtx. More...
 
RUAPI void ruPreLogSink (perm_ptr rpc, uint32_t logLevel, trans_chars msg)
 Pre logging sink to store log entries until a final log sink has been established. More...
 
RUAPI ruSinkCtx ruSinkCtxNew (trans_chars filePath, ruCloseFunc closeCb, perm_ptr closeCtx)
 Creates a new context to use with ruFileLogSink. More...
 
RUAPI int32_t ruSinkCtxPath (ruSinkCtx rsc, trans_chars filePath)
 Allows changing the file path of the given ruSinkCtx. More...
 
RUAPI ruSinkCtx ruSinkCtxFree (ruSinkCtx rsc)
 Frees the given ruSinkCtx after ruStopLogger has been called. This must not be called when the context is still in use. More...
 
RUAPI void ruFileLogSink (perm_ptr rsc, uint32_t logLevel, trans_chars msg)
 A log sink that logs into the filepath set in the given ruSinkCtx. More...
 
RUAPI void ruStdErrLogSink (perm_ptr udata, uint32_t logLevel, trans_chars msg)
 A convenience logging implementation that logs to stderr. More...
 
RUAPI void ruSetLogger (ruLogFunc logger, uint32_t logLevel, perm_ptr userData, ruCleaner cleaner, bool threaded)
 Sets the global logging function for this process. More...
 
RUAPI void ruStopLogger (void)
 Stop the current logger and flush the queue before returning. More...
 
RUAPI void ruSetLogLevel (uint32_t logLevel)
 Adjusts the current log level. More...
 
RUAPI uint32_t ruGetLogLevel (void)
 Returns the currently set log level. More...
 
RUAPI bool ruDoesLog (uint32_t log_level)
 Returns whether the given log level should log. More...
 
RUAPI void ruDoLog (uint32_t log_level, trans_chars filePath, trans_chars func, int32_t line, trans_chars format,...)
 Logs the given parameters if logging is set. More...
 
RUAPI void ruDoLogV (uint32_t log_level, trans_chars filePath, trans_chars func, int32_t line, trans_chars format, va_list args)
 Logs the given parameters if logging is set. More...
 
RUAPI alloc_chars ruMakeLogMsgV (uint32_t log_level, trans_chars filePath, trans_chars func, int32_t line, trans_chars format, va_list args)
 
RUAPI void ruRawLog (uint32_t log_level, trans_chars msg)
 Sends the given message as is to the current log sink. More...
 
RUAPI void ruFlushLog (void)
 Sends a NULL message to the current log sink, usually causing it to flush or close the log file. More...
 
RUAPI char * ruMakeLogMsg (uint32_t log_level, trans_chars file, trans_chars func, int32_t line, trans_chars format,...)
 

Detailed Description

This section contains logging specific APIs.

Example:

#include <regify-util.h>
void testlog(trans_chars logPath, bool cleaning, bool threaded) {
ruCleaner rc = NULL;
ruSinkCtx sc = NULL;
// Use the pre logger until we determine where to log to...
ruSetLogger(ruPreLogSink, RU_LOG_DBUG, pc, NULL, threaded);
if (cleaning) {
ruInfoLog("Using log cleaner");
rc = ruCleanNew(0);
ruCleanAdd(rc, "testsecret", "^^^TEST_SECRET^^^");
ruVerbLog("keeping testsecret hidden");
}
if (ruStrEmpty(logfile)) {
ruInfoLog("Using std error logger");
ruSetLogger(ruStdErrLogSink, RU_LOG_DBUG, NULL, rc, threaded);
} else {
ruInfoLogf("Logging to '%s'", logfile);
sc = ruSinkCtxNew(logfile, NULL, NULL);
ruSetLogger(ruFileLogSink, RU_LOG_DBUG, sc, rc, threaded);
}
// flush and free the pre logger
pc = ruPreCtxFree(pc, true);
// program logic here...
ruInfoLog("starting with testsecret and cleaner");
// clean up when finished
ruInfoLog("stopping logger");
}
RUAPI int32_t ruCleanAdd(ruCleaner rc, trans_chars instr, trans_chars substitute)
void * ruCleaner
Definition: regify-util.h:685
RUAPI ruCleaner ruCleanFree(ruCleaner rc)
RUAPI ruCleaner ruCleanNew(rusize chunkSize)
Creates a new ruCleaner object. To be freed with ruCleanFree.
RUAPI ruPreCtx ruPreCtxNew(void)
Creates a new pre logging context for use with ruPreLogSink.
RUAPI void ruSetLogger(ruLogFunc logger, uint32_t logLevel, perm_ptr userData, ruCleaner cleaner, bool threaded)
Sets the global logging function for this process.
#define ruVerbLog(msg)
Emits a VERB level log message.
Definition: logging.h:409
#define ruInfoLogf(fmt,...)
Emits a INFO level log message.
Definition: logging.h:403
RUAPI void ruFileLogSink(perm_ptr rsc, uint32_t logLevel, trans_chars msg)
A log sink that logs into the filepath set in the given ruSinkCtx.
RUAPI ruPreCtx ruPreCtxFree(ruPreCtx rpc, bool flush)
Frees the given ruPreCtx.
#define RU_LOG_DBUG
This is a debugging logging level used for debugging things that are normally assumed to be working....
Definition: logging.h:107
RUAPI ruSinkCtx ruSinkCtxFree(ruSinkCtx rsc)
Frees the given ruSinkCtx after ruStopLogger has been called. This must not be called when the contex...
RUAPI void ruStopLogger(void)
Stop the current logger and flush the queue before returning.
ptr ruPreCtx
Opaque pointer to ruPreLogSink object.
Definition: logging.h:135
ptr ruSinkCtx
Opaque pointer to ruFileLogSink object.
Definition: logging.h:174
RUAPI void ruPreLogSink(perm_ptr rpc, uint32_t logLevel, trans_chars msg)
Pre logging sink to store log entries until a final log sink has been established.
RUAPI ruSinkCtx ruSinkCtxNew(trans_chars filePath, ruCloseFunc closeCb, perm_ptr closeCtx)
Creates a new context to use with ruFileLogSink.
#define ruInfoLog(msg)
Emits a INFO level log message.
Definition: logging.h:396
RUAPI void ruStdErrLogSink(perm_ptr udata, uint32_t logLevel, trans_chars msg)
A convenience logging implementation that logs to stderr.
const char * trans_chars
A transient NULL terminated string pointer.
Definition: regify-util.h:186
RUAPI bool ruStrEmpty(trans_chars str)
Checks whether given string has any content other than whitespace.

Macro Definition Documentation

◆ RU_LOG_CRIT

#define RU_LOG_CRIT   1

This level reduces logging to fatal errors and generally unexpected serious stuff.

◆ RU_LOG_DBUG

#define RU_LOG_DBUG   5

This is a debugging logging level used for debugging things that are normally assumed to be working. It is usually off because it litters the logs with often redundant data.

◆ RU_LOG_INFO

#define RU_LOG_INFO   3

This level includes RU_LOG_WARN and general information. Usual low quantity.

◆ RU_LOG_NONE

#define RU_LOG_NONE   0

This level disables logging altogether.

◆ RU_LOG_VERB

#define RU_LOG_VERB   4

This is the verbose logging level used for debugging and usually high quantity.

◆ RU_LOG_WARN

#define RU_LOG_WARN   2

This level includes RU_LOG_CRIT and general warning messages.

◆ ruCritLog

#define ruCritLog (   msg)    ruLog_(RU_LOG_CRIT, "%s", msg)

Emits a CRIT level log message.

Parameters
msgThe message to be logged.

◆ ruCritLogf

#define ruCritLogf (   fmt,
  ... 
)    ruLog_(RU_LOG_CRIT, fmt, __VA_ARGS__)

Emits a CRIT level log message.

Parameters
fmtThe format specifier for the remaining arguments.
...The remaining arguments that make up the log message.

◆ ruDbgLog

#define ruDbgLog (   msg)    ruLog_(RU_LOG_DBUG, "%s", msg)

Emits a DBUG level log message.

Parameters
msgThe message to be logged.

◆ ruDbgLogf

#define ruDbgLogf (   fmt,
  ... 
)    ruLog_(RU_LOG_DBUG, fmt, __VA_ARGS__)

Emits a DBUG level log message.

Parameters
fmtThe format specifier for the remaining arguments.
...The remaining arguments that make up the log message.

◆ ruDump16

#define ruDump16 (   msg,
  start 
)
Value:
ruDbgLogf("%s @0x%x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x", msg, (start), \
(uint8_t)*(start), (uint8_t)*((start)+1), (uint8_t)*((start)+2), (uint8_t)*((start)+3), \
(uint8_t)*((start)+4), (uint8_t)*((start)+5), (uint8_t)*((start)+6), (uint8_t)*((start)+7), \
(uint8_t)*((start)+8), (uint8_t)*((start)+9), (uint8_t)*((start)+10), (uint8_t)*((start)+11), \
(uint8_t)*((start)+12), (uint8_t)*((start)+13), (uint8_t)*((start)+14), (uint8_t)*((start)+15))
#define ruDbgLogf(fmt,...)
Emits a DBUG level log message.
Definition: logging.h:429

Debug logs the 16 bytes following start preceded by given message.

Parameters
msgprefix message
startaddress from where to start dumping bytes

◆ ruDump8

#define ruDump8 (   msg,
  start 
)
Value:
ruDbgLogf("%s @0x%x %02x %02x %02x %02x %02x %02x %02x %02x", msg, (start), \
(uint8_t)*(start), (uint8_t)*((start)+1), (uint8_t)*((start)+2), (uint8_t)*((start)+3), \
(uint8_t)*((start)+4), (uint8_t)*((start)+5), (uint8_t)*((start)+6), (uint8_t)*((start)+7))

Debug logs the 8 bytes following start preceded by given message.

Parameters
msgprefix message
startaddress from where to start dumping bytes

◆ ruInfoLog

#define ruInfoLog (   msg)    ruLog_(RU_LOG_INFO, "%s", msg)

Emits a INFO level log message.

Parameters
msgThe message to be logged.

◆ ruInfoLogf

#define ruInfoLogf (   fmt,
  ... 
)    ruLog_(RU_LOG_INFO, fmt, __VA_ARGS__)

Emits a INFO level log message.

Parameters
fmtThe format specifier for the remaining arguments.
...The remaining arguments that make up the log message.

◆ ruVerbLog

#define ruVerbLog (   msg)    ruLog_(RU_LOG_VERB, "%s", msg)

Emits a VERB level log message.

Parameters
msgThe message to be logged.

◆ ruVerbLogf

#define ruVerbLogf (   fmt,
  ... 
)    ruLog_(RU_LOG_VERB, fmt, __VA_ARGS__)

Emits a VERB level log message.

Parameters
fmtThe format specifier for the remaining arguments.
...The remaining arguments that make up the log message.

◆ ruWarnLog

#define ruWarnLog (   msg)    ruLog_(RU_LOG_WARN, "%s", msg)

Emits a WARN level log message.

Parameters
msgThe message to be logged.

◆ ruWarnLogf

#define ruWarnLogf (   fmt,
  ... 
)    ruLog_(RU_LOG_WARN, fmt, __VA_ARGS__)

Emits a WARN level log message.

Parameters
fmtThe format specifier for the remaining arguments.
...The remaining arguments that make up the log message.

Typedef Documentation

◆ ruCloseFunc

typedef void(* ruCloseFunc) (perm_ptr userData)

The type of function to pass to ruSinkCtxNew which will be called when ruFileLogSink has closed its logfile.

Parameters
userDataThe closeCtx reference that was passed to ruSinkCtxNew.

◆ ruLogFunc

typedef void(* ruLogFunc) (perm_ptr userData, uint32_t logLevel, trans_chars msg)

The type of function to pass to ruSetLogger in order to hook up logging.

When this function receives NULL for msg, it means that this may be the last log call this function will receive in the current context. This is intended to finalize whatever resources it may have, or simply to flush data.

Parameters
userDataThe userData reference that was passed to ruSetLogger.
logLevelLog level pertaining to this message. Log filtering has been done at this point, and this is to facilitate further log level based processing decisions.
msgThe message to log, or NULL for flushing or closing.

◆ ruPreCtx

typedef ptr ruPreCtx

Opaque pointer to ruPreLogSink object.

◆ ruSinkCtx

typedef ptr ruSinkCtx

Opaque pointer to ruFileLogSink object.

Function Documentation

◆ ruDoesLog()

RUAPI bool ruDoesLog ( uint32_t  log_level)

Returns whether the given log level should log.

This function considers the set log level only if a logging function was set. If no logging function was set it will return false.

Parameters
log_levelLevel to be considered.
Returns
Whether to log.

◆ ruDoLog()

RUAPI void ruDoLog ( uint32_t  log_level,
trans_chars  filePath,
trans_chars  func,
int32_t  line,
trans_chars  format,
  ... 
)

Logs the given parameters if logging is set.

This function is really internal and used by the log macros.

Parameters
log_levelLog level of this message.
filePathThe source file where this message originates from.
funcThe function that created this log entry.
lineThe line where the log was created at.
formatThe format specifier for the remaining arguments.
...The remaining arguments that make up the log message.

◆ ruDoLogV()

RUAPI void ruDoLogV ( uint32_t  log_level,
trans_chars  filePath,
trans_chars  func,
int32_t  line,
trans_chars  format,
va_list  args 
)

Logs the given parameters if logging is set.

This function is advanced and designed to be used by custom log macros.

Parameters
log_levelLog level of this message.
filePathThe source file where this message originates from.
funcThe function that created this log entry.
lineThe line where the log was created at.
formatThe format specifier for the remaining arguments.
argsvariable argument list

◆ ruFileLogSink()

RUAPI void ruFileLogSink ( perm_ptr  rsc,
uint32_t  logLevel,
trans_chars  msg 
)

A log sink that logs into the filepath set in the given ruSinkCtx.

This sink checks its underlying file every 3 seconds to see whether it has moved or the ruSinkCtx filePath has changed. When it does, it closes its current file handle and reopens it to allow for logrotation to work. When it receives a NULL message, like through ruFlushLog, it closes its file. Through the use of ruSinkCtxPath, output files can be changed on the fly. Every time it closes its log file, the ruSinkCtx closeCb is called.

Parameters
rscUser data. Must be a ruSinkCtx.
logLevelIgnored log level pertaining to this message.
msgThe message to log. NULL will cause it to close the log file.

◆ ruFlushLog()

RUAPI void ruFlushLog ( void  )

Sends a NULL message to the current log sink, usually causing it to flush or close the log file.

If the logger is threaded this will block until a NULL has been sent to the given log sink.

◆ ruGetLogLevel()

RUAPI uint32_t ruGetLogLevel ( void  )

Returns the currently set log level.

Returns
The currently set log level.

◆ ruMakeLogMsg()

RUAPI char* ruMakeLogMsg ( uint32_t  log_level,
trans_chars  file,
trans_chars  func,
int32_t  line,
trans_chars  format,
  ... 
)

Internal logging function used by the log macros.

Parameters
log_levelLog level of this message.
fileThe source file where this message originates from.
funcThe function that created this log entry.
lineThe line where the log was created at.
formatThe format specifier for the remaining arguments.
...The remaining arguments that make up the log message.
Returns
The allocated log message to be freed by the caller after usage.

◆ ruMakeLogMsgV()

RUAPI alloc_chars ruMakeLogMsgV ( uint32_t  log_level,
trans_chars  filePath,
trans_chars  func,
int32_t  line,
trans_chars  format,
va_list  args 
)

Internal logging function used by the log macros.

Parameters
log_levelLog level of this message.
filePathThe source file where this message originates from.
funcThe function that created this log entry.
lineThe line where the log was created at.
formatThe format specifier for the remaining arguments.
argsvariable argument list
Returns
The allocated log message to be freed by the caller after usage.

◆ ruPreCtxFree()

RUAPI ruPreCtx ruPreCtxFree ( ruPreCtx  rpc,
bool  flush 
)

Frees the given ruPreCtx.

Parameters
rpccontext to free
flushlog the accumulated entries before freeing them.
Returns
NULL

◆ ruPreCtxNew()

RUAPI ruPreCtx ruPreCtxNew ( void  )

Creates a new pre logging context for use with ruPreLogSink.

This context is useful for temporarily storing log entries until the final log sink has been established. At that point this context may be freed optionally logging its content into the current logsink.

Returns
ruPreCtx object

◆ ruPreLogSink()

RUAPI void ruPreLogSink ( perm_ptr  rpc,
uint32_t  logLevel,
trans_chars  msg 
)

Pre logging sink to store log entries until a final log sink has been established.

When calling ruSetLogger with this sink, the log level should be set to RU_LOG_DBUG so that all potential entries will be accumulated. The actual filtering is done when this context is flushed. At that point entries will be filtered by the then current log level.

Parameters
rpcUser data. Must be a ruPreCtx.
logLevelLog level pertaining to this message. Will be stored for flush time.
msgThe message to log. NULL will be ignored.

◆ ruRawLog()

RUAPI void ruRawLog ( uint32_t  log_level,
trans_chars  msg 
)

Sends the given message as is to the current log sink.

Parameters
log_levelLog level of this message used solely for filtering.
msgstring to be logged without modification. The trailing line feed must be given.

◆ ruSetLogger()

RUAPI void ruSetLogger ( ruLogFunc  logger,
uint32_t  logLevel,
perm_ptr  userData,
ruCleaner  cleaner,
bool  threaded 
)

Sets the global logging function for this process.

When threaded is set, the log thread always follows up with one NULL message when there are no more messages to log. This is intended to facilitate log flushing. In the absense of the log thread this can also be accomplished by calling ruFlushLog. At the end ruStopLogger should be called to flush and close the log file.

Parameters
loggerLogging function that will be called with messages.
logLevelLoglevel to determine what gets logged.
userDataOpaque custom user data that will be passed to the ruLogFunc implementation.
cleanerOptional ruCleaner instance for real time password cleaning.
threadedWhether to receive all logger calls from a dedicated thread. This is useful when many threads do lots of logging.

◆ ruSetLogLevel()

RUAPI void ruSetLogLevel ( uint32_t  logLevel)

Adjusts the current log level.

Parameters
logLevelNew log level to log at.

◆ ruSinkCtxFree()

RUAPI ruSinkCtx ruSinkCtxFree ( ruSinkCtx  rsc)

Frees the given ruSinkCtx after ruStopLogger has been called. This must not be called when the context is still in use.

Parameters
rscruSinkCtx to free.
Returns
NULL

◆ ruSinkCtxNew()

RUAPI ruSinkCtx ruSinkCtxNew ( trans_chars  filePath,
ruCloseFunc  closeCb,
perm_ptr  closeCtx 
)

Creates a new context to use with ruFileLogSink.

Parameters
filePathPath to where log file will be written. Directory must be writable.
closeCbOptional callback to run when the old file has been closed after ruSinkCtxPath was called.
closeCtxOptional context which will be passed to closeCb
Returns
New ruSinkCtx to free with ruSinkCtxFree.

◆ ruSinkCtxPath()

RUAPI int32_t ruSinkCtxPath ( ruSinkCtx  rsc,
trans_chars  filePath 
)

Allows changing the file path of the given ruSinkCtx.

This will have immediate effect on logs sent to ruFileLogSink. When the old log has been closed the closeCb function given to ruSinkCtxNew will be called exactly one time. This will not happen if the new filePath is equal to the old filePath.

Parameters
rscruSinkCtx to update.
filePathPath to new log file.
Returns
Status of the operation.

◆ ruStdErrLogSink()

RUAPI void ruStdErrLogSink ( perm_ptr  udata,
uint32_t  logLevel,
trans_chars  msg 
)

A convenience logging implementation that logs to stderr.

Parameters
udataUser data. This is ignored in this function.
logLevelLog level pertaining to this message.
msgThe message to log.

◆ ruStopLogger()

RUAPI void ruStopLogger ( void  )

Stop the current logger and flush the queue before returning.