DebugPrint - 1.0.0.3

本文介绍了一种C++程序中实现调试打印信息的方法。通过定义DbgPrintA接口及宏控制,在Debug版本中启用调试信息输出,并在Release版本禁用以避免冗余输出。利用CDebug类实现了调试信息的具体解析与输出。

   很久没有写下什么东西了, 现在继续来写这个调试打印信息的函数吧!或许是自己一直使用C语言和汇编的缘故吧, 对于C++自己可是一直都未曾入门呢?

   这个打印的调试信息也是根据某同事的作品经过自己的修改而最终形成的。在实现打印调试功能时, 我先定义了一个打印调试信息的接口:
WINDBGAPI
DbgPrintA(
    __in const CHAR* szFormat,
    __in ...
    );
这个接口便是供程序调用的, 以便实时追踪程序当前的运行状况。

   然后定义了一个宏, 控制这个接口在Debug版本才起着打印调试信息的作用, 而在发布的Release版本则不需要打印那令人头疼的庞大调试信息。
#ifdef  _DEBUG
#define DbgPrint DbgPrintA
#else
#define DbgPrint (void)0
#endif 
/* _DEBUG */

接着我定义了一个类来具体实现打印信息的解析和具体的打印到具体指定的调试文件:
class CDebug {
    CRITICAL_SECTION m_cs;
public:
    CDebug(
        __in const CHAR* szFormat,
        __in va_list argList
        );
    virtual ~CDebug();
protected:
    BOOL Debug(
        __in const CHAR* szFormat,
        __in va_list argList
        );
    BOOL DebugInternal(
        __in const CHAR* szFormat,
        __in va_list argList,
        __in const int nLength
        );
    BOOL DebugPrint(
        __in const CHAR* szDebug
        );
};

   这样在DbgPrintA函数接口里面构造一个CDebug的对象, CDebug->Debug->DebugInternal->DebugPrint这样就最终打印出了具体的调试信息了。
   当然对于C++, 我只不过是一个初学者, 希望高手见到我的code不要见笑, 能够不吝赐教, 谢谢, 下面便是整个实现的源代码(这里为了节约空间, 去掉了程序的注释, 请谅解)!


/* source code ---- debug.h */

#ifndef __DEBUG_LOG_INFORMATION_H__
#define __DEBUG_LOG_INFORMATION_H__

#include <windows.h>
#include <stdio.h>
#pragma warning(disable:4353)

#if !defined(_WINDBG_LOG_)
#define WINDBGAPI extern void WINAPI
#else
#define WINDBGAPI
#endif 
/* _WINDBG_LOG_ */


 
#ifndef WINDBG_NO_DEBUG_INTERFACE

WINDBGAPI
DbgPrintA(
    __in const CHAR* szFormat,
    __in ...
    );
#ifdef  _DEBUG
#define DbgPrint DbgPrintA
#else
#define DbgPrint (void)0
#endif 
/* _DEBUG */

#endif  /* WINDBG_NO_DEBUG_INTERFACE */


class CDebug {
    CRITICAL_SECTION m_cs;
public:
    CDebug(
        __in const CHAR* szFormat,
        __in va_list argList
        );
    virtual ~CDebug();
protected:
    BOOL Debug(
        __in const CHAR* szFormat,
        __in va_list argList
        );
    BOOL DebugInternal(
        __in const CHAR* szFormat,
        __in va_list argList,
        __in const int nLength
        );
    BOOL DebugPrint(
        __in const CHAR* szDebug
        );
};

__inline
CDebug::CDebug(
    __in const CHAR* szFormat,
    __in va_list argList
    )
{
    InitializeCriticalSection(&m_cs);
    Debug(szFormat, argList);
}

__inline
CDebug::~CDebug()
{
    DeleteCriticalSection(&m_cs);
}

__inline
BOOL CDebug::Debug(
    __in const CHAR* szFormat,
    __in va_list argList
    )
{
    int  len  = 0;
    BOOL bRet = FALSE;

    if (NULL == szFormat || NULL == argList)
        return FALSE;

    EnterCriticalSection(&m_cs);
    len  = _vscprintf(szFormat, argList) + 1;
    bRet = DebugInternal(szFormat, argList, len);
    LeaveCriticalSection(&m_cs);
    return bRet;
}

__inline
BOOL CDebug::DebugInternal(
    __in const CHAR* szFormat,
    __in va_list argList,
    __in const int nLength
    )
{
    CHAR* pDebug = NULL;
    BOOL  bRet   = FALSE;
    int   size   = sizeof(CHAR) * nLength;

    if (NULL == szFormat || NULL == argList)
        return FALSE;

    pDebug = (CHAR*)HeapAlloc(GetProcessHeap(), 0, size);
    if (NULL == pDebug) return FALSE;
    memset(pDebug, 0, size);
    _vsnprintf_s(pDebug, nLength, nLength, szFormat, argList);
    bRet = DebugPrint(pDebug);
 
    if (NULL != pDebug) {
        HeapFree(GetProcessHeap(), 0, pDebug);
        pDebug = NULL;
    }
    return bRet;
}

