diff --git a/src/add-ons/kernel/file_systems/udf/UdfDebug.cpp b/src/add-ons/kernel/file_systems/udf/UdfDebug.cpp index 1cdf2509c1..f934350aeb 100644 --- a/src/add-ons/kernel/file_systems/udf/UdfDebug.cpp +++ b/src/add-ons/kernel/file_systems/udf/UdfDebug.cpp @@ -272,6 +272,70 @@ get_tls_handle() } #endif +/*! \brief Helper class for initializing the debugging output + file. + + Note that this hummer isn't threadsafe, but it doesn't really + matter for our concerns, since the worst it'll result in is + a dangling file descriptor, and that would be in the case of + two or more volumes being mounted almost simultaneously... + not too big of a worry. +*/ +class DebugOutputFile { +public: + DebugOutputFile(const char *filename = NULL) + : fFile(-1) + { + Init(filename); + } + + ~DebugOutputFile() { + if (fFile >= 0) + close(fFile); + } + + void Init(const char *filename) { + if (fFile < 0 && filename) + fFile = open(filename, O_RDWR | O_CREAT | O_TRUNC); + } + + int File() const { return fFile; } +private: + int fFile; +}; + +DebugOutputFile out; + +/*! \brief It doesn't appear that the constructor for the global + \c out variable is called when built as an R5 filesystem add-on, + so this function needs to be called in udf_mount to let the + magic happen. +*/ +void initialize_debugger(const char *filename) +{ + out.Init(filename); +} + +// dbg_printf, stolen from Ingo's ReiserFS::Debug.cpp. +void +dbg_printf(const char *format,...) +{ +#if DEBUG_TO_FILE + char buffer[1024]; + va_list args; + va_start(args, format); + // no vsnprintf() on PPC and in kernel + #if defined(__INTEL__) && USER + vsnprintf(buffer, sizeof(buffer) - 1, format, args); + #else + vsprintf(buffer, format, args); + #endif + va_end(args); + buffer[sizeof(buffer) - 1] = '\0'; + write(out.File(), buffer, strlen(buffer)); +#endif +} + //---------------------------------------------------------------------- // DebugHelper //---------------------------------------------------------------------- diff --git a/src/add-ons/kernel/file_systems/udf/UdfDebug.h b/src/add-ons/kernel/file_systems/udf/UdfDebug.h index c28983d5aa..5b5cc9b008 100644 --- a/src/add-ons/kernel/file_systems/udf/UdfDebug.h +++ b/src/add-ons/kernel/file_systems/udf/UdfDebug.h @@ -4,6 +4,7 @@ // // UDF version copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net // Initial version copyright (c) 2002 Axel Dörfler, axeld@pinc-software.de +// dbg_printf() function copyright (c) 2003 Ingo Weinhold, bonefish@cs.tu-berlin.edu //---------------------------------------------------------------------- #ifndef UDF_DEBUG_H #define UDF_DEBUG_H @@ -19,13 +20,24 @@ #ifdef DEBUG # include #endif +#include -#ifdef USER +#define DEBUG_TO_FILE 0 + +#if DEBUG_TO_FILE # include -# define __out printf +# include +# define __out dbg_printf + void dbg_printf(const char *format,...); + void initialize_debugger(const char *filename); #else -# include -# define __out dprintf +# ifdef USER +# include +# define __out printf +# else +# include +# define __out dprintf +# endif #endif #include "cpp.h" @@ -119,6 +131,12 @@ private: // DEBUG-dependent macros //---------------------------------------------------------------------- #ifdef DEBUG + #if DEBUG_TO_FILE + #define INITIALIZE_DEBUGGING_OUTPUT_FILE(filename) initialize_debugger(filename); + #else + #define INITIALIZE_DEBUGGING_OUTPUT_FILE(filename) ; + #endif + #define DEBUG_INIT_SILENT(categoryFlags, className) \ DebugHelper _debugHelper((categoryFlags), className, 2); @@ -243,6 +261,7 @@ private: #define DBG(x) x ; #else // ifdef DEBUG + #define INITIALIZE_DEBUGGING_OUTPUT_FILE(filename) ; #define DEBUG_INIT_SILENT(categoryFlags, className) ; #define DEBUG_INIT(categoryFlags, className) ; #define DEBUG_INIT_ETC(categoryFlags, className, arguments) ;