Logging 编辑
This chapter describes the global functions you use to perform logging. NSPR provides a set of logging functions that conditionally write printf()
style strings to the console or to a log file. NSPR uses this facility itself for its own development debugging purposes.
You can select events to be logged by module or level. A module is a user-defined class of log events. A level is a numeric value that indicates the seriousness of the event to be logged. You can combine module and level criteria to get highly selective logging.
NSPR also provides "assert"-style macros and functions to aid in application debugging.
Conditional Compilation and Execution
NSPR's logging facility is conditionally compiled in and enabled for applications using it. These controls are platform dependent. Logging is not compiled in for the Win16 platform. Logging is compiled into the NSPR debug builds; logging is not compiled into the NSPR optimized builds. The compile time #define
values DEBUG
or FORCE_/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LOG
enable NSPR logging for application programs.
To enable NSPR logging and/or the debugging aids in your application, compile using the NSPR debug build headers and runtime. Set one of the compile-time defines when you build your application.
Execution-time control of NSPR's logging uses two environment variables. These variables control which modules and levels are logged as well as the file name of the log file. By default, no logging is enabled at execution time.
Log Types and Variables
Two types supporting NSPR logging are exposed in the API:
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PRLogModuleInfo
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PRLogModuleLevel
Two environment variables control the behavior of logging at execution time:
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/NSPR_LOG_MODULES
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/NSPR_LOG_FILE
Logging Functions and Macros
The functions and macros for logging are:
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_NewLogModule
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_SetLogFile
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_SetLogBuffering
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LogPrint
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LogFlush
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LOG_TEST
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LOG
PR_Assert
/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_STATIC_ASSERT
(new in NSPR 4.6.6XXX this hasn't been released yet; the number is a logical guess)/wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_NOT_REACHED
The above documentation has not been ported to MDN yet, see http://www-archive.mozilla.org/projects/nspr/reference/html/prlog.html.
Use Example
The following sample code fragment demonstrates use of the logging and debugging aids.
- Compile the program with DEBUG defined.
- Before running the compiled program, set the environment variable /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/NSPR_LOG_MODULES to userStuff:5
static void UserLogStuff( void ) { /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PRLogModuleInfo *myLM; PRIntn i; /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_STATIC_ASSERT(5 > 4); /* NSPR 4.6.6 or newer */ myLM = /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_NewLogModule( "userStuff" ); /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_ASSERT( myLM ); /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LOG( myLM, /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LOG_NOTICE, ("Log a Notice %d\n", 999 )); for (i = 0; i < 10 ; i++ ) { /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LOG( myLM, /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LOG_DEBUG, ("Log Debug number: %d\n", i)); PR_Sleep( 500 ); } /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LOG( myLM, /wiki/en-US/docs/Mozilla/Projects/NSPR/Reference/PR_LOG_NOTICE, "That's all folks\n"); } /* end UserLogStuff() */
See also
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论