int ICACHE_RAM_ATTR sprintf(char* buffer, const char* format, ...) { int ret; va_list arglist; va_start(arglist, format); ret = ets_vsprintf(buffer, format, arglist); va_end(arglist); return ret; }
/****************************************************************************** * FunctionName : syslog_compose * Description : compose a syslog_entry_t from va_args * Parameters : va_args * Returns : the malloced syslog_entry_t ******************************************************************************/ LOCAL syslog_entry_t ICACHE_FLASH_ATTR * syslog_compose(uint8_t facility, uint8_t severity, const char *tag, const char *fmt, ...) { union { uint8_t buf[sizeof (syslog_entry_t) + 1024]; syslog_entry_t se; } sl; DBG("[%dµs] %s id=%lu\n", WDEV_NOW(), __FUNCTION__, syslog_msgid); syslog_entry_t *se = os_zalloc(sizeof (syslog_entry_t) + 1024); // allow up to 1k datagram if (se == NULL) return NULL; char *p = se->datagram; se->tick = WDEV_NOW(); // 0 ... 4294.967295s se->msgid = syslog_msgid; // The Priority value is calculated by first multiplying the Facility // number by 8 and then adding the numerical value of the Severity. p += os_sprintf(p, "<%d> ", facility * 8 + severity); // strftime doesn't work as expected - or adds 8k overhead. // so let's do poor man conversion - format is fixed anyway if (flashConfig.syslog_showdate == 0) p += os_sprintf(p, "- "); else { time_t now = NULL; struct tm *tp = NULL; // create timestamp: FULL-DATE "T" PARTIAL-TIME "Z": 'YYYY-mm-ddTHH:MM:SSZ ' // as long as realtime_stamp is 0 we use tick div 10⁶ as date now = (realtime_stamp == 0) ? (sl.se.tick / 1000000) : realtime_stamp; tp = gmtime(&now); p += os_sprintf(p, "%4d-%02d-%02dT%02d:%02d:%02d", tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec); if (realtime_stamp == 0) p += os_sprintf(p, ".%06luZ ", sl.se.tick % 1000000); else p += os_sprintf(p, "%+03d:00 ", flashConfig.timezone_offset); } // add HOSTNAME APP-NAME PROCID MSGID if (flashConfig.syslog_showtick) p += os_sprintf(p, "%s %s %lu.%06lu %lu ", flashConfig.hostname, tag, sl.se.tick / 1000000, sl.se.tick % 1000000, syslog_msgid++); else p += os_sprintf(p, "%s %s - %lu ", flashConfig.hostname, tag, syslog_msgid++); // append syslog message va_list arglist; va_start(arglist, fmt); p += ets_vsprintf(p, fmt, arglist ); va_end(arglist); sl.se.datagram_len = 1 + p - sl.se.datagram; syslog_entry_t *se = os_zalloc(sizeof (syslog_entry_t) + sl.se.datagram_len); os_memcpy(se, &sl.se, sizeof (syslog_entry_t) + sl.se.datagram_len); return se; }
char ICACHE_FLASH_ATTR *json_sprintf(char *buffer, const char *fmt, ...) { va_list args; va_start(args, fmt); ets_vsprintf(buffer, fmt, args); va_end(args); return buffer; }
ICACHE_FLASH_ATTR void syslog_send(int fac_pri, const char *fmt, ...) { va_list ap; static char syslogpkt[128]; static char timestr[64]; const char *hostname; char *p; sint8 rc; if ((hostname = syslog_hostname) == NULL) hostname = "esp8266"; os_strcpy(timestr, sntp_get_real_time(sntp_get_current_timestamp())); /* * "Thu Sep 03 03:33:33 2015\n" * 0123456789012345678901234 */ if (timestr[8] == '0') timestr[8] = ' '; timestr[19] = '\0'; sprintf(syslogpkt, "<%d>%s %s ", fac_pri, ×tr[4], hostname); for (p = syslogpkt; *p != '\0'; p++) ; va_start(ap, fmt); ets_vsprintf(p, fmt, ap); va_end(ap); for (p = syslogpkt; *p != '\0'; p++) ; rc = espconn_sent(&syslog_espconn, syslogpkt, p - syslogpkt); }