/** * Logs a formatted string -- similiar to printf() * * @param sev The severity * @param fmt The format string * @param ... Additional arguments */ void log_fmt(log_sev_t sev, const char *fmt, ...) { va_list args; int len; size_t cap; if ((fmt == NULL) || !log_sev_on(sev)) { return; } /* Some implementation of vsnprintf() return the number of characters * that would have been stored if the buffer was large enough instead of * the number of characters actually stored. */ cap = log_start(sev); /* Add on the variable log parameters to the log string */ va_start(args, fmt); len = vsnprintf(&g_log.buf[g_log.buf_len], cap, fmt, args); va_end(args); if (len > 0) { if (len > (int)cap) { len = cap; } g_log.buf_len += len; g_log.buf[g_log.buf_len] = 0; } log_end(); }
/** * Dumps hex characters inline, no newlines inserted * * @param sev The severity * @param data The data to log * @param len The number of bytes to log */ void log_hex(log_sev_t sev, const void *vdata, int len) { const UINT8 *dat = (const UINT8 *)vdata; int idx; char buf[80]; if ((vdata == NULL) || !log_sev_on(sev)) { return; } idx = 0; while (len-- > 0) { buf[idx++] = to_hex_char(*dat >> 4); buf[idx++] = to_hex_char(*dat); dat++; if (idx >= (int)(sizeof(buf) - 3)) { buf[idx] = 0; log_str(sev, buf, idx); idx = 0; } } if (idx > 0) { buf[idx] = 0; log_str(sev, buf, idx); } }
static void print_stack(log_sev_t logsev, const char *str, struct parse_frame *frm, chunk_t *pc) { (void)pc; LOG_FUNC_ENTRY(); if (log_sev_on(logsev)) { int idx; log_fmt(logsev, "%8.8s", str); for (idx = 1; idx <= frm->pse_tos; idx++) { if (frm->pse[idx].stage != BS_NONE) { LOG_FMT(logsev, " [%s - %d]", get_token_name(frm->pse[idx].type), frm->pse[idx].stage); } else { LOG_FMT(logsev, " [%s]", get_token_name(frm->pse[idx].type)); } } log_fmt(logsev, "\n"); } }
void log_pcf_flags(log_sev_t sev, UINT64 flags) { if (!log_sev_on(sev)) { return; } log_fmt(sev, "[0x%" PRIx64 ":", flags); const char *tolog = NULL; for (int i = 0; i < (int)ARRAY_SIZE(pcf_names); i++) { if ((flags & (1ULL << i)) != 0) { if (tolog != NULL) { log_str(sev, tolog, strlen(tolog)); log_str(sev, ",", 1); } tolog = pcf_names[i]; } } if (tolog != NULL) { log_str(sev, tolog, strlen(tolog)); } log_str(sev, "]\n", 2); }
/** * Logs a block of data in a pretty hex format * Numbers on the left, characters on the right, just like I like it. * * @param sev The severity * @param data The data to log * @param len The number of bytes to log */ void log_hex_blk(log_sev_t sev, const void *data, int len) { static char buf[80] = "nnn | XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX | cccccccccccccccc\n"; const UINT8 *dat = (const UINT8 *)data; int idx; int count; int str_idx = 0; int chr_idx = 0; int tmp; int total; if ((data == NULL) || (len <= 0) || !log_sev_on(sev)) { return; } /* * Dump the specified number of bytes in hex, 16 byte per line by * creating a string and then calling log_str() */ /* Loop through the data of the current iov */ count = 0; total = 0; for (idx = 0; idx < len; idx++) { if (count == 0) { str_idx = 6; chr_idx = 56; buf[0] = to_hex_char(total >> 12); buf[1] = to_hex_char(total >> 8); buf[2] = to_hex_char(total >> 4); } tmp = dat[idx]; buf[str_idx] = to_hex_char(tmp >> 4); buf[str_idx + 1] = to_hex_char(tmp); str_idx += 3; buf[chr_idx++] = unc_isprint(tmp) ? tmp : '.'; total++; count++; if (count >= 16) { count = 0; log_str(sev, buf, 73); } }
static void align_log_al(log_sev_t sev, int line) { int idx; if (log_sev_on(sev)) { log_fmt(sev, "%s: line %d, %d)", __func__, line, cpd.al_cnt); for (idx = 0; idx < cpd.al_cnt; idx++) { log_fmt(sev, " %d/%d=%s", cpd.al[idx].col, cpd.al[idx].len, get_token_name(cpd.al[idx].type)); } log_fmt(sev, "\n"); } }
/** * Logs a string of known length * * @param sev The severity * @param str The pointer to the string * @param len The length of the string from strlen(str) */ void log_str(log_sev_t sev, const char *str, int len) { if ((str == NULL) || (len <= 0) || !log_sev_on(sev)) { return; } size_t cap = log_start(sev); if (cap > 0) { if (len > (int)cap) { len = cap; } memcpy(&g_log.buf[g_log.buf_len], str, len); g_log.buf_len += len; g_log.buf[g_log.buf_len] = 0; } log_end(); }
static void print_stack(log_sev_t logsev, const char *str, parse_frame_t *frm, chunk_t *pc) { UNUSED(pc); LOG_FUNC_ENTRY(); if (log_sev_on(logsev)) { log_fmt(logsev, "%8.8s", str); for (size_t idx = 1; idx <= frm->pse_tos; idx++) { if (frm->pse[idx].stage != brace_stage_e::NONE) { LOG_FMT(logsev, " [%s - %u]", get_token_name(frm->pse[idx].type), (unsigned int)frm->pse[idx].stage); } else { LOG_FMT(logsev, " [%s]", get_token_name(frm->pse[idx].type)); } } log_fmt(logsev, "\n"); } }