michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "cpr_stdio.h" michael@0: #include "cpr_string.h" michael@0: #include "CSFLog.h" michael@0: michael@0: /** michael@0: * @def LOG_MAX michael@0: * michael@0: * Constant represents the maximum allowed length for a message michael@0: */ michael@0: #define LOG_MAX 1024 michael@0: michael@0: /** michael@0: * @addtogroup DebugAPIs The CPR Logging Abstractions michael@0: * @ingroup CPR michael@0: * @brief The CPR Debug/Logging APIs michael@0: * michael@0: * @{ michael@0: */ michael@0: michael@0: /** michael@0: * Debug message michael@0: * michael@0: * @param _format format string michael@0: * @param ... variable arg list michael@0: * michael@0: * @return Return code from vsnprintf michael@0: * michael@0: * @pre (_format not_eq NULL) michael@0: */ michael@0: int michael@0: buginf (const char *_format, ...) michael@0: { michael@0: char fmt_buf[LOG_MAX + 1]; michael@0: va_list ap; michael@0: int rc; michael@0: michael@0: va_start(ap, _format); michael@0: rc = vsnprintf(fmt_buf, LOG_MAX, _format, ap); michael@0: va_end(ap); michael@0: if (rc <= 0) { michael@0: return rc; michael@0: } michael@0: michael@0: CSFLogDebug("cpr", "%s", fmt_buf); michael@0: michael@0: return rc; michael@0: } michael@0: michael@0: /** michael@0: * Debug message that can be larger than #LOG_MAX michael@0: * michael@0: * @param str - a fixed constant string michael@0: * michael@0: * @return zero(0) michael@0: * michael@0: * @pre (str not_eq NULL) michael@0: */ michael@0: int michael@0: buginf_msg (const char *str) michael@0: { michael@0: char buf[LOG_MAX + 1]; michael@0: const char *p; michael@0: int16_t len; michael@0: michael@0: // terminate buffer michael@0: buf[LOG_MAX] = NUL; michael@0: michael@0: len = (int16_t) strlen(str); michael@0: michael@0: if (len > LOG_MAX) { michael@0: p = str; michael@0: do { michael@0: memcpy(buf, p, LOG_MAX); michael@0: p += LOG_MAX; michael@0: len -= LOG_MAX; michael@0: michael@0: printf("%s",buf); michael@0: } while (len > LOG_MAX); michael@0: michael@0: if (len) { michael@0: CSFLogDebug("cpr", "%s", (char *)p); michael@0: } michael@0: } else { michael@0: CSFLogDebug("cpr", "%s", (char *) str); michael@0: } michael@0: michael@0: return 0; michael@0: } michael@0: michael@0: /** michael@0: * Error message michael@0: * michael@0: * @param _format format string michael@0: * @param ... variable arg list michael@0: * michael@0: * @return Return code from vsnprintf michael@0: * michael@0: * @pre (_format not_eq NULL) michael@0: */ michael@0: void michael@0: err_msg (const char *_format, ...) michael@0: { michael@0: char fmt_buf[LOG_MAX + 1]; michael@0: va_list ap; michael@0: int rc; michael@0: michael@0: va_start(ap, _format); michael@0: rc = vsnprintf(fmt_buf, LOG_MAX, _format, ap); michael@0: va_end(ap); michael@0: if (rc <= 0) { michael@0: return; michael@0: } michael@0: michael@0: CSFLogError("cpr", "%s", fmt_buf); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Notice message michael@0: * michael@0: * @param _format format string michael@0: * @param ... variable arg list michael@0: * michael@0: * @return Return code from vsnprintf michael@0: * michael@0: * @pre (_format not_eq NULL) michael@0: */ michael@0: void michael@0: notice_msg (const char *_format, ...) michael@0: { michael@0: char fmt_buf[LOG_MAX + 1]; michael@0: va_list ap; michael@0: int rc; michael@0: michael@0: va_start(ap, _format); michael@0: rc = vsnprintf(fmt_buf, LOG_MAX, _format, ap); michael@0: va_end(ap); michael@0: if (rc <= 0) { michael@0: return; michael@0: } michael@0: michael@0: CSFLogInfo("cpr", "%s", fmt_buf); michael@0: } michael@0: