prlog.h 6.9 KB

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