qLibc
qlog.c File Reference

Rotating file logger object. More...

Go to the source code of this file.

Functions

qlog_t * qlog (const char *filepathfmt, mode_t mode, int rotateinterval, int options)
 Open ratating-log file.
 
static bool write_ (qlog_t *log, const char *str)
 qlog->write(): Log messages
 
static bool writef (qlog_t *log, const char *format,...)
 qlog->writef(): Log messages
 
static bool duplicate (qlog_t *log, FILE *outfp, bool flush)
 qlog->duplicate(): Duplicate log string into other stream
 
static bool flush_ (qlog_t *log)
 qlog->flush(): Flush buffered log
 
static void free_ (qlog_t *log)
 qlog->free(): Close ratating-log file & de-allocate resources
 

Detailed Description

Rotating file logger object.

qlog implements a auto-rotating file logger.

// create a daily-rotating log file.
qlog_t *log = qlog("/tmp/dailylog-%Y%m%d.err", 0644, 86400, false);
if(log == NULL) return;
// screen out.
log->duplicate(log, stdout, true);
// write logs.
log->write(log, "Service started.");
log->writef(log, "Server Id: %d", 1);
// close and release resources.
log->free(log);
qlog_t * qlog(const char *filepathfmt, mode_t mode, int rotateinterval, int options)
Open ratating-log file.
Definition qlog.c:105

Definition in file qlog.c.

Function Documentation

◆ qlog()

qlog_t * qlog ( const char *  filepathfmt,
mode_t  mode,
int  rotateinterval,
int  options 
)

Open ratating-log file.

Parameters
filepathfmtfilename format. formatting argument is same as strftime()
modenew file mode. 0 for system default
rotateintervalrotating interval seconds, set 0 to disable rotation
optionscombination of options.
Returns
a pointer of qlog_t structure
Note
rotateinterval is not relative time. If you set it to 3600, log file will be rotated at every hour. And filenameformat is same as strftime(). So If you want to log with hourly rotating, filenameformat must be defined including hour format like "/somepath/xxx-%Y%m%d%H.log". You can set it to "/somepath/xxx-%H.log" for daily overrided log file.
Available options:
  • QLOG_OPT_THREADSAFE - make it thread-safe.
  • QLOG_OPT_FLUSH - flush out buffer everytime.
qlog_t *log = qlog("/tmp/qdecoder-%Y%m%d.err", 0644, 86400, QLOG_OPT_THREADSAFE);
log->free(log);

Definition at line 105 of file qlog.c.

◆ write_()

static bool write_ ( qlog_t *  log,
const char *  str 
)
static

qlog->write(): Log messages

Parameters
loga pointer of qlog_t
strmessage string
Returns
true if successful, otherewise returns false

Definition at line 160 of file qlog.c.

◆ writef()

static bool writef ( qlog_t *  log,
const char *  format,
  ... 
)
static

qlog->writef(): Log messages

Parameters
loga pointer of qlog_t
formatmessages format
Returns
true if successful, otherewise returns false

Definition at line 199 of file qlog.c.

◆ duplicate()

static bool duplicate ( qlog_t *  log,
FILE *  outfp,
bool  flush 
)
static

qlog->duplicate(): Duplicate log string into other stream

Parameters
loga pointer of qlog_t
fplogging messages will be printed out into this stream. set NULL to disable.
flushset to true if you want to flush everytime duplicating.
Returns
true if successful, otherewise returns false
log->duplicate(log, stdout, true); // enable console out with flushing
log->duplicate(log, stderr, false); // enable console out
log->duplicate(log, NULL, false); // disable console out (default)

Definition at line 230 of file qlog.c.

◆ flush_()

static bool flush_ ( qlog_t *  log)
static

qlog->flush(): Flush buffered log

Parameters
loga pointer of qlog_t
Returns
true if successful, otherewise returns false

Definition at line 249 of file qlog.c.

◆ free_()

static void free_ ( qlog_t *  log)
static

qlog->free(): Close ratating-log file & de-allocate resources

Parameters
loga pointer of qlog_t

Definition at line 269 of file qlog.c.