zoukankan      html  css  js  c++  java
  • C++第三方日志库Pantheios

    C++第三方日志库Pantheios

    Kagula

    2012-1-11

    简介

        在项目实践中发现一个好的日志记录非常重要,你需要借助Logging才能跟踪软件中的错误。所以这里研究下第三方C++库Pantheios的使用。

    Pantheios的架构分为前端和后端,具体概念介绍参考资料[3],这里只给出实际如何使用的源码示例。

    我这里使用的环境:

    [1]Windows XP SP3

    [2]Visual Studio2008 + SP1

    [3]pantheios-1.0.1-beta213

    [4] stlsoft-1.9.111

    [5]pantheios-libselw-1.9.6.52

    正文

    配置Pantheios使用环境

    第一步:从参考资料[1]下载pantheios-1.0.1-beta213.zip压缩包,并解压。

    为pantheios配置系统环境变量,例如

    PANTHEIOS_ROOT=E:SDKpantheios-1.0.1-beta213

    其中“E:SDKpantheios-1.0.1-beta213”是解压后的位置

    第二步:从参考资料[2]下载stlsoft-1.9.111-hdrs.zip压缩包,并解压。

    为stlsoft配置系统环境变量,例如

    STLSOFT=E:SDKstlsoft-1.9.111

    里面只有些头文件,但是我们只需要头文件就足够了。

    第三步:启动Visual Studio 2008 命令行,它的位置在[开始]->[ Microsoft Visual Studio 2008]->[ Visual Studio Tools]->[Visual Studio 2008 Command Prompt]

        在VS2008命令行中转到“E:SDKpantheios-1.0.1-beta213uildvc9”路径,输入命令“NMAKE   BUILD”开始建立Pantheios库和样例,等待编译、链接完成。

    第四步:在VS2008里配置头文件路径和库文件路径

    我这里分别是“E:SDKpantheios-1.0.1-beta213include”

    “E:SDKstlsoft-1.9.111include”

     “E:SDKpantheios-1.0.1-beta213lib”

    Pantheios调用示例

            项目运行时,Explicitly方式,需要输入依赖库名称,这时你需要“Pantheios Library Selector Tool”在参考资料[4]中下载。参考资料[5]把链接库列表从应用程序中复制出来,张贴到你的项目依赖项(库)中,实际使用碰到了很多问题(例如复制到剪贴板按钮失灵,依赖库找不到、依赖库冲突)所以本文不采用这种方式

    这里由简入繁,举了三个实例来说明Pantheios的使用,它们均是在VS2008的 Win32 Console Application向导中建立和测试通过的。

    第一个例子,输出日志到文件中

    只有一个源文件源码如下,编译顺利的话可以直接运行:

    [cpp] view plain copy
     
    1. // testFile.cpp : Defines the entry point for the console application.  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5.   
    6. //指定链接库.begin  
    7. #include <pantheios/implicit_link/core.h>  
    8. #include <pantheios/implicit_link/fe.simple.h>  
    9. #include <pantheios/implicit_link/be.file.h>  
    10. //指定链接库.end  
    11.   
    12. #define PANTHEIOS_NO_INCLUDE_OS_AND_3PTYLIB_STRING_ACCESS // Faster compilation  
    13.   
    14. /* Pantheios Header Files */  
    15. #include <pantheios/pantheios.hpp>            // Pantheios C++ main header  
    16. #include <pantheios/backends/bec.file.h>      // be.file header  
    17.   
    18. /* Standard C/C++ Header Files */  
    19. #include <exception>                          // for std::exception  
    20. #include <new>                                // for std::bad_alloc  
    21. #include <string>                             // for std::string  
    22. #include <stdlib.h>                           // for exit codes  
    23.   
    24. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("testFile");  
    25. #define PSTR(x)         PANTHEIOS_LITERAL_STRING(x)  
    26.   
    27. int _tmain(int argc, _TCHAR* argv[])  
    28. {  
    29.     try  
    30.     {  
    31.         //设置文件名  
    32.         pantheios_be_file_setFilePath(PSTR("kagula-%D-%T.log"),   
    33.             PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_ALL);  
    34.   
    35.         //打印到文件  
    36.         pantheios::log_NOTICE(PSTR("stmt 1"));  
    37.         pantheios::log_NOTICE(PSTR("stmt 2"));  
    38.         pantheios::log_NOTICE(PSTR("stmt 3"));  
    39.         pantheios::log_DEBUG(PSTR("exiting main()"));  
    40.   
    41.         return EXIT_SUCCESS;  
    42.     }  
    43.     catch(std::bad_alloc&)  
    44.     {  
    45.         pantheios::log_ALERT(PSTR("out of memory"));  
    46.     }  
    47.     catch(std::exception& x)  
    48.     {  
    49.         pantheios::log_CRITICAL(PSTR("Exception: "), x);  
    50.     }  
    51.     catch(...)  
    52.     {  
    53.         //如果只是打印一条字符串形式的信息,建议用下面这种形式打印日志!  
    54.         pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));  
    55.     }  
    56.       
    57.     return EXIT_FAILURE;  
    58. }  


    第二个例子,输出日志到Windows控制台

    有三个源文件组成,stdafx.h文件插入了“MY_PROCESS_ID”宏,定义当前进程名称。

    implicit_link.cpp文件,用来指定链接库和日志输出等级,第三个文件T2.cpp是示例代码,列举了不同水平的信息输出,和如何格式化输出信息。

    stdafx.h源码

    [cpp] view plain copy
     
    1. // stdafx.h : include file for standard system include files,  
    2. // or project specific include files that are used frequently, but  
    3. // are changed infrequently  
    4. //  
    5.   
    6. #pragma once  
    7.   
    8. #include "targetver.h"  
    9.   
    10. #include <stdio.h>  
    11. #include <tchar.h>  
    12.   
    13.   
    14.   
    15. // TODO: reference additional headers your program requires here  
    16. #define MY_PROGRAM_ID "MyProgramID"  

    implicit_link.cpp源码

    [cpp] view plain copy
     
    1. #include "stdafx.h"  
    2.   
    3. #include <pantheios/implicit_link/core.h>  
    4.   
    5. //如果定义了USER_SPECIFIED_LEVEL宏定义,则使用用户自定义的输出等级  
    6. //如果不定义“USER_SPECIFIED_LEVEL”宏,表示所有等级的信息都输出  
    7. #define USER_SPECIFIED_LEVEL  
    8.   
    9. #ifndef USER_SPECIFIED_LEVEL  
    10.   #include <pantheios/implicit_link/fe.simple.h>  
    11. #endif  
    12.   
    13. //一旦加入下面这条include语句,程序的log输出就会转到Windows控制台  
    14. //在本例中,这条语句是必须的。  
    15. #include <pantheios/implicit_link/be.WindowsConsole.h>  
    16.   
    17. #ifdef USER_SPECIFIED_LEVEL  
    18. PANTHEIOS_CALL(int) pantheios_fe_init(void*   reserved,void**  ptoken)  
    19. {  
    20.     *ptoken = NULL;  
    21.     return 0;  
    22. }  
    23.   
    24. PANTHEIOS_CALL(void) pantheios_fe_uninit(void* token)  
    25. {}  
    26.   
    27. PANTHEIOS_CALL(PAN_CHAR_T const*)  pantheios_fe_getProcessIdentity  (void *  token)   
    28. {  
    29.     return PANTHEIOS_LITERAL_STRING(MY_PROGRAM_ID);  
    30. }  
    31.   
    32. PANTHEIOS_CALL(int) pantheios_fe_isSeverityLogged(void* token  
    33.                                                   , int   severity  
    34.                                                   , int   backEndId)  
    35. {  
    36.     //数值越小说明要输出的信息越重要,常用的越先级大小如下  
    37.     //SEV_CRITICAL=2 < SEV_ERROR=3 < SEV_WARNING=4 < SEV_INFORMATIONAL=6  
    38.     if(severity <= pantheios::SEV_INFORMATIONAL)  
    39.         return 1;//允许输出  
    40.     return 0;   
    41. }  
    42.   
    43. #endif  


    T2.cpp源码

    [cpp] view plain copy
     
    1. // T2.cpp : Defines the entry point for the console application.  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5.   
    6. #include <pantheios/pantheios.hpp>            // Pantheios C++ main header  
    7. #include <pantheios/inserters/processid.hpp>  // for pantheios::processId  
    8. #include <pantheios/inserters/threadid.hpp>   // for pantheios::threadId  
    9. #include <pantheios/inserters/integer.hpp>    //for pantheios::integer  
    10. #include <pantheios/inserters/real.hpp>       //for pantheios::real  
    11. #include <pantheios/inserters/blob.hpp>       // for pantheios::blob  
    12. #include <pantheios/inserters/hex_ptr.hpp>    // for pantheios::hex_ptr  
    13.   
    14. #include <pantheios/backend.h>  
    15. #include <pantheios/backends/bec.WindowsConsole.h>  
    16.   
    17. #define PSTR(x)         PANTHEIOS_LITERAL_STRING(x)  
    18.   
    19. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING(MY_PROGRAM_ID);  
    20.   
    21. int _tmain(int argc, _TCHAR* argv[])  
    22. {  
    23.     //输出当前进程ID  
    24.     pantheios::log_NOTICE(PSTR("process id: ["), pantheios::processId, PSTR("]"));  
    25.   
    26.     //输出当前线程ID  
    27.     pantheios::log_NOTICE(PSTR("thread id: ["), pantheios::threadId, PSTR("]"));  
    28.   
    29.     //演示:不同等级输出  
    30.     //典型的输出格式为“MY_PROGRAM_ID.线程ID 时分秒毫秒 等级 日志信息”  
    31.     //建议把MY_PROGRAM_ID替换为你的进程ID  
    32.     pantheios::log_DEBUG(PSTR("debug"));  
    33.     pantheios::log_INFORMATIONAL(PSTR("informational"));  
    34.     pantheios::log_NOTICE(PSTR("notice"));  
    35.     pantheios::log_WARNING(PSTR("warning"));  
    36.     pantheios::log_ERROR(PSTR("error"));  
    37.     pantheios::log_CRITICAL(PSTR("critical"));  
    38.     pantheios::log_ALERT(PSTR("alert"));  
    39.     pantheios::log_EMERGENCY(PSTR("emergency"));  
    40.   
    41.     //演示int、float格式化输出  
    42.     int   i=123;  
    43.     float f=4.567f;   
    44.   
    45.     pantheios::log_INFORMATIONAL("[[ i=", pantheios::integer(i),   
    46.         " f=",pantheios::real(f),"]]");  
    47.   
    48.     pantheios::pantheios_logprintf(pantheios::SEV_INFORMATIONAL,PSTR("int=%d, float=%.2g"),i,f);  
    49.   
    50.   
    51.     //演示二进制格式化输出  
    52.     pantheios::uint8_t  bytes[20];  
    53.     for(size_t i = 0; i < STLSOFT_NUM_ELEMENTS(bytes); ++i)  
    54.     {  
    55.       bytes[i] = static_cast<pantheios::uint8_t>(i);  
    56.     }  
    57.       
    58.     //默认打印格式如右"bytes: [03020100070605040b0a09080f0e0d0c13121110]"  
    59.     pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes)), PSTR("]"));  
    60.   
    61.     //四字节为一组当中以空格分隔,打印格式如右 "bytes: [03020100 07060504 0b0a0908 0f0e0d0c 13121110]"  
    62.     pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes), 4, PSTR(" ")), PSTR("]"));  
    63.   
    64.     // 每字节以空格分隔,四个为一行,每行以换行符结尾和两个空格结尾  
    65.     // 打印格式如下  
    66.     // "bytes: [00 01 02 03  
    67.     //   04 05 06 07  
    68.     //   08 09 0a 0b  
    69.     //   0c 0d 0e 0f  
    70.     //   10 11 12 13]"  
    71.     pantheios::log_NOTICE(PSTR("bytes: ["), pantheios::blob(bytes, sizeof(bytes), 1, PSTR(" "), 4, PSTR("   ")), PSTR("]"));  
    72.   
    73.   
    74.     //演示打印十六进制指针  
    75.     //打印格式如右"pv: [0x0012fed0]"  
    76.     pantheios::log_NOTICE(PSTR("pv: ["), pantheios::hex_ptr(&i), PSTR("]"));  
    77.   
    78.     return EXIT_SUCCESS;  
    79. }  


    第三个例子,输出日志到多种流中

    主要有两个源文件组成

     implicit_link.cpp

    [cpp] view plain copy
     
    1. #include "stdafx.h"  
    2.   
    3. #include <pantheios/implicit_link/core.h>  
    4. #include <pantheios/implicit_link/be.N.h>  
    5. #include <pantheios/implicit_link/be.WindowsConsole.h>  
    6. #include <pantheios/implicit_link/be.file.h>  
    7.   
    8. PANTHEIOS_CALL(int) pantheios_fe_init(void*   reserved,void**  ptoken)  
    9. {  
    10.     *ptoken = NULL;  
    11.     return 0;  
    12. }  
    13.   
    14. PANTHEIOS_CALL(void) pantheios_fe_uninit(void* token)  
    15. {}  
    16.   
    17. PANTHEIOS_CALL(PAN_CHAR_T const*)  pantheios_fe_getProcessIdentity  (void *  token)   
    18. {  
    19.     return PANTHEIOS_LITERAL_STRING("example");  
    20. }  
    21.   
    22. PANTHEIOS_CALL(int) pantheios_fe_isSeverityLogged(void* token  
    23.                                                   , int   severity  
    24.                                                   , int   backEndId)  
    25. {  
    26.     //数值越小说明要输出的信息越重要,常用的越先级大小如下  
    27.     //SEV_CRITICAL=2 < SEV_ERROR=3 < SEV_WARNING=4 < SEV_INFORMATIONAL=6 < SEV_DEBUG=7  
    28.     //backEndId=0的时候必须返回1,这样才会进一步调用这个函数,这时会传入详细的backEndId值(1或2)。  
    29.     if( backEndId==0 )  
    30.         return 1;  
    31.   
    32.     //Windows控制台中只记录SEV_INFORMATIONAL以上级别的信息  
    33.     if(severity <= pantheios::SEV_INFORMATIONAL && backEndId==1)  
    34.         return 1;//允许输出  
    35.   
    36.     //文件流中只记录严重错误的信息  
    37.     if(severity <= pantheios::SEV_ERROR && backEndId==2)  
    38.         return 1;//允许输出  
    39.   
    40.     return 0;   
    41. }  


    testPantheios.cpp

    [cpp] view plain copy
     
    1. // testPantheios.cpp : Defines the entry point for the console application.  
    2. //  
    3.   
    4. #include "stdafx.h"  
    5.   
    6. #include <pantheios/util/test/compiler_warnings_suppression.first_include.h>  
    7.   
    8. #define PANTHEIOS_NO_INCLUDE_OS_AND_3PTYLIB_STRING_ACCESS // Faster compilation  
    9.   
    10. /* Pantheios Header Files */  
    11. #include <pantheios/pantheios.hpp>                  // Pantheios C++ main header  
    12. #include <pantheios/backends/be.N.h>                // pan_be_N_t声明  
    13. #include <pantheios/backends/bec.WindowsConsole.h>  // Include the API for bec.WindowsConsole  
    14. #include <pantheios/backends/bec.file.h>            // be.file header  
    15.   
    16. /* Standard C/C++ Header Files */  
    17. #include <exception>                                // for std::exception  
    18. #include <new>                                      // for std::bad_alloc  
    19. #include <string>                                   // for std::string  
    20. #include <stdlib.h>                                 // for exit codes  
    21.   
    22. /* Windows Header Files */  
    23. # include <windows.h>                               // for console colour constants  
    24.   
    25. #include <pantheios/util/test/compiler_warnings_suppression.last_include.h>  
    26.   
    27. PANTHEIOS_EXTERN_C const PAN_CHAR_T PANTHEIOS_FE_PROCESS_IDENTITY[] = PANTHEIOS_LITERAL_STRING("example");  
    28. #define PSTR(x)         PANTHEIOS_LITERAL_STRING(x)  
    29.   
    30. /* ///////////////////////////////////////////////////////////////////////// 
    31.  * Logging management 
    32.  */  
    33. enum beid_target  
    34. {  
    35.     beid_Console =   1,  
    36.     beid_File =   2  
    37. };  
    38.   
    39. pan_be_N_t  PAN_BE_N_BACKEND_LIST[] =  
    40. {  
    41.     PANTHEIOS_BE_N_STDFORM_ENTRY(beid_Console, pantheios_be_WindowsConsole, 0),  
    42.     PANTHEIOS_BE_N_STDFORM_ENTRY(beid_File, pantheios_be_file, 0),  
    43.     PANTHEIOS_BE_N_TERMINATOR_ENTRY  
    44. };  
    45.   
    46. int _tmain(int argc, _TCHAR* argv[])  
    47. {  
    48.     try  
    49.     {  
    50.         //设置文件输出流  
    51.         //如果遇到同名文件,原文件的内容会被重写  
    52.         pantheios_be_file_setFilePath(PSTR("kagula.log"),   
    53.             PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BE_FILE_F_TRUNCATE, PANTHEIOS_BEID_ALL);  
    54.   
    55.         //同时输出到两个目标(文件流和Windows控制台)中  
    56.         //我重载了pantheios_fe_isSeverityLogged这样两个目标流的输出等级可以分别控制  
    57.         pantheios::log_DEBUG(PSTR("stmt log_DEBUG"));   //没有输出  
    58.         pantheios::log_NOTICE(PSTR("stmt log_NOTICE")); //只输出到控制台  
    59.         pantheios::log_ERROR( PTSTR(L"stmt 3"));        //输出到控制台和文件流中,log_ERROR不加L会乱码奇怪  
    60.         pantheios::log_EMERGENCY(PTSTR(L"stmt 4"));     //输出到控制台和文件流中,log_EMERGENCY不加L会乱码奇怪  
    61.   
    62.         return EXIT_SUCCESS;  
    63.     }  
    64.     catch(std::bad_alloc&)  
    65.     {  
    66.         pantheios::log(pantheios::alert, PSTR("out of memory"));  
    67.     }  
    68.     catch(std::exception& x)  
    69.     {  
    70.         pantheios::log_CRITICAL(PSTR("Exception: "), x);  
    71.     }  
    72.     catch(...)  
    73.     {  
    74.         pantheios::logputs(pantheios::emergency, PSTR("Unexpected unknown error"));  
    75.     }  
    76.   
    77.     return EXIT_FAILURE;  
    78. }  



    参考资料

    [1]Pantheios下载地址

    http://sourceforge.net/projects/pantheios/files/Pantheios%20(C%20and%20Cxx)/

    [2]stlsoft下载地址

    http://sourceforge.net/projects/stlsoft/files/latest/download

    [3]Pantheios官网

    http://pantheios.sourceforge.net/

    [4]《Pantheios LibrarySelector Tool》下载

    http://sourceforge.net/projects/pantheios/files/Pantheios%20Library%20Selector%20Tool/

    [5] Pantheios Library Selector Tool使用

    http://pantheios.sourceforge.net/tutorials_library_selector.html

    http://blog.csdn.net/lee353086/article/details/7196522

  • 相关阅读:
    tomcat配置
    java.net.ConnectException: Connection timed out: connect,java.net.ConnectException: Connection timed out: connect at java.net.DualStackPlainSocketImpl.waitForConnect
    Tomat 下载地址
    Gradle的依赖方式——Lombok在Gradle中的正确配置姿势 本文来源:码农网 本文链接:https://www.codercto.com/a/70161.html
    mssql 那表语句
    监控系统搭建
    vue 子组件触发父组件方法的两种方式
    css margin边界叠加问题详谈
    sticky footer
    JS的构造函数
  • 原文地址:https://www.cnblogs.com/findumars/p/7635889.html
Copyright © 2011-2022 走看看