static FORCEINLINE PCHAR #pragma warning(suppress: 28195) __DriverFormatV( __in PCHAR Fmt, __in va_list Args ) { NTSTATUS Status; PCHAR Str; ULONG Size = 32; for (;;) { Str = (PCHAR)__AllocateNonPagedPoolWithTag(__FUNCTION__, __LINE__, Size, FORMAT_POOL_TAG); if (!Str) { return NULL; } Status = RtlStringCchVPrintfA(Str, Size - 1, Fmt, Args); if (Status == STATUS_SUCCESS) { Str[Size - 1] = '\0'; return Str; } __FreePoolWithTag(Str, FORMAT_POOL_TAG); if (Status == STATUS_BUFFER_OVERFLOW) { Size *= 2; } else { return NULL; } } }
/** * Adds a string to the data recorder. * * \return NDIS_STATUS */ NDIS_STATUS SaveToDataRecorder(PMP_DATA_RECORDER_OBJ Recorder, __format_string __nullterminated CHAR *Format, ...) { ULONG myIndex; va_list args; if (Recorder->RecordList) { myIndex = NdisInterlockedIncrement((PLONG)&(Recorder->CurrentIndex)) % Recorder->MaxLength; Recorder->RecordList[myIndex].DataType = MP_STRING; // Format the message va_start(args, Format); if (NT_SUCCESS(RtlStringCchVPrintfA( Recorder->RecordList[myIndex].Data.String, MAX_RECORDED_STRING_LENGTH, Format, args)) ) { Recorder->RecordList[myIndex].Data.String[MAX_RECORDED_STRING_LENGTH-1] = 0; } va_end (args); } return NDIS_STATUS_SUCCESS; }
//============================================================================= // Global Functions static FORCEINLINE PCHAR __DriverFormatV( IN PCHAR Fmt, IN va_list Args ) { NTSTATUS Status; PCHAR Str; ULONG Size = 32; for (;;) { Str = (PCHAR)AustereAllocate(Size); if (!Str) { return NULL; } RtlZeroMemory(Str, Size); Status = RtlStringCchVPrintfA(Str, Size - 1, Fmt, Args); if (Status == STATUS_SUCCESS) { Str[Size - 1] = '\0'; return Str; } AustereFree(Str); if (Status == STATUS_BUFFER_OVERFLOW) { Size *= 2; } else { return NULL; } } }
__inline KDBG(int nLevel, char* msg, ...) { static char csmsg[1024]; va_list argp; va_start(argp, msg); RtlStringCchVPrintfA(csmsg, 1024, msg, argp); va_end(argp); KdPrintEx((DPFLTR_DEFAULT_ID, nLevel, "V-ASIO:"));// 格式化头 KdPrintEx((DPFLTR_DEFAULT_ID, nLevel, csmsg)); // Log body KdPrintEx((DPFLTR_DEFAULT_ID, nLevel, "\n")); // 换行 }
// Actual implementation of logging API. _Use_decl_annotations_ NTSTATUS LogpPrint(ULONG level, const char *function_name, const char *format, ...) { auto status = STATUS_SUCCESS; if (!LogpIsLogNeeded(level)) { return status; } va_list args; va_start(args, format); char log_message[412]; status = RtlStringCchVPrintfA(log_message, RTL_NUMBER_OF(log_message), format, args); va_end(args); if (!NT_SUCCESS(status)) { NT_ASSERT(false); return status; } if (log_message[0] == '\0') { return STATUS_INVALID_PARAMETER; } const auto pure_level = level & 0xf0; const auto attribute = level & 0x0f; // A single entry of log should not exceed 512 bytes. See // Reading and Filtering Debugging Messages in MSDN for details. char message[512]; static_assert(RTL_NUMBER_OF(message) <= 512, "One log message should not exceed 512 bytes."); status = LogpMakePrefix(pure_level, function_name, log_message, message, RTL_NUMBER_OF(message)); if (!NT_SUCCESS(status)) { NT_ASSERT(false); return status; } status = LogpPut(message, attribute); NT_ASSERT(NT_SUCCESS(status)); return status; }
// Actual implementation of logging API. EXTERN_C NTSTATUS LogpPrint(_In_ ULONG Level, _In_ const char *FunctionName, _In_ const char *Format, ...) { auto status = STATUS_SUCCESS; if (!LogpIsLogNeeded(Level)) { return status; } va_list args; va_start(args, Format); char logMessage[412]; status = RtlStringCchVPrintfA(logMessage, RTL_NUMBER_OF(logMessage), Format, args); va_end(args); if (!NT_SUCCESS(status)) { return status; } if (logMessage[0] == '\0') { return STATUS_INVALID_PARAMETER; } const auto pureLevel = Level & 0xf0; const auto attribute = Level & 0x0f; // A single entry of log should not exceed 512 bytes. See // Reading and Filtering Debugging Messages in MSDN for details. char message[512]; static_assert(RTL_NUMBER_OF(message) <= 512, "One log message should not exceed 512 bytes."); status = LogpMakePrefix(pureLevel, FunctionName, logMessage, message, RTL_NUMBER_OF(message)); if (!NT_SUCCESS(status)) { return status; } return LogpPut(message, attribute); }
VOID __cdecl LogDbgMsg( IN NTSTATUS ErrorCode, IN NTSTATUS NTStatus OPTIONAL, _In_z_ LPCSTR pszFormat, ... ) { #define MAX_ERRMSG_LEN ((ERROR_LOG_MAXIMUM_SIZE - \ FIELD_OFFSET(IO_ERROR_LOG_PACKET, DumpData)) \ /sizeof(WCHAR)) static char szErrMsg[MAX_ERRMSG_LEN] = {0}; TEnter(Func, ("(ErrorCode=%x,NTStatus=%x,Format=%s)", ErrorCode, NTStatus, pszFormat)); TAssert(gDriverObj != NULL); if (gDriverObj != NULL) { va_list arglist; NTSTATUS status; size_t iLen = 0; ULONG_PTR iTotalLen; PIO_ERROR_LOG_PACKET ErrEntry; va_start(arglist, pszFormat); status = RtlStringCchVPrintfA(szErrMsg, ARRAYSIZE(szErrMsg), pszFormat, arglist); va_end(arglist); if (NT_SUCCESS(status)) { status = RtlStringCchLengthA(szErrMsg, ARRAYSIZE(szErrMsg), &iLen); } if (NT_SUCCESS(status)) { iTotalLen = FIELD_OFFSET(IO_ERROR_LOG_PACKET, DumpData) + (iLen + 1)*sizeof(WCHAR); iTotalLen = max(iTotalLen, sizeof(IO_ERROR_LOG_PACKET)); ErrEntry = IoAllocateErrorLogEntry(gDriverObj, (UCHAR)iTotalLen); if (ErrEntry) { ErrEntry->NumberOfStrings = 1; ErrEntry->ErrorCode = ErrorCode; ErrEntry->StringOffset = FIELD_OFFSET(IO_ERROR_LOG_PACKET, DumpData); mbstowcs((WCHAR *)ErrEntry->DumpData, szErrMsg, iLen); ErrEntry->FinalStatus = NTStatus; IoWriteErrorLogEntry(ErrEntry); } else { TWarn(("Failed to allocate error log entry (len=%d).", (int)iTotalLen)); } if (ErrorCode == ERRLOG_DEBUG_INFORMATION) { TInfo(("%s", szErrMsg)); } else if (ErrorCode == ERRLOG_DEBUG_WARNING) { TWarn(("%s", szErrMsg)); } else if (ErrorCode == ERRLOG_DEBUG_ERROR) { TErr(("%s", szErrMsg)); } } } TExit(Func, ("!")); return; } //LogDbgMsg
/*----------------------------------------------------------------------------*/ static void zrtp_log(uint8_t is_clean, const char *sender, uint32_t level, const char *format, va_list marker) { #if (defined(ZRTP_USE_STACK_MINIM) && (ZRTP_USE_STACK_MINIM == 1)) char *log_buffer = zrtp_sys_alloc(ZRTP_LOG_BUFFER_SIZE); #else char log_buffer[ZRTP_LOG_BUFFER_SIZE]; #endif char* sline = log_buffer; uint32_t offset = 0; int len = 0; if (!sline) { return; } if (!is_clean) { /* Print sender with left aligment */ uint32_t sender_len = strlen(sender); *sline++ = ' '; *sline++ = '['; if (sender_len <= ZRTP_LOG_SENDER_MAX_LEN) { while (sender_len < ZRTP_LOG_SENDER_MAX_LEN) { *sline++ = ' ', ++sender_len; } while (*sender) { *sline++ = *sender++; } } else { int i = 0; for (i=0; i<ZRTP_LOG_SENDER_MAX_LEN; ++i) { *sline++ = *sender++; } } *sline++ = ']'; *sline++ = ':'; offset += 3 + ZRTP_LOG_SENDER_MAX_LEN; *sline++ = ' '; offset += 1; } /* Print Message itself */ #if (ZRTP_PLATFORM == ZP_WIN32) || (ZRTP_PLATFORM == ZP_WIN64) || (ZRTP_PLATFORM == ZP_WINCE) # if (_MSC_VER >= 1400) && (ZRTP_PLATFORM != ZP_WINCE) len = _vsnprintf_s(sline, ZRTP_LOG_BUFFER_SIZE-offset-1, ZRTP_LOG_BUFFER_SIZE-offset-1, format, marker); # else len = _vsnprintf(sline, ZRTP_LOG_BUFFER_SIZE-offset, format, marker); # endif #elif (ZRTP_PLATFORM == ZP_WIN32_KERNEL) RtlStringCchVPrintfA(sline, ZRTP_LOG_BUFFER_SIZE-offset, format, marker); #elif (ZRTP_PLATFORM == ZP_LINUX) || (ZRTP_PLATFORM == ZP_DARWIN) || (ZRTP_PLATFORM == ZP_BSD) || (ZRTP_PLATFORM == ZP_ANDROID) len = vsnprintf(sline, ZRTP_LOG_BUFFER_SIZE-offset, format, marker); #elif (ZRTP_PLATFORM == ZP_SYMBIAN) len = vsprintf(sline, format, marker); #endif if ((len > 0) && log_writer) { (*log_writer)(level, log_buffer, len+offset, offset); } #if (defined(ZRTP_USE_STACK_MINIM) && (ZRTP_USE_STACK_MINIM == 1)) zrtp_sys_free(log_buffer); #endif }