📄 mprlog.c
字号:
/** * @file mprLog.c * @brief Mbedthis Portable Runtime (MPR) Logging and error reporting. * @remarks We always provide these routines. *//*********************************** License **********************************//* * @copy default * * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. * * This software is distributed under commercial and open source licenses. * You may use the GPL open source license described below or you may acquire * a commercial license from Mbedthis Software. You agree to be fully bound * by the terms of either license. Consult the LICENSE.TXT distributed with * this software for full details. * * This software is open source; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. See the GNU General Public License for more * details at: http://www.mbedthis.com/downloads/gplLicense.html * * This program is distributed WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * This GPL license does NOT permit incorporating this software into * proprietary programs. If you are unable to comply with the GPL, you must * acquire a commercial license to use this software. Commercial licenses * for this software and support services are available from Mbedthis * Software at http://www.mbedthis.com * * @end */#include "mpr.h"/****************************** Forward Declarations **************************/static void defaultLogHandler(MPR_LOC_DEC(ctx, loc), int flags, int level, const char *msg);static void logOutput(MPR_LOC_DEC(ctx, loc), int flags, int level, const char *msg);/************************************ Code ************************************/void mprLog(MprCtx ctx, int level, const char *fmt, ...){ va_list args; char *buf; if (level > mprGetLogLevel(ctx)) { return; } va_start(args, fmt); mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, args); va_end(args); logOutput(MPR_LOC_ARGS(ctx), MPR_LOG_SRC, level, buf); va_end(args); mprFree(buf);}/*****************************************************************************//* * Do raw output */void mprRawLog(MprCtx ctx, const char *fmt, ...){ va_list args; char *buf; int len; va_start(args, fmt); len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, args); va_end(args); logOutput(MPR_LOC_ARGS(ctx), MPR_RAW, 0, buf); mprFree(buf);}/*****************************************************************************//* * Handle an error */void mprError(MPR_LOC_DEC(ctx, loc), const char *fmt, ...){ va_list args; char *buf; int len; va_start(args, fmt); len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, args); va_end(args); logOutput(MPR_LOC_PASS(ctx, loc), MPR_ERROR_MSG | MPR_ERROR_SRC, 0, buf); mprFree(buf);}/*****************************************************************************//* * Handle an error that should be displayed to the user */void mprUserError(MPR_LOC_DEC(ctx, loc), const char *fmt, ...){ va_list args; char *buf; int len; va_start(args, fmt); len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, args); va_end(args); logOutput(MPR_LOC_PASS(ctx, loc), MPR_USER_MSG | MPR_ERROR_SRC, 0, buf); mprFree(buf);}/*****************************************************************************//* * Handle a fatal error. Forcibly shutdown the application. */void mprFatalError(MPR_LOC_DEC(ctx, loc), const char *fmt, ...){ va_list args; char *buf; int len; va_start(args, fmt); len = mprAllocVsprintf(MPR_LOC_ARGS(ctx), &buf, 0, fmt, args); va_end(args); logOutput(MPR_LOC_PASS(ctx, loc), MPR_USER_MSG | MPR_FATAL_SRC, 0, buf); mprFree(buf);#if BREW mprSignalExit(ctx);#else exit(2);#endif}/*****************************************************************************//* * Handle a program assertion */void mprAssertError(MPR_LOC_DEC(ctx, loc), const char *msg){ logOutput(MPR_LOC_PASS(ctx, loc), MPR_ASSERT_MSG | MPR_ASSERT_SRC, 0, msg);}/*****************************************************************************//* * Handle an error */void mprStaticError(MPR_LOC_DEC(ctx, loc), const char *fmt, ...){ va_list args; int len; char buf[MPR_MAX_STRING]; va_start(args, fmt); len = mprVsprintf(buf, sizeof(buf), fmt, args); va_end(args); logOutput(MPR_LOC_PASS(ctx, loc), MPR_ERROR_MSG | MPR_ERROR_SRC, 0, buf);}/*****************************************************************************//* * Direct output to the standard output. Does not hook into the logging * system and does not allocate memory. */void mprStaticAssert(const char *loc, const char *msg){#if BLD_DEBUG char buf[MPR_MAX_STRING]; int len; len = mprSprintf(buf, sizeof(buf), "Assertion %s, failed at %s\n", msg, loc); mprBreakpoint(loc, buf); #if BLD_HOST_UNIX /* * MOB -- but is stdout always okay to use */ write(1, buf, len);#elif BREW || WIN /* * Only time we use printf. We can't get an alloc context so we have * to use real print */#if BREW && !BREW_SIMULATOR printf(" MP: %s\n", buf);#else printf("%s\n", buf);#endif#endif#endif}/*****************************************************************************/int mprGetLogLevel(MprCtx ctx){ return mprGetApp(ctx)->logLevel;}/******************************************************************************/void mprSetLogLevel(MprCtx ctx, int level){ mprGetApp(ctx)->logLevel = level;}/*****************************************************************************//* * Output a log message to the log handler */static void logOutput(MPR_LOC_DEC(ctx, loc), int flags, int level, const char *msg){ MprLogHandler handler; if (flags & (MPR_ERROR_SRC | MPR_FATAL_SRC | MPR_ASSERT_SRC)) { mprBreakpoint(MPR_LOC, 0); } mprAssert(ctx != 0); handler = mprGetApp(ctx)->logHandler; if (handler != 0) { (handler)(MPR_LOC_PASS(ctx, loc), flags, level, msg); return; } defaultLogHandler(MPR_LOC_PASS(ctx, loc), flags, level, msg);}/*****************************************************************************//* * Default log output is just to the console */static void defaultLogHandler(MPR_LOC_DEC(ctx, loc), int flags, int level, const char *msg){ MprApp *app; char *prefix; app = mprGetApp(ctx); prefix = app->name; while (*msg == '\n') { mprPrintf(ctx, "\n"); msg++; } if (flags & MPR_LOG_SRC) {#if BREW && !BREW_SIMULATOR mprPrintf(ctx, "%s\n", msg);#else mprPrintf(ctx, "%s: %d: %s\n", prefix, level, msg);#endif } else if (flags & MPR_ERROR_SRC) { /* * Use static printing to avoid malloc when the messages are small. * This is important for memory allocation errors. */ if (strlen(msg) < (MPR_MAX_STRING - 32)) { mprStaticPrintf(ctx, "%s: Error: %s\n", prefix, msg); } else { mprPrintf(ctx, "%s: Error: %s\n", prefix, msg); } } else if (flags & MPR_FATAL_SRC) { mprPrintf(ctx, "%s: Fatal: %s\n", prefix, msg); } else if (flags & MPR_ASSERT_SRC) {#if BLD_FEATURE_ALLOC_LEAK_TRACK mprPrintf(ctx, "%s: Assertion %s, failed at %s\n", prefix, msg, loc);#else mprPrintf(ctx, "%s: Assertion %s, failed\n", prefix, msg);#endif } else if (flags & MPR_RAW) { mprPrintf(ctx, "%s", msg);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -