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 a rotating 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 output to another stream.
static bool flush_ (qlog_t *log)
 qlog->flush(): Flush buffered log data.
static void free_ (qlog_t *log)
 qlog->free(): Close rotating-log file & free resources

Detailed Description

Rotating file logger object.

qlog implements an 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 a rotating 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 a rotating log file.

Parameters
filepathfmtfilename format. The format string follows strftime()
modenew file mode. Use 0 for the system default
rotateintervalrotation interval in seconds. Use 0 to disable rotation
optionscombination of options
Returns
qlog_t pointer.
Note
rotateinterval is not relative time. If you set it to 3600, the log file rotates every hour. filepathfmt uses the same format as strftime(), so if you want hourly rotation, the format must include the hour, such as "/somepath/xxx-%Y%m%d%H.log". You can use "/somepath/xxx-%H.log" for a daily overwritten log file.
Available options:
  • QLOG_OPT_THREADSAFE - make it thread-safe.
  • QLOG_OPT_FLUSH - flush out buffer every time.
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_()

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

qlog->write(): Log messages

Parameters
logpointer to qlog_t
strmessage string
Returns
true on success, otherwise false

Definition at line 160 of file qlog.c.

◆ writef()

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

qlog->writef(): Log messages

Parameters
logpointer to qlog_t
formatmessage format string
Returns
true on success, otherwise false

Definition at line 199 of file qlog.c.

◆ duplicate()

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

qlog->duplicate(): Duplicate log output to another stream.

Parameters
logpointer to qlog_t
fpstream to write duplicated log messages to. Set NULL to disable duplication.
flushset to true to flush every time data is duplicated.
Returns
true on success, otherwise 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_()

bool flush_ ( qlog_t * log)
static

qlog->flush(): Flush buffered log data.

Parameters
logpointer to qlog_t
Returns
true on success, otherwise false

Definition at line 249 of file qlog.c.

◆ free_()

void free_ ( qlog_t * log)
static

qlog->free(): Close rotating-log file & free resources

Parameters
logpointer to qlog_t

Definition at line 269 of file qlog.c.