version 1.1, 2011/11/23 08:50:38
|
version 1.2, 2012/01/27 15:05:38
|
Line 0
|
Line 1
|
|
#include "global.h" |
|
|
|
|
|
extern char *__progname; |
|
|
|
|
|
/* |
|
* mqtt_rtlm_log() Log message |
|
* |
|
* @fmt = format string |
|
* @... = argument list |
|
* return: none |
|
*/ |
|
static void |
|
mqtt_rtlm_log(const char *fmt, ...) |
|
{ |
|
va_list lst; |
|
|
|
va_start(lst, fmt); |
|
vsyslog(LOG_ERR, fmt, lst); |
|
va_end(lst); |
|
} |
|
#define MQTT_RTLM_LOG() (mqtt_rtlm_log("Error:: System #%d - %s", errno, strerror(errno))) |
|
|
|
|
|
/* |
|
* mqtt_rtlm_open() Open log file |
|
* |
|
* @cfg = loaded config |
|
* return: NULL error or SQL handle |
|
*/ |
|
FILE * |
|
mqtt_rtlm_open(sl_config *cfg) |
|
{ |
|
FILE *f = NULL; |
|
const char *str = NULL; |
|
|
|
if (!cfg) |
|
return NULL; |
|
|
|
str = (const char*) cfg_GetAttribute(cfg, CFG("mqtt_log"), CFG("name")); |
|
if (!str) { |
|
mqtt_rtlm_log("Error:: Unknown file name ...\n"); |
|
return NULL; |
|
} |
|
|
|
if (!(f = fopen(str, "a"))) { |
|
MQTT_RTLM_LOG(); |
|
return NULL; |
|
} |
|
|
|
return f; |
|
} |
|
|
|
/* |
|
* mqtt_rtlm_close() Close log file |
|
* |
|
* @f = File handle |
|
* return: none |
|
*/ |
|
void |
|
mqtt_rtlm_close(FILE *f) |
|
{ |
|
if (f) |
|
fclose(f); |
|
} |
|
|
|
/* |
|
* mqtt_rtlm_logger() Logger to file |
|
* |
|
* @f = File handle |
|
* @fmt = Message format |
|
* @... = argument list |
|
* return: -1 error or >-1 logged successful |
|
*/ |
|
int |
|
mqtt_rtlm_logger(FILE *f, const char *fmt, ...) |
|
{ |
|
va_list lst; |
|
int ret = 0; |
|
char szStr[STRSIZ] = { 0 }, szFmt[BUFSIZ] = { 0 }; |
|
struct tm *tm; |
|
time_t tim; |
|
|
|
if (!f || !fmt) |
|
return -1; |
|
|
|
tim = time(NULL); |
|
tm = localtime(&tim); |
|
strftime(szFmt, sizeof szFmt, "[%Y-%m-%d %H:%M:%S] ", tm); |
|
snprintf(szStr, sizeof szStr, "%s (%d):: ", __progname, getpid()); |
|
strlcat(szFmt, szStr, sizeof szFmt); |
|
strlcat(szFmt, fmt, sizeof szFmt); |
|
|
|
va_start(lst, fmt); |
|
ret = vfprintf(f, szFmt, lst); |
|
va_end(lst); |
|
|
|
fflush(f); |
|
return ret; |
|
} |