michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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: #ifndef prlog_h___ michael@0: #define prlog_h___ michael@0: michael@0: #include "prtypes.h" michael@0: michael@0: PR_BEGIN_EXTERN_C michael@0: michael@0: /* michael@0: ** prlog.h -- Declare interfaces to NSPR's Logging service michael@0: ** michael@0: ** NSPR provides a logging service that is used by NSPR itself and is michael@0: ** available to client programs. michael@0: ** michael@0: ** To use the service from a client program, you should create a michael@0: ** PRLogModuleInfo structure by calling PR_NewLogModule(). After michael@0: ** creating the LogModule, you can write to the log using the PR_LOG() michael@0: ** macro. michael@0: ** michael@0: ** Initialization of the log service is handled by NSPR initialization. michael@0: ** michael@0: ** At execution time, you must enable the log service. To enable the michael@0: ** log service, set the environment variable: NSPR_LOG_MODULES michael@0: ** variable. michael@0: ** michael@0: ** NSPR_LOG_MODULES variable has the form: michael@0: ** michael@0: ** :[, :]* michael@0: ** michael@0: ** Where: michael@0: ** is the name passed to PR_NewLogModule(). michael@0: ** is a numeric constant, e.g. 5. This value is the maximum michael@0: ** value of a log event, enumerated by PRLogModuleLevel, that you want michael@0: ** written to the log. michael@0: ** michael@0: ** For example: to record all events of greater value than or equal to michael@0: ** PR_LOG_ERROR for a LogModule names "gizmo", say: michael@0: ** michael@0: ** set NSPR_LOG_MODULES=gizmo:2 michael@0: ** michael@0: ** Note that you must specify the numeric value of PR_LOG_ERROR. michael@0: ** michael@0: ** Special LogModule names are provided for controlling NSPR's log michael@0: ** service at execution time. These controls should be set in the michael@0: ** NSPR_LOG_MODULES environment variable at execution time to affect michael@0: ** NSPR's log service for your application. michael@0: ** michael@0: ** The special LogModule "all" enables all LogModules. To enable all michael@0: ** LogModule calls to PR_LOG(), say: michael@0: ** michael@0: ** set NSPR_LOG_MODULES=all:5 michael@0: ** michael@0: ** The special LogModule name "sync" tells the NSPR log service to do michael@0: ** unbuffered logging. michael@0: ** michael@0: ** The special LogModule name "bufsize:" tells NSPR to set the michael@0: ** log buffer to . michael@0: ** michael@0: ** The environment variable NSPR_LOG_FILE specifies the log file to use michael@0: ** unless the default of "stderr" is acceptable. For MS Windows michael@0: ** systems, NSPR_LOG_FILE can be set to a special value: "WinDebug" michael@0: ** (case sensitive). This value causes PR_LOG() output to be written michael@0: ** using the Windows API OutputDebugString(). OutputDebugString() michael@0: ** writes to the debugger window; some people find this helpful. michael@0: ** michael@0: ** michael@0: ** To put log messages in your programs, use the PR_LOG macro: michael@0: ** michael@0: ** PR_LOG(, , (, *)); michael@0: ** michael@0: ** Where is the address of a PRLogModuleInfo structure, and michael@0: ** is one of the levels defined by the enumeration: michael@0: ** PRLogModuleLevel. is a printf() style of argument list. That michael@0: ** is: (fmtstring, ...). michael@0: ** michael@0: ** Example: michael@0: ** michael@0: ** main() { michael@0: ** PRIntn one = 1; michael@0: ** PRLogModuleInfo * myLm = PR_NewLogModule("gizmo"); michael@0: ** PR_LOG( myLm, PR_LOG_ALWAYS, ("Log this! %d\n", one)); michael@0: ** return; michael@0: ** } michael@0: ** michael@0: ** Note the use of printf() style arguments as the third agrument(s) to michael@0: ** PR_LOG(). michael@0: ** michael@0: ** After compiling and linking you application, set the environment: michael@0: ** michael@0: ** set NSPR_LOG_MODULES=gizmo:5 michael@0: ** set NSPR_LOG_FILE=logfile.txt michael@0: ** michael@0: ** When you execute your application, the string "Log this! 1" will be michael@0: ** written to the file "logfile.txt". michael@0: ** michael@0: ** Note to NSPR engineers: a number of PRLogModuleInfo structures are michael@0: ** defined and initialized in prinit.c. See this module for ideas on michael@0: ** what to log where. michael@0: ** michael@0: */ michael@0: michael@0: typedef enum PRLogModuleLevel { michael@0: PR_LOG_NONE = 0, /* nothing */ michael@0: PR_LOG_ALWAYS = 1, /* always printed */ michael@0: PR_LOG_ERROR = 2, /* error messages */ michael@0: PR_LOG_WARNING = 3, /* warning messages */ michael@0: PR_LOG_DEBUG = 4, /* debug messages */ michael@0: michael@0: PR_LOG_NOTICE = PR_LOG_DEBUG, /* notice messages */ michael@0: PR_LOG_WARN = PR_LOG_WARNING, /* warning messages */ michael@0: PR_LOG_MIN = PR_LOG_DEBUG, /* minimal debugging messages */ michael@0: PR_LOG_MAX = PR_LOG_DEBUG /* maximal debugging messages */ michael@0: } PRLogModuleLevel; michael@0: michael@0: /* michael@0: ** One of these structures is created for each module that uses logging. michael@0: ** "name" is the name of the module michael@0: ** "level" is the debugging level selected for that module michael@0: */ michael@0: typedef struct PRLogModuleInfo { michael@0: const char *name; michael@0: PRLogModuleLevel level; michael@0: struct PRLogModuleInfo *next; michael@0: } PRLogModuleInfo; michael@0: michael@0: /* michael@0: ** Create a new log module. michael@0: */ michael@0: NSPR_API(PRLogModuleInfo*) PR_NewLogModule(const char *name); michael@0: michael@0: /* michael@0: ** Set the file to use for logging. Returns PR_FALSE if the file cannot michael@0: ** be created michael@0: */ michael@0: NSPR_API(PRBool) PR_SetLogFile(const char *name); michael@0: michael@0: /* michael@0: ** Set the size of the logging buffer. If "buffer_size" is zero then the michael@0: ** logging becomes "synchronous" (or unbuffered). michael@0: */ michael@0: NSPR_API(void) PR_SetLogBuffering(PRIntn buffer_size); michael@0: michael@0: /* michael@0: ** Print a string to the log. "fmt" is a PR_snprintf format type. All michael@0: ** messages printed to the log are preceeded by the name of the thread michael@0: ** and a time stamp. Also, the routine provides a missing newline if one michael@0: ** is not provided. michael@0: */ michael@0: NSPR_API(void) PR_LogPrint(const char *fmt, ...); michael@0: michael@0: /* michael@0: ** Flush the log to its file. michael@0: */ michael@0: NSPR_API(void) PR_LogFlush(void); michael@0: michael@0: NSPR_API(void) PR_Assert(const char *s, const char *file, PRIntn ln); michael@0: michael@0: #if defined(DEBUG) || defined(FORCE_PR_LOG) michael@0: #define PR_LOGGING 1 michael@0: michael@0: #define PR_LOG_TEST(_module,_level) \ michael@0: ((_module)->level >= (_level)) michael@0: michael@0: /* michael@0: ** Log something. michael@0: ** "module" is the address of a PRLogModuleInfo structure michael@0: ** "level" is the desired logging level michael@0: ** "args" is a variable length list of arguments to print, in the following michael@0: ** format: ("printf style format string", ...) michael@0: */ michael@0: #define PR_LOG(_module,_level,_args) \ michael@0: PR_BEGIN_MACRO \ michael@0: if (PR_LOG_TEST(_module,_level)) { \ michael@0: PR_LogPrint _args; \ michael@0: } \ michael@0: PR_END_MACRO michael@0: michael@0: #else /* defined(DEBUG) || defined(FORCE_PR_LOG) */ michael@0: michael@0: #undef PR_LOGGING michael@0: #define PR_LOG_TEST(module,level) 0 michael@0: #define PR_LOG(module,level,args) michael@0: michael@0: #endif /* defined(DEBUG) || defined(FORCE_PR_LOG) */ michael@0: michael@0: #ifndef NO_NSPR_10_SUPPORT michael@0: michael@0: #ifdef PR_LOGGING michael@0: #define PR_LOG_BEGIN PR_LOG michael@0: #define PR_LOG_END PR_LOG michael@0: #define PR_LOG_DEFINE PR_NewLogModule michael@0: #else michael@0: #define PR_LOG_BEGIN(module,level,args) michael@0: #define PR_LOG_END(module,level,args) michael@0: #define PR_LOG_DEFINE(_name) NULL michael@0: #endif /* PR_LOGGING */ michael@0: michael@0: #endif /* NO_NSPR_10_SUPPORT */ michael@0: michael@0: #if defined(DEBUG) || defined(FORCE_PR_ASSERT) michael@0: michael@0: #define PR_ASSERT(_expr) \ michael@0: ((_expr)?((void)0):PR_Assert(# _expr,__FILE__,__LINE__)) michael@0: michael@0: #define PR_NOT_REACHED(_reasonStr) \ michael@0: PR_Assert(_reasonStr,__FILE__,__LINE__) michael@0: michael@0: #else michael@0: michael@0: #define PR_ASSERT(expr) ((void) 0) michael@0: #define PR_NOT_REACHED(reasonStr) michael@0: michael@0: #endif /* defined(DEBUG) || defined(FORCE_PR_ASSERT) */ michael@0: michael@0: PR_END_EXTERN_C michael@0: michael@0: #endif /* prlog_h___ */