C/C++ Pantheios诊断日志API库

来源:互联网 发布:淘宝买电动车怎么样 编辑:程序博客网 时间:2024/05/16 23:24
        Pantheios是一个开源的C/C++诊断日志API库,提供一个100%类型安全,效率,通用性和可扩展性的最佳组合。
Pantheios网址:http://pantheios.sourceforge.net
STLSoft网址:http://stlsoft.org/          
1.下载pantheios、STLSoft
2.解压STLSoft到目录,把此目录加入到环境变量中,用如下命令:
C:\>SET STLSOFT=E:\项目\公共组件\stlsoft-1.9.111
3.进入D:\Program Files\Microsoft Visual Studio 9.0\VC\bin目录,运行vcvars32.bat,为nmake设置环境变量(若已设置,则忽略此步);
4.用CMD命令进入E:\项目\公共组件\pantheios-1.0.1-beta213\build\vc9目录,键入nmake /f makefile,然后等待编译;
5.编译完成后,LIB文件夹会生成许多lib文件。

下面测试:
1.打开VC2008,新建一个Win32控制台应用程序,名称TestLog,其余默认设置;
2.菜单→"项目"→"属性"→"常规"→"字符集",设置"使用多字节字符集";
3.菜单→"项目"→"属性"→"C/C++"→"常规"→"附加包含目录",加入"...\pantheios-1.0.1-beta213\include"目录和"...\stlsoft-1.9.111\include"目录,"链接器"→"常规"→"附加库目录",加入"...\pantheios-1.0.1-beta213\lib";
4.加入以下代码:
01
02
03
04
05
06
07
08
09
10
11
12
13#include "stdafx.h" 
#include <pantheios/pantheios.hpp> 
#include <pantheios/implicit_link/core.h> 
#include <pantheios/implicit_link/fe.simple.h> 
#include <pantheios/implicit_link/be.WindowsConsole.h> 
 
extern "C" const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "TestLog"
 
int _tmain(int argc, _TCHAR* argv[]) 

    pantheios::log_DEBUG("这是用pantheios显示出来的日志信息"); 
    return 0

5.按Ctrl+F5运行,结果如下图所示:

6.若是要改变颜色,改变显示内容,则改变代码如下所示:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "stdafx.h" 
#include <pantheios/pantheios.hpp> 
#include <pantheios/backends/bec.WindowsConsole.h> 
#include <pantheios/implicit_link/core.h> 
#include <pantheios/implicit_link/fe.simple.h> 
#include <pantheios/implicit_link/be.WindowsConsole.WithCallback.h> 
 
extern "C" const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "TestLog"
 
PANTHEIOS_CALL(void) pantheios_be_WindowsConsole_getAppInit(int  /* backEndId */,  
    pan_be_WindowsConsole_init_t* init) /* throw() */ 

    init->flags |=  PANTHEIOS_BE_INIT_F_NO_PROCESS_ID; 
    init->flags |=  PANTHEIOS_BE_INIT_F_NO_THREAD_ID; 
    init->flags |=  PANTHEIOS_BE_INIT_F_HIGH_RESOLUTION; 
 
    init->colours[pantheios::debug] = FOREGROUND_BLUE | FOREGROUND_INTENSITY;   

 
int _tmain(int argc, _TCHAR* argv[]) 

    pantheios::log_DEBUG("这是用pantheios显示出来的日志信息"); 
    return 0

7.结果如下图所示:

8.若是想保存到文件的话,改动如下代码:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19#include "stdafx.h" 
#include <pantheios/pantheios.hpp> 
#include <pantheios/backends/bec.file.h>  
#include <pantheios/implicit_link/core.h> 
#include <pantheios/implicit_link/fe.simple.h> 
#include <pantheios/implicit_link/be.file.h> 
 
extern "C" const char PANTHEIOS_FE_PROCESS_IDENTITY[] = "TestLog"
 
int _tmain(int argc, _TCHAR* argv[]) 

    pantheios::log_DEBUG("这是用pantheios显示出来的日志信息"); 
    pantheios_be_file_setFilePath("single.log",  
        PANTHEIOS_BE_FILE_F_TRUNCATE,  
        PANTHEIOS_BE_FILE_F_TRUNCATE,  
        PANTHEIOS_BEID_ALL); 
    pantheios_be_file_setFilePath(NULL, PANTHEIOS_BEID_ALL); 
    return 0

9.运行后,可在工程下发现有"single.log"文件,打开如下图所示:


扩展阅读:
1.Using Callback Back-ends with the Pantheios Logging API Library  http://www.codeproject.com/KB/cpp/callback_backends.aspx
2.Adding Logging to C Programs with the Pantheios C API  http://www.codeproject.com/KB/cpp/pantheios_C.aspx
3.An Introduction to Pantheios Back-ends, Part 1: The Back-end API  http://www.codeproject.com/KB/trace/PantheiosBackendIntro.aspx
4.pantheios  http://pantheios.sourceforge.net/tutorials.html