📄 vm_debug_win32.c
字号:
/*
//
// INTEL CORPORATION PROPRIETARY INFORMATION
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Intel Corporation and may not be copied
// or disclosed except in accordance with the terms of that agreement.
// Copyright(c) 2003-2007 Intel Corporation. All Rights Reserved.
//
*/
#include <stdio.h>
#include <stdarg.h>
#include "vm_file.h"
#include "vm_debug.h"
#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE)
# include <tchar.h>
# if defined(_WIN32_WCE)
# include <Kfuncs.h>
#endif
#elif defined LINUX32
# include <time.h>
# include <sys/types.h>
# include <unistd.h>
# include <syslog.h>
#endif
#pragma warning(disable: 4100)
//#define VM_DEBUG (VM_DEBUG_LOG_ALL|VM_DEBUG_SHOW_FILELINE|VM_DEBUG_SHOW_THIS|VM_DEBUG_SHOW_FUNCTION|VM_DEBUG_FILE)
#ifdef VM_DEBUG
#define VM_ONE_FILE_OPEN
#define VM_DEBUG_LINE 1024
static vm_debug_level vm_DebugLevel = VM_DEBUG & VM_DEBUG_ALL;
static vm_debug_output vm_DebugOutput = VM_DEBUG &~ VM_DEBUG_ALL;
static vm_char vm_LogFileName[_MAX_LEN] = VM_STRING("\\vm_debug_log.txt");
static FILE *vm_LogFile = NULL;
//static vm_mutex mDebugMutex;
//static Ipp32s mDebugMutexIsValid = 1;
vm_debug_level vm_debug_setlevel(vm_debug_level new_level) {
vm_debug_level old_level = vm_DebugLevel;
vm_DebugLevel = new_level;
return old_level;
}
vm_debug_output vm_debug_setoutput(vm_debug_output new_output) {
vm_debug_output old_output = vm_DebugOutput;
vm_DebugOutput = new_output;
return old_output;
}
void vm_debug_setfile(vm_char *file, Ipp32s truncate)
{
if (!file)
{
file = VM_STRING("");
}
if (!vm_string_strcmp(vm_LogFileName, file))
{
return;
}
if (vm_LogFile)
{
fclose(vm_LogFile);
vm_LogFile = NULL;
}
vm_string_strcpy(vm_LogFileName, file);
if (*vm_LogFileName)
{
vm_LogFile = _wfopen(vm_LogFileName, (truncate) ? VM_STRING("w") : VM_STRING("a"));
}
}
void vm_debug_message(const vm_char *format,...)
{
vm_char line[VM_DEBUG_LINE];
va_list args;
va_start(args, format);
vm_string_vsprintf(line, format, args);
va_end(args);
#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE)
MessageBox(GetActiveWindow(), line, VM_STRING("MESSAGE"), MB_OK);
#elif defined(LINUX32)
fprintf(stderr, line);
#endif
}
void vm_debug_trace_ex(Ipp32s level,
const void *ptr_this,
const vm_char *func_name,
const vm_char *file_name,
Ipp32s num_line,
const vm_char *format,
...)
{
va_list args;
vm_char tmp[VM_DEBUG_LINE] = {0,}, *p;
if (level && !(level & vm_DebugLevel)) {
return;
}
//if (vm_string_strstr(file_name, "mpeg2_mux") && level == VM_DEBUG_PROGRESS) return;
/* print filename and line */
if (vm_DebugLevel & VM_DEBUG_SHOW_FILELINE) {
int showMask;
if (vm_DebugLevel &~ VM_DEBUG_SHOW_DIRECTORY) {
p = vm_string_strrchr(file_name, '\\');
if (p != NULL) file_name = p + 1;
p = vm_string_strrchr(file_name, '/');
if (p != NULL) file_name = p + 1;
}
vm_string_sprintf(tmp, VM_STRING("%s(%i), "), file_name, num_line);
showMask = vm_DebugLevel & VM_DEBUG_SHOW_ALL;
if (!(showMask &~ (VM_DEBUG_SHOW_FILELINE | VM_DEBUG_SHOW_FUNCTION))) {
// in case of fileline&function only expand to 35 symbols
p = tmp + vm_string_strlen(tmp);
while (p < tmp + 32) *p++ = ' ';
*p = 0;
}
}
/* print time and PID */
#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE)
if (vm_DebugLevel & VM_DEBUG_SHOW_TIME) {
SYSTEMTIME SystemTime;
GetLocalTime(&SystemTime);
vm_string_sprintf(tmp + vm_string_strlen(tmp),
VM_STRING("%u/%u/%u %u:%u:%u:%u, "),
SystemTime.wDay,
SystemTime.wMonth,
SystemTime.wYear,
SystemTime.wHour,
SystemTime.wMinute,
SystemTime.wSecond,
SystemTime.wMilliseconds);
}
if (vm_DebugLevel & VM_DEBUG_SHOW_PID) {
vm_string_sprintf(tmp + vm_string_strlen(tmp), VM_STRING("PID %i, "), GetCurrentProcessId());
}
#elif defined(LINUX32)
if (vm_DebugLevel & VM_DEBUG_SHOW_TIME) {
time_t timep;
struct tm *ptm;
time(&timep);
ptm = localtime((const time_t*) &timep);
vm_string_sprintf(tmp + vm_string_strlen(tmp),
VM_STRING("%s%u/%u/%u %u:%u:%u, "),
ptm->tm_mday, 1+(ptm->tm_mon), 1900+(ptm->tm_year),
ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
}
if (vm_DebugLevel & VM_DEBUG_SHOW_PID) {
vm_string_sprintf(tmp + vm_string_strlen(tmp), VM_STRING("%sPID %i, "), getpid());
}
#endif
/* print 'this' pointer if needed */
if ((vm_DebugLevel & VM_DEBUG_SHOW_THIS) && ptr_this) {
vm_string_sprintf(tmp + vm_string_strlen(tmp), VM_STRING("%08x, "), ptr_this);
}
/* print func_name if needed */
if (func_name && (vm_DebugLevel & VM_DEBUG_SHOW_FUNCTION)) {
p = vm_string_strstr(func_name, VM_STRING("::"));
if (p) p += 2; else p = func_name;
vm_string_sprintf(tmp + vm_string_strlen(tmp), VM_STRING("%s(): "), p);
}
if (vm_DebugLevel & VM_DEBUG_SHOW_LEVEL) {
vm_string_sprintf(tmp + vm_string_strlen(tmp), VM_STRING("Level%d, "), level);
}
va_start(args, format);
vm_string_vsprintf(tmp + vm_string_strlen(tmp), format, args);
va_end(args);
// remove trailing \n
p = tmp + vm_string_strlen(tmp) - 1;
if (*p == 0xD || *p == 0xA) *p-- = 0;
if (*p == 0xD || *p == 0xA) *p-- = 0;
vm_string_strcat(tmp, VM_STRING("\n"));
if (vm_DebugOutput & VM_DEBUG_FILE) {
FILE *pFile = NULL;
/*
if (mDebugMutexIsValid) {
vm_mutex_set_invalid(&mDebugMutex);
res = vm_mutex_init(&mDebugMutex);
if (res != VM_OK) {
vm_debug_trace(VM_DEBUG_ALL, VM_STRING("Failed to init debug mutex !!!"));
return;
}
mDebugMutexIsValid = 0;
}
res = vm_mutex_lock(&mDebugMutex);
if (res != VM_OK) {
vm_debug_trace(VM_DEBUG_ALL, VM_STRING("Failed to lock debug mutex !!!"));
return;
}
*/
if (!vm_LogFile) {
pFile = _wfopen(vm_LogFileName, VM_STRING("w"));
vm_LogFile = pFile;
} else {
#ifdef VM_ONE_FILE_OPEN
pFile = vm_LogFile;
#else
pFile = fopen(vm_LogFileName, VM_STRING("a"));
#endif
}
if (pFile) {
#if defined(UNICODE) || defined(_UNICODE)
fprintf(pFile, "%S", tmp);
#else
fprintf(pFile, "%s", tmp);
#endif
#ifdef VM_ONE_FILE_OPEN
fflush(pFile);
#else
fclose(pFile);
#endif
}
/*
res = vm_mutex_unlock(&mDebugMutex);
if (res != VM_OK) {
vm_debug_trace(VM_DEBUG_ALL, VM_STRING("Failed to unlock debug mutex !!!"));
return;
}
*/
}
if (vm_DebugOutput & VM_DEBUG_CONSOLE) {
vm_string_printf(tmp);
}
if (vm_DebugOutput == VM_DEBUG_AUTO) {
#if defined(_WIN32) || defined(_WIN64) || defined(_WIN32_WCE)
OutputDebugString(tmp);
#elif defined(LINUX32)
syslog("%s", tmp);
#endif
}
}
#else /* !VM_DEBUG */
vm_debug_level vm_debug_setlevel(vm_debug_level level)
{
return level;
}
vm_debug_output vm_debug_setoutput(vm_debug_output output)
{
return output;
}
void vm_debug_setfile(vm_char *file, Ipp32s truncate) {}
void vm_debug_message(const vm_char *format, ...) {}
void vm_debug_trace_ex(Ipp32s level, const vm_char *func_name, const vm_char *file_name,
Ipp32s num_line, const vm_char *format, ...){}
#endif /* VM_DEBUG */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -