nsprpub/pr/include/prlog.h

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef prlog_h___
michael@0 7 #define prlog_h___
michael@0 8
michael@0 9 #include "prtypes.h"
michael@0 10
michael@0 11 PR_BEGIN_EXTERN_C
michael@0 12
michael@0 13 /*
michael@0 14 ** prlog.h -- Declare interfaces to NSPR's Logging service
michael@0 15 **
michael@0 16 ** NSPR provides a logging service that is used by NSPR itself and is
michael@0 17 ** available to client programs.
michael@0 18 **
michael@0 19 ** To use the service from a client program, you should create a
michael@0 20 ** PRLogModuleInfo structure by calling PR_NewLogModule(). After
michael@0 21 ** creating the LogModule, you can write to the log using the PR_LOG()
michael@0 22 ** macro.
michael@0 23 **
michael@0 24 ** Initialization of the log service is handled by NSPR initialization.
michael@0 25 **
michael@0 26 ** At execution time, you must enable the log service. To enable the
michael@0 27 ** log service, set the environment variable: NSPR_LOG_MODULES
michael@0 28 ** variable.
michael@0 29 **
michael@0 30 ** NSPR_LOG_MODULES variable has the form:
michael@0 31 **
michael@0 32 ** <moduleName>:<value>[, <moduleName>:<value>]*
michael@0 33 **
michael@0 34 ** Where:
michael@0 35 ** <moduleName> is the name passed to PR_NewLogModule().
michael@0 36 ** <value> is a numeric constant, e.g. 5. This value is the maximum
michael@0 37 ** value of a log event, enumerated by PRLogModuleLevel, that you want
michael@0 38 ** written to the log.
michael@0 39 **
michael@0 40 ** For example: to record all events of greater value than or equal to
michael@0 41 ** PR_LOG_ERROR for a LogModule names "gizmo", say:
michael@0 42 **
michael@0 43 ** set NSPR_LOG_MODULES=gizmo:2
michael@0 44 **
michael@0 45 ** Note that you must specify the numeric value of PR_LOG_ERROR.
michael@0 46 **
michael@0 47 ** Special LogModule names are provided for controlling NSPR's log
michael@0 48 ** service at execution time. These controls should be set in the
michael@0 49 ** NSPR_LOG_MODULES environment variable at execution time to affect
michael@0 50 ** NSPR's log service for your application.
michael@0 51 **
michael@0 52 ** The special LogModule "all" enables all LogModules. To enable all
michael@0 53 ** LogModule calls to PR_LOG(), say:
michael@0 54 **
michael@0 55 ** set NSPR_LOG_MODULES=all:5
michael@0 56 **
michael@0 57 ** The special LogModule name "sync" tells the NSPR log service to do
michael@0 58 ** unbuffered logging.
michael@0 59 **
michael@0 60 ** The special LogModule name "bufsize:<size>" tells NSPR to set the
michael@0 61 ** log buffer to <size>.
michael@0 62 **
michael@0 63 ** The environment variable NSPR_LOG_FILE specifies the log file to use
michael@0 64 ** unless the default of "stderr" is acceptable. For MS Windows
michael@0 65 ** systems, NSPR_LOG_FILE can be set to a special value: "WinDebug"
michael@0 66 ** (case sensitive). This value causes PR_LOG() output to be written
michael@0 67 ** using the Windows API OutputDebugString(). OutputDebugString()
michael@0 68 ** writes to the debugger window; some people find this helpful.
michael@0 69 **
michael@0 70 **
michael@0 71 ** To put log messages in your programs, use the PR_LOG macro:
michael@0 72 **
michael@0 73 ** PR_LOG(<module>, <level>, (<printfString>, <args>*));
michael@0 74 **
michael@0 75 ** Where <module> is the address of a PRLogModuleInfo structure, and
michael@0 76 ** <level> is one of the levels defined by the enumeration:
michael@0 77 ** PRLogModuleLevel. <args> is a printf() style of argument list. That
michael@0 78 ** is: (fmtstring, ...).
michael@0 79 **
michael@0 80 ** Example:
michael@0 81 **
michael@0 82 ** main() {
michael@0 83 ** PRIntn one = 1;
michael@0 84 ** PRLogModuleInfo * myLm = PR_NewLogModule("gizmo");
michael@0 85 ** PR_LOG( myLm, PR_LOG_ALWAYS, ("Log this! %d\n", one));
michael@0 86 ** return;
michael@0 87 ** }
michael@0 88 **
michael@0 89 ** Note the use of printf() style arguments as the third agrument(s) to
michael@0 90 ** PR_LOG().
michael@0 91 **
michael@0 92 ** After compiling and linking you application, set the environment:
michael@0 93 **
michael@0 94 ** set NSPR_LOG_MODULES=gizmo:5
michael@0 95 ** set NSPR_LOG_FILE=logfile.txt
michael@0 96 **
michael@0 97 ** When you execute your application, the string "Log this! 1" will be
michael@0 98 ** written to the file "logfile.txt".
michael@0 99 **
michael@0 100 ** Note to NSPR engineers: a number of PRLogModuleInfo structures are
michael@0 101 ** defined and initialized in prinit.c. See this module for ideas on
michael@0 102 ** what to log where.
michael@0 103 **
michael@0 104 */
michael@0 105
michael@0 106 typedef enum PRLogModuleLevel {
michael@0 107 PR_LOG_NONE = 0, /* nothing */
michael@0 108 PR_LOG_ALWAYS = 1, /* always printed */
michael@0 109 PR_LOG_ERROR = 2, /* error messages */
michael@0 110 PR_LOG_WARNING = 3, /* warning messages */
michael@0 111 PR_LOG_DEBUG = 4, /* debug messages */
michael@0 112
michael@0 113 PR_LOG_NOTICE = PR_LOG_DEBUG, /* notice messages */
michael@0 114 PR_LOG_WARN = PR_LOG_WARNING, /* warning messages */
michael@0 115 PR_LOG_MIN = PR_LOG_DEBUG, /* minimal debugging messages */
michael@0 116 PR_LOG_MAX = PR_LOG_DEBUG /* maximal debugging messages */
michael@0 117 } PRLogModuleLevel;
michael@0 118
michael@0 119 /*
michael@0 120 ** One of these structures is created for each module that uses logging.
michael@0 121 ** "name" is the name of the module
michael@0 122 ** "level" is the debugging level selected for that module
michael@0 123 */
michael@0 124 typedef struct PRLogModuleInfo {
michael@0 125 const char *name;
michael@0 126 PRLogModuleLevel level;
michael@0 127 struct PRLogModuleInfo *next;
michael@0 128 } PRLogModuleInfo;
michael@0 129
michael@0 130 /*
michael@0 131 ** Create a new log module.
michael@0 132 */
michael@0 133 NSPR_API(PRLogModuleInfo*) PR_NewLogModule(const char *name);
michael@0 134
michael@0 135 /*
michael@0 136 ** Set the file to use for logging. Returns PR_FALSE if the file cannot
michael@0 137 ** be created
michael@0 138 */
michael@0 139 NSPR_API(PRBool) PR_SetLogFile(const char *name);
michael@0 140
michael@0 141 /*
michael@0 142 ** Set the size of the logging buffer. If "buffer_size" is zero then the
michael@0 143 ** logging becomes "synchronous" (or unbuffered).
michael@0 144 */
michael@0 145 NSPR_API(void) PR_SetLogBuffering(PRIntn buffer_size);
michael@0 146
michael@0 147 /*
michael@0 148 ** Print a string to the log. "fmt" is a PR_snprintf format type. All
michael@0 149 ** messages printed to the log are preceeded by the name of the thread
michael@0 150 ** and a time stamp. Also, the routine provides a missing newline if one
michael@0 151 ** is not provided.
michael@0 152 */
michael@0 153 NSPR_API(void) PR_LogPrint(const char *fmt, ...);
michael@0 154
michael@0 155 /*
michael@0 156 ** Flush the log to its file.
michael@0 157 */
michael@0 158 NSPR_API(void) PR_LogFlush(void);
michael@0 159
michael@0 160 NSPR_API(void) PR_Assert(const char *s, const char *file, PRIntn ln);
michael@0 161
michael@0 162 #if defined(DEBUG) || defined(FORCE_PR_LOG)
michael@0 163 #define PR_LOGGING 1
michael@0 164
michael@0 165 #define PR_LOG_TEST(_module,_level) \
michael@0 166 ((_module)->level >= (_level))
michael@0 167
michael@0 168 /*
michael@0 169 ** Log something.
michael@0 170 ** "module" is the address of a PRLogModuleInfo structure
michael@0 171 ** "level" is the desired logging level
michael@0 172 ** "args" is a variable length list of arguments to print, in the following
michael@0 173 ** format: ("printf style format string", ...)
michael@0 174 */
michael@0 175 #define PR_LOG(_module,_level,_args) \
michael@0 176 PR_BEGIN_MACRO \
michael@0 177 if (PR_LOG_TEST(_module,_level)) { \
michael@0 178 PR_LogPrint _args; \
michael@0 179 } \
michael@0 180 PR_END_MACRO
michael@0 181
michael@0 182 #else /* defined(DEBUG) || defined(FORCE_PR_LOG) */
michael@0 183
michael@0 184 #undef PR_LOGGING
michael@0 185 #define PR_LOG_TEST(module,level) 0
michael@0 186 #define PR_LOG(module,level,args)
michael@0 187
michael@0 188 #endif /* defined(DEBUG) || defined(FORCE_PR_LOG) */
michael@0 189
michael@0 190 #ifndef NO_NSPR_10_SUPPORT
michael@0 191
michael@0 192 #ifdef PR_LOGGING
michael@0 193 #define PR_LOG_BEGIN PR_LOG
michael@0 194 #define PR_LOG_END PR_LOG
michael@0 195 #define PR_LOG_DEFINE PR_NewLogModule
michael@0 196 #else
michael@0 197 #define PR_LOG_BEGIN(module,level,args)
michael@0 198 #define PR_LOG_END(module,level,args)
michael@0 199 #define PR_LOG_DEFINE(_name) NULL
michael@0 200 #endif /* PR_LOGGING */
michael@0 201
michael@0 202 #endif /* NO_NSPR_10_SUPPORT */
michael@0 203
michael@0 204 #if defined(DEBUG) || defined(FORCE_PR_ASSERT)
michael@0 205
michael@0 206 #define PR_ASSERT(_expr) \
michael@0 207 ((_expr)?((void)0):PR_Assert(# _expr,__FILE__,__LINE__))
michael@0 208
michael@0 209 #define PR_NOT_REACHED(_reasonStr) \
michael@0 210 PR_Assert(_reasonStr,__FILE__,__LINE__)
michael@0 211
michael@0 212 #else
michael@0 213
michael@0 214 #define PR_ASSERT(expr) ((void) 0)
michael@0 215 #define PR_NOT_REACHED(reasonStr)
michael@0 216
michael@0 217 #endif /* defined(DEBUG) || defined(FORCE_PR_ASSERT) */
michael@0 218
michael@0 219 PR_END_EXTERN_C
michael@0 220
michael@0 221 #endif /* prlog_h___ */

mercurial