implement DbgPrintEx, vDbgPrintEx, and vDbgPrintExWithPrefix
Modified: trunk/reactos/ntoskrnl/rtl/debug.c

Modified: trunk/reactos/ntoskrnl/rtl/debug.c
--- trunk/reactos/ntoskrnl/rtl/debug.c	2005-08-14 04:22:07 UTC (rev 17377)
+++ trunk/reactos/ntoskrnl/rtl/debug.c	2005-08-14 04:24:01 UTC (rev 17378)
@@ -12,6 +12,17 @@
 #include <ntoskrnl.h>
 #include <internal/debug.h>
 
+/* DATA *********************************************************************/
+
+typedef struct
+{
+	ULONG ComponentId;
+	ULONG Level;
+} KD_COMPONENT_DATA;
+#define MAX_KD_COMPONENT_TABLE_ENTRIES 128
+KD_COMPONENT_DATA KdComponentTable[MAX_KD_COMPONENT_TABLE_ENTRIES];
+ULONG KdComponentTableEntries = 0;
+
 /* FUNCTIONS ****************************************************************/
 
 /*
@@ -23,12 +34,17 @@
 /*
  * @implemented
  */
-ULONG
-DbgPrint(PCH Format, ...)
+ULONG WINAPI
+vDbgPrintExWithPrefix(IN LPCSTR Prefix,
+                      IN ULONG ComponentId,
+                      IN ULONG Level,
+                      IN LPCSTR Format,
+                      IN va_list ap)
 {
    ANSI_STRING DebugString;
-   CHAR Buffer[1024];
-   va_list ap;
+   CHAR Buffer[513];
+   PCHAR pBuffer;
+   ULONG pBufferSize;
 #ifdef SERIALIZE_DBGPRINT
 #  define MESSAGETABLE_SIZE  16
    LONG MyTableIndex;
@@ -37,14 +53,31 @@
    static CHAR MessageTable[MESSAGETABLE_SIZE][sizeof(Buffer)] = { { '\0' } };
 #endif /* SERIALIZE_DBGPRINT */
 
+   /* TODO FIXME - call NtQueryDebugFilterState() instead per Alex */
+   if ( !DbgQueryDebugFilterState ( ComponentId, Level ) )
+      return 0;
+
    /* init ansi string */
    DebugString.Buffer = Buffer;
    DebugString.MaximumLength = sizeof(Buffer);
 
-   va_start (ap, Format);
-   DebugString.Length = _vsnprintf (Buffer, sizeof( Buffer ), Format, ap);
-   va_end (ap);
+   pBuffer = Buffer;
+   pBufferSize = sizeof(Buffer);
+   DebugString.Length = 0;
+   if ( Prefix && *Prefix )
+   {
+      DebugString.Length = strlen(Prefix);
+      if ( DebugString.Length >= sizeof(Buffer) )
+         DebugString.Length = sizeof(Buffer) - 1;
+      memmove ( Buffer, Prefix, DebugString.Length );
+      Buffer[DebugString.Length] = '\0';
+      pBuffer = &Buffer[DebugString.Length];
+      pBufferSize -= DebugString.Length;
+   }
 
+   DebugString.Length += _vsnprintf ( pBuffer, pBufferSize, Format, ap );
+   Buffer[sizeof(Buffer)-1] = '\0';
+
 #ifdef SERIALIZE_DBGPRINT
    /* check if we are already running */
    if (InterlockedCompareExchange(&Lock, 1, 0) == 1)
@@ -62,9 +95,6 @@
           }
         else
           {
-             /*DebugString.Buffer = "ÁÁÁ";
-             DebugString.Length = 3;
-             KdpPrintString(&DebugString);*/
              memcpy(MessageTable[MyTableIndex], DebugString.Buffer, DebugString.Length);
              MessageTable[MyTableIndex][DebugString.Length] = '\0';
           }
@@ -101,17 +131,60 @@
 }
 
 /*
- * @unimplemented
+ * @implemented
  */
+ULONG WINAPI
+vDbgPrintEx(IN ULONG ComponentId,
+            IN ULONG Level,
+            IN LPCSTR Format,
+            IN va_list ap)
+{
+	return vDbgPrintExWithPrefix ( NULL, ComponentId, Level, Format, ap );
+}
+
+/*
+ * @implemented
+ */
 ULONG
+DbgPrint(PCH Format, ...)
+{
+	va_list ap;
+	ULONG rc;
+
+	va_start (ap, Format);
+	/* TODO FIXME - use DPFLTR_DEFAULT_ID and DPFLTR_INFO_LEVEL
+	 *
+	 * https://www.osronline.com/article.cfm?article=295
+	 *
+	 * ( first need to add those items to registry by default tho so we don't anger
+	 *  ros-devs when DbgPrint() suddenly stops working )
+	 *
+	 * ( also when you do this, remove -1 hack from DbgQueryDebugFilterState() )
+	 */
+	rc = vDbgPrintExWithPrefix ( NULL, (ULONG)-1, (ULONG)-1, Format, ap );
+	va_end (ap);
+
+	return rc;
+}
+
+/*
+ * @implemented
+ */
+ULONG
 __cdecl
 DbgPrintEx(IN ULONG ComponentId,
            IN ULONG Level,
            IN PCH Format,
            ...)
 {
-    UNIMPLEMENTED;
-    return 0;
+	va_list ap;
+	ULONG rc;
+
+	va_start (ap, Format);
+	rc = vDbgPrintExWithPrefix ( NULL, ComponentId, Level, Format, ap );
+	va_end (ap);
+
+	return rc;
 }
 
 /*
@@ -151,19 +224,36 @@
 }
 
 /*
- * @unimplemented
+ * @implemented
  */
-NTSTATUS
+BOOLEAN
 STDCALL
 DbgQueryDebugFilterState(IN ULONG ComponentId,
                          IN ULONG Level)
 {
-    UNIMPLEMENTED;
-    return STATUS_NOT_IMPLEMENTED;
+	int i;
+
+	if ( ComponentId == -1 )
+		return TRUE;
+
+	/* convert Level to mask if it isn't already one */
+	if ( Level < 32 )
+		Level = 1 << Level;
+
+	for ( i = 0; i < KdComponentTableEntries; i++ )
+	{
+		if ( ComponentId == KdComponentTable[i].ComponentId )
+		{
+			if ( Level & KdComponentTable[i].Level )
+				return TRUE;
+			break;
+		}
+	}
+	return FALSE;
 }
 
 /*
- * @unimplemented
+ * @implemented
  */
 NTSTATUS
 STDCALL
@@ -171,8 +261,25 @@
                        IN ULONG Level,
                        IN BOOLEAN State)
 {
-    UNIMPLEMENTED;
-    return STATUS_NOT_IMPLEMENTED;
+	int i;
+	for ( i = 0; i < KdComponentTableEntries; i++ )
+	{
+		if ( ComponentId == KdComponentTable[i].ComponentId )
+			break;
+	}
+	if ( i == KdComponentTableEntries )
+	{
+		if ( i == MAX_KD_COMPONENT_TABLE_ENTRIES )
+			return STATUS_INVALID_PARAMETER_1;
+		++KdComponentTableEntries;
+		KdComponentTable[i].ComponentId = ComponentId;
+		KdComponentTable[i].Level = 0;
+	}
+	if ( State )
+		KdComponentTable[i].Level |= Level;
+	else
+		KdComponentTable[i].Level &= ~Level;
+	return STATUS_SUCCESS;
 }
 
 /*