debug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. Samba utility functions
  5. Copyright (C) Andrew Tridgell 1992-1998
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include "includes.h"
  19. /* -------------------------------------------------------------------------- **
  20. * Defines...
  21. *
  22. * FORMAT_BUFR_MAX - Index of the last byte of the format buffer;
  23. * format_bufr[FORMAT_BUFR_MAX] should always be reserved
  24. * for a terminating nul byte.
  25. */
  26. #define FORMAT_BUFR_MAX ( sizeof( format_bufr ) - 1 )
  27. /* -------------------------------------------------------------------------- **
  28. * This module implements Samba's debugging utility.
  29. *
  30. * The syntax of a debugging log file is represented as:
  31. *
  32. * <debugfile> :== { <debugmsg> }
  33. *
  34. * <debugmsg> :== <debughdr> '\n' <debugtext>
  35. *
  36. * <debughdr> :== '[' TIME ',' LEVEL ']' [ [FILENAME ':'] [FUNCTION '()'] ]
  37. *
  38. * <debugtext> :== { <debugline> }
  39. *
  40. * <debugline> :== TEXT '\n'
  41. *
  42. * TEXT is a string of characters excluding the newline character.
  43. * LEVEL is the DEBUG level of the message (an integer in the range 0..10).
  44. * TIME is a timestamp.
  45. * FILENAME is the name of the file from which the debug message was generated.
  46. * FUNCTION is the function from which the debug message was generated.
  47. *
  48. * Basically, what that all means is:
  49. *
  50. * - A debugging log file is made up of debug messages.
  51. *
  52. * - Each debug message is made up of a header and text. The header is
  53. * separated from the text by a newline.
  54. *
  55. * - The header begins with the timestamp and debug level of the message
  56. * enclosed in brackets. The filename and function from which the
  57. * message was generated may follow. The filename is terminated by a
  58. * colon, and the function name is terminated by parenthesis.
  59. *
  60. * - The message text is made up of zero or more lines, each terminated by
  61. * a newline.
  62. */
  63. /* -------------------------------------------------------------------------- **
  64. * External variables.
  65. *
  66. * dbf - Global debug file handle.
  67. * debugf - Debug file name.
  68. * append_log - If True, then the output file will be opened in append
  69. * mode.
  70. * DEBUGLEVEL - System-wide debug message limit. Messages with message-
  71. * levels higher than DEBUGLEVEL will not be processed.
  72. */
  73. FILE *dbf = NULL;
  74. pstring debugf = "";
  75. BOOL append_log = False;
  76. int DEBUGLEVEL = 1;
  77. /* -------------------------------------------------------------------------- **
  78. * Internal variables.
  79. *
  80. * stdout_logging - Default False, if set to True then dbf will be set to
  81. * stdout and debug output will go to dbf only, and not
  82. * to syslog. Set in setup_logging() and read in Debug1().
  83. *
  84. * syslog_level - Internal copy of the message debug level. Written by
  85. * dbghdr() and read by Debug1().
  86. *
  87. * format_bufr - Used to format debug messages. The dbgtext() function
  88. * prints debug messages to a string, and then passes the
  89. * string to format_debug_text(), which uses format_bufr
  90. * to build the formatted output.
  91. *
  92. * format_pos - Marks the first free byte of the format_bufr.
  93. */
  94. static BOOL stdout_logging = False;
  95. static pstring format_bufr = { '\0' };
  96. static size_t format_pos = 0;
  97. /* -------------------------------------------------------------------------- **
  98. * Functions...
  99. */
  100. /* ************************************************************************** **
  101. * get ready for syslog stuff
  102. * ************************************************************************** **
  103. */
  104. void setup_logging( const char *pname, BOOL interactive )
  105. {
  106. (void) pname;
  107. if( interactive )
  108. {
  109. stdout_logging = True;
  110. dbf = stderr;
  111. }
  112. } /* setup_logging */
  113. /* ************************************************************************** **
  114. * Write an debug message on the debugfile.
  115. * This is called by dbghdr() and format_debug_text().
  116. * ************************************************************************** **
  117. */
  118. #ifdef HAVE_STDARG_H
  119. int Debug1( const char *format_str, ... )
  120. {
  121. #else
  122. int Debug1(va_alist)
  123. va_dcl
  124. {
  125. const char *format_str;
  126. #endif
  127. va_list ap;
  128. int old_errno = errno;
  129. if( stdout_logging )
  130. {
  131. #ifdef HAVE_STDARG_H
  132. va_start( ap, format_str );
  133. #else
  134. va_start( ap );
  135. format_str = va_arg( ap, const char * );
  136. #endif
  137. (void)vfprintf( dbf, format_str, ap );
  138. va_end( ap );
  139. errno = old_errno;
  140. return( 0 );
  141. }
  142. if( !dbf && *debugf)
  143. {
  144. mode_t oldumask = umask( 022 );
  145. if( append_log )
  146. dbf = sys_fopen( debugf, "a" );
  147. else
  148. dbf = sys_fopen( debugf, "w" );
  149. (void)umask( oldumask );
  150. if( dbf )
  151. {
  152. setbuf( dbf, NULL );
  153. }
  154. else
  155. {
  156. errno = old_errno;
  157. return(0);
  158. }
  159. }
  160. if (dbf)
  161. {
  162. #ifdef HAVE_STDARG_H
  163. va_start( ap, format_str );
  164. #else
  165. va_start( ap );
  166. format_str = va_arg( ap, const char * );
  167. #endif
  168. (void)vfprintf( dbf, format_str, ap );
  169. va_end( ap );
  170. (void)fflush( dbf );
  171. }
  172. errno = old_errno;
  173. return( 0 );
  174. } /* Debug1 */
  175. /* ************************************************************************** **
  176. * Print the buffer content via Debug1(), then reset the buffer.
  177. *
  178. * Input: none
  179. * Output: none
  180. *
  181. * ************************************************************************** **
  182. */
  183. static void bufr_print( void )
  184. {
  185. format_bufr[format_pos] = '\0';
  186. (void)Debug1( "%s", format_bufr );
  187. format_pos = 0;
  188. } /* bufr_print */
  189. /* ************************************************************************** **
  190. * Format the debug message text.
  191. *
  192. * Input: msg - Text to be added to the "current" debug message text.
  193. *
  194. * Output: none.
  195. *
  196. * Notes: The purpose of this is two-fold. First, each call to syslog()
  197. * (used by Debug1(), see above) generates a new line of syslog
  198. * output. This is fixed by storing the partial lines until the
  199. * newline character is encountered. Second, printing the debug
  200. * message lines when a newline is encountered allows us to add
  201. * spaces, thus indenting the body of the message and making it
  202. * more readable.
  203. *
  204. * ************************************************************************** **
  205. */
  206. static void format_debug_text( char *msg )
  207. {
  208. size_t i;
  209. BOOL timestamp = (!stdout_logging && (lp_timestamp_logs() ||
  210. !(lp_loaded())));
  211. for( i = 0; msg[i]; i++ )
  212. {
  213. /* Indent two spaces at each new line. */
  214. if(timestamp && 0 == format_pos)
  215. {
  216. format_bufr[0] = format_bufr[1] = ' ';
  217. format_pos = 2;
  218. }
  219. /* If there's room, copy the character to the format buffer. */
  220. if( format_pos < FORMAT_BUFR_MAX )
  221. format_bufr[format_pos++] = msg[i];
  222. /* If a newline is encountered, print & restart. */
  223. if( '\n' == msg[i] )
  224. bufr_print();
  225. /* If the buffer is full dump it out, reset it, and put out a line
  226. * continuation indicator.
  227. */
  228. if( format_pos >= FORMAT_BUFR_MAX )
  229. {
  230. bufr_print();
  231. (void)Debug1( " +>\n" );
  232. }
  233. }
  234. /* Just to be safe... */
  235. format_bufr[format_pos] = '\0';
  236. } /* format_debug_text */
  237. /* ************************************************************************** **
  238. * Flush debug output, including the format buffer content.
  239. *
  240. * Input: none
  241. * Output: none
  242. *
  243. * ************************************************************************** **
  244. */
  245. void dbgflush( void )
  246. {
  247. bufr_print();
  248. (void)fflush( dbf );
  249. } /* dbgflush */
  250. /* ************************************************************************** **
  251. * Print a Debug Header.
  252. *
  253. * Input: level - Debug level of the message (not the system-wide debug
  254. * level.
  255. * file - Pointer to a string containing the name of the file
  256. * from which this function was called, or an empty string
  257. * if the __FILE__ macro is not implemented.
  258. * func - Pointer to a string containing the name of the function
  259. * from which this function was called, or an empty string
  260. * if the __FUNCTION__ macro is not implemented.
  261. * line - line number of the call to dbghdr, assuming __LINE__
  262. * works.
  263. *
  264. * Output: Always True. This makes it easy to fudge a call to dbghdr()
  265. * in a macro, since the function can be called as part of a test.
  266. * Eg: ( (level <= DEBUGLEVEL) && (dbghdr(level,"",line)) )
  267. *
  268. * Notes: This function takes care of setting syslog_level.
  269. *
  270. * ************************************************************************** **
  271. */
  272. BOOL dbghdr( int level, const char *file, const char *func, int line )
  273. {
  274. if( format_pos )
  275. {
  276. /* This is a fudge. If there is stuff sitting in the format_bufr, then
  277. * the *right* thing to do is to call
  278. * format_debug_text( "\n" );
  279. * to write the remainder, and then proceed with the new header.
  280. * Unfortunately, there are several places in the code at which
  281. * the DEBUG() macro is used to build partial lines. That in mind,
  282. * we'll work under the assumption that an incomplete line indicates
  283. * that a new header is *not* desired.
  284. */
  285. return( True );
  286. }
  287. /* Don't print a header if we're logging to stdout. */
  288. if( stdout_logging )
  289. return( True );
  290. /* Print the header if timestamps are turned on. If parameters are
  291. * not yet loaded, then default to timestamps on.
  292. */
  293. if( lp_timestamp_logs() || !(lp_loaded()) )
  294. {
  295. /* Print it all out at once to prevent split syslog output. */
  296. (void)Debug1( "[%s, %d] %s:%s(%d)\n",
  297. timestring(), level, file, func, line );
  298. }
  299. return( True );
  300. } /* dbghdr */
  301. /* ************************************************************************** **
  302. * Add text to the body of the "current" debug message via the format buffer.
  303. *
  304. * Input: format_str - Format string, as used in printf(), et. al.
  305. * ... - Variable argument list.
  306. *
  307. * ..or.. va_alist - Old style variable parameter list starting point.
  308. *
  309. * Output: Always True. See dbghdr() for more info, though this is not
  310. * likely to be used in the same way.
  311. *
  312. * ************************************************************************** **
  313. */
  314. #ifdef HAVE_STDARG_H
  315. BOOL dbgtext( const char *format_str, ... )
  316. {
  317. va_list ap;
  318. pstring msgbuf;
  319. va_start( ap, format_str );
  320. vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
  321. va_end( ap );
  322. format_debug_text( msgbuf );
  323. return( True );
  324. } /* dbgtext */
  325. #else
  326. BOOL dbgtext( va_alist )
  327. va_dcl
  328. {
  329. char *format_str;
  330. va_list ap;
  331. pstring msgbuf;
  332. va_start( ap );
  333. format_str = va_arg( ap, char * );
  334. vslprintf( msgbuf, sizeof(msgbuf)-1, format_str, ap );
  335. va_end( ap );
  336. format_debug_text( msgbuf );
  337. return( True );
  338. } /* dbgtext */
  339. #endif
  340. /* ************************************************************************** */