prlog.h 8.4 KB

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