Annotation of mqtt/src/logmqtt.c, revision 1.2
1.2 ! misho 1: #include "global.h"
! 2:
! 3:
! 4: extern char *__progname;
! 5:
! 6:
! 7: /*
! 8: * mqtt_rtlm_log() Log message
! 9: *
! 10: * @fmt = format string
! 11: * @... = argument list
! 12: * return: none
! 13: */
! 14: static void
! 15: mqtt_rtlm_log(const char *fmt, ...)
! 16: {
! 17: va_list lst;
! 18:
! 19: va_start(lst, fmt);
! 20: vsyslog(LOG_ERR, fmt, lst);
! 21: va_end(lst);
! 22: }
! 23: #define MQTT_RTLM_LOG() (mqtt_rtlm_log("Error:: System #%d - %s", errno, strerror(errno)))
! 24:
! 25:
! 26: /*
! 27: * mqtt_rtlm_open() Open log file
! 28: *
! 29: * @cfg = loaded config
! 30: * return: NULL error or SQL handle
! 31: */
! 32: FILE *
! 33: mqtt_rtlm_open(sl_config *cfg)
! 34: {
! 35: FILE *f = NULL;
! 36: const char *str = NULL;
! 37:
! 38: if (!cfg)
! 39: return NULL;
! 40:
! 41: str = (const char*) cfg_GetAttribute(cfg, CFG("mqtt_log"), CFG("name"));
! 42: if (!str) {
! 43: mqtt_rtlm_log("Error:: Unknown file name ...\n");
! 44: return NULL;
! 45: }
! 46:
! 47: if (!(f = fopen(str, "a"))) {
! 48: MQTT_RTLM_LOG();
! 49: return NULL;
! 50: }
! 51:
! 52: return f;
! 53: }
! 54:
! 55: /*
! 56: * mqtt_rtlm_close() Close log file
! 57: *
! 58: * @f = File handle
! 59: * return: none
! 60: */
! 61: void
! 62: mqtt_rtlm_close(FILE *f)
! 63: {
! 64: if (f)
! 65: fclose(f);
! 66: }
! 67:
! 68: /*
! 69: * mqtt_rtlm_logger() Logger to file
! 70: *
! 71: * @f = File handle
! 72: * @fmt = Message format
! 73: * @... = argument list
! 74: * return: -1 error or >-1 logged successful
! 75: */
! 76: int
! 77: mqtt_rtlm_logger(FILE *f, const char *fmt, ...)
! 78: {
! 79: va_list lst;
! 80: int ret = 0;
! 81: char szStr[STRSIZ] = { 0 }, szFmt[BUFSIZ] = { 0 };
! 82: struct tm *tm;
! 83: time_t tim;
! 84:
! 85: if (!f || !fmt)
! 86: return -1;
! 87:
! 88: tim = time(NULL);
! 89: tm = localtime(&tim);
! 90: strftime(szFmt, sizeof szFmt, "[%Y-%m-%d %H:%M:%S] ", tm);
! 91: snprintf(szStr, sizeof szStr, "%s (%d):: ", __progname, getpid());
! 92: strlcat(szFmt, szStr, sizeof szFmt);
! 93: strlcat(szFmt, fmt, sizeof szFmt);
! 94:
! 95: va_start(lst, fmt);
! 96: ret = vfprintf(f, szFmt, lst);
! 97: va_end(lst);
! 98:
! 99: fflush(f);
! 100: return ret;
! 101: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>