__inline
BOOL CDebug::DebugPrint(
    __in const CHAR* szDebug
    )
{
    FILE* pFile = NULL;
    SYSTEMTIME st;

    if (NULL == szDebug) return FALSE;
    fopen_s(&pFile, "debug.log", "a+");
    if (NULL == pFile) return FALSE;
    GetLocalTime(&st);

    fprintf_s(pFile, "[%.2d:%.2d:%.2d:%.3d] %s/n",
        st.wHour, st.wMinute, st.wSecond,
        st.wMilliseconds, szDebug);
    if (NULL != pFile) {
        fclose(pFile);
        pFile = NULL;
    }
    return TRUE;
}


__inline
WINDBGAPI DbgPrintA(
    __in const CHAR* szFormat,
    __in ...
    )
{
    va_list argList = NULL;

    va_start(argList, szFormat);
    CDebug dbg(szFormat, argList);
    va_end(argList);
}


#endif 
/* __DEBUG_LOG_INFORMATION_H__ */

有关调用实时(JIT)调试而不是此对话框的详细信息, 请参见此消息的结尾。 ************** 异常文本 ************** System.ComponentModel.Win32Exception (0x80004005): RPC 服务器不可用。 在 System.Drawing.Printing.PrinterSettings.get_InstalledPrinters() 在 JJMes.PrintHelper`1.GetPrinter(ComboBox& cmbPrint) 位置 F:\vspro\jjmes\CofcoMes\PrintHelper.cs:行号 95 在 JJMes.Warehouse.WarehouselocationFrm.WarehouselocationFrm_Load(Object sender, EventArgs e) 位置 F:\vspro\jjmes\CofcoMes\A01warehouse\WarehouselocationFrm.cs:行号 28 在 System.Windows.Forms.Form.OnLoad(EventArgs e) 在 DevComponents.DotNetBar.Office2007RibbonForm.OnLoad(EventArgs e) 在 System.Windows.Forms.Form.OnCreateControl() 在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) 在 System.Windows.Forms.Control.CreateControl() 在 System.Windows.Forms.Control.WmShowWindow(Message& m) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ScrollableControl.WndProc(Message& m) 在 System.Windows.Forms.Form.WmShowWindow(Message& m) 在 System.Windows.Forms.Form.WndProc(Message& m) 在 DevComponents.DotNetBar.Office2007RibbonForm.WndProc(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) ************** 已加载的程序集 ************** mscorlib 程序集版本:4.0.0.0 Win32 版本:4.7.4135.0 built by: NET472REL1LAST_B 基本代码:file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll ---------------------------------------- JJMes 程序集版本:1.0.0.0 Win32 版本:1.0.0.0 基本代码:file:///C:/Users/Administrator/Desktop/Debug/JJMes.exe ---------------------------------------- System.Windows.Forms 程序集版本:4.0.0.0 Win32 版本:4.7.4057.0 built by: NET472REL1LAST_B 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll ---------------------------------------- System 程序集版本:4.0.0.0 Win32 版本:4.7.4135.0 built by: NET472REL1LAST_B 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll ---------------------------------------- System.Drawing 程序集版本:4.0.0.0 Win32 版本:4.7.3062.0 built by: NET472REL1 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll ---------------------------------------- DevComponents.DotNetBar2 程序集版本:7.7.0.3 Win32 版本:7.7.0.3 基本代码:file:///C:/Users/Administrator/Desktop/Debug/DevComponents.DotNetBar2.DLL ---------------------------------------- System.Configuration 程序集版本:4.0.0.0 Win32 版本:4.7.3630.0 built by: NET472REL1LAST_B 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll ---------------------------------------- System.Core 程序集版本:4.0.0.0 Win32 版本:4.7.4130.0 built by: NET472REL1LAST_B 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll ---------------------------------------- System.Xml 程序集版本:4.0.0.0 Win32 版本:4.7.3062.0 built by: NET472REL1 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll ---------------------------------------- Accessibility 程序集版本:4.0.0.0 Win32 版本:4.7.3062.0 built by: NET472REL1 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll ---------------------------------------- System.Windows.Forms.resources 程序集版本:4.0.0.0 Win32 版本:4.6.1586.0 built by: NETFXREL2 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.Windows.Forms.resources.dll ---------------------------------------- Newtonsoft.Json 程序集版本:13.0.1.0 Win32 版本:13.0.1.25924 基本代码:file:///C:/Users/Administrator/Desktop/Debug/Newtonsoft.Json.DLL ---------------------------------------- System.Data 程序集版本:4.0.0.0 Win32 版本:4.7.4081.0 built by: NET472REL1LAST_B 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll ---------------------------------------- System.Runtime.Serialization 程序集版本:4.0.0.0 Win32 版本:4.7.4092.0 built by: NET472REL1LAST_B 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll ---------------------------------------- System.Numerics 程序集版本:4.0.0.0 Win32 版本:4.7.3062.0 built by: NET472REL1 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Numerics/v4.0_4.0.0.0__b77a5c561934e089/System.Numerics.dll ---------------------------------------- mscorlib.resources 程序集版本:4.0.0.0 Win32 版本:4.6.1586.0 built by: NETFXREL2 基本代码:file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/mscorlib.resources.dll ---------------------------------------- ************** JIT 调试 ************** 要启用实时(JIT)调试, 该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置 jitDebugging 值。 编译应用程序时还必须启用 调试。 例如: <configuration> <system.windows.forms jitDebugging="true" /> </configuration> 启用 JIT 调试后,任何未经处理的异常 都将被发送到在此计算机上注册的 JIT 调试器, 而不是由此对话框处理。
最新发布
11-15
Global: debug: false use_gpu: true epoch_num: &epoch_num 50 log_smooth_window: 20 print_batch_step: 20 save_model_dir: ./output/ch_PP-OCRv4 save_epoch_step: 20 eval_batch_step: - 0 - 500 cal_metric_during_train: false checkpoints: null pretrained_model: ./ch_PP-OCRv4_det_train/best_accuracy.pdparams save_inference_dir: null use_visualdl: false infer_img: doc/imgs_en/img_10.jpg save_res_path: ./checkpoints/det_db/predicts_db.txt d2s_train_image_shape: [3, 640, 640] distributed: true Architecture: model_type: det algorithm: DB Transform: null Backbone: name: PPLCNetV3 scale: 0.75 pretrained: false det: true Neck: name: RSEFPN out_channels: 96 shortcut: true Head: name: DBHead k: 50 Loss: name: DBLoss weight: 1.0 key: maps balance_loss: true main_loss_type: DiceLoss alpha: 5 beta: 10 ohem_ratio: 3 Optimizer: name: Adam beta1: 0.9 beta2: 0.999 lr: name: Cosine learning_rate: 0.001 warmup_epoch: 2 regularizer: name: L2 factor: 5.0e-05 PostProcess: name: DBPostProcess thresh: 0.3 box_thresh: 0.6 max_candidates: 1000 unclip_ratio: 1.5 Metric: name: DetMetric base_metric_name: DetMetric main_indicator: hmean key: Student Train: dataset: name: SimpleDataSet data_dir: ./train_data/det/train/ label_file_list: - ./train_data/det/train.txt ratio_list: [1.0] transforms: - DecodeImage: img_mode: BGR channel_first: false # - DetResize: # image_shape: [640, 640] # 统一输出尺寸,后添加 - DetLabelEncode: null - IaaAugment: augmenter_args: - type: Fliplr args: p: 0.5 - type: Affine args: rotate: - -10 - 10 - type: Resize args: size: - 0.5 - 3 - EastRandomCropData: size: - 640 - 640 max_tries: 50 keep_ratio: true - MakeBorderMap: shrink_ratio: 0.4 thresh_min: 0.3 thresh_max: 0.7 total_epoch: *epoch_num - MakeShrinkMap: shrink_ratio: 0.4 min_text_size: 8 total_epoch: *epoch_num - NormalizeImage: scale: 1./255. mean: - 0.485 - 0.456 - 0.406 std: - 0.229 - 0.224 - 0.225 order: hwc - ToCHWImage: null - KeepKeys: keep_keys: - image - threshold_map - threshold_mask - shrink_map - shrink_mask loader: shuffle: true drop_last: false batch_size_per_card: 8 num_workers: 4 Eval: dataset: name: SimpleDataSet data_dir: ./train_data/det/val/ label_file_list: - ./train_data/det/val.txt transforms: - DecodeImage: img_mode: BGR channel_first: false - DetLabelEncode: null - DetResizeForTest: limit_side_len: 960 limit_type: max - NormalizeImage: scale: 1./255. mean: - 0.485 - 0.456 - 0.406 std: - 0.229 - 0.224 - 0.225 order: hwc - ToCHWImage: null - KeepKeys: keep_keys: - image - shape - polys - ignore_tags/ - Resize: size: [640, 640] # 额外加 # cal_metric_during_train: true loader: shuffle: false drop_last: false batch_size_per_card: 4 num_workers: 2 profiler_options: null这是配置文件
07-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值