当前位置:编程学习 > C/C++ >>

AMPS:Trace模块源码解读

 从本节起,开始分析AMPS各模块的源码,其中主要地方均加了中文注释。
 
  跟踪功能与通常软件使用的日志功能类似,但记录的信息比日志更详细,通过它可以看出整个代码的运行轨迹,AMPS支持多以下几种跟踪级别:
 
 
 ERROR
 WARNING
 DEBUG
 DEBUG_2
 INFO
 并支持如下三种跟踪模式:
  终端界面显示模式
 文件记录模式  
 以上两种模式并存  
  下面是源码:
 
 
AMPS_Trace.h
 
[cpp]  
#ifndef __HEADER_AMPS_TRACE_H__   
#define __HEADER_AMPS_TRACE_H__   
  
#include "AMPS_SystemAPI.h"   
#include "AMPS_API.h"   
  
#ifdef __cplusplus   
    extern "C" {  
#endif   
  
/*跟踪文件名最大长度*/  
#define AMPS_SIZE_OF_TRACE_FILE_NAME_PLUS_TIME_STAMP                                1024   
  
/*每个跟踪文件均以此结尾*/  
#define AMPS_TRACE_FILE_NAME                                                        "trace.txt"   
  
  
typedef struct _AMPSTrace                       t_AMPSTrace;  
  
typedef void(*Trace_Callback)(void* r_pvAMPSTrace, int r_nLineNumber, char* r_pchFileName, const char* r_pchFunctionName, char* r_puchMessage);  
  
/*跟踪模块使用的数据结构*/  
struct _AMPSTrace  
{  
    void*               pvAMPSContext;  
  
    t_AMPSFile          oAMPSFile;  
    char                pchTraceFilePath[AMPS_SIZE_OF_TRACE_FILE_NAME_PLUS_TIME_STAMP];  
  
    t_AMPSTimerValue    oAMPSTimerValue;  
    t_AMPSParameterList oAMPSParameterList;   
  
    unsigned int        unModuleId;  
    unsigned int        unTraceLevel;  
    unsigned int        unTraceMode;  
  
    int                 nTraceID;  
};  
  
  
int Trace_Init(void* r_pvAMPSContext, char* r_pchFileName, unsigned int r_unTraceLevel, unsigned int r_unTraceMode);  
void Trace_Cleanup(void* r_pvAMPSContext);  
  
int Trace_OpenFileForTracing(void* r_pvAMPSTrace, char* r_pchFileName);  
  
void Trace_SetTraceForTraceID(void* r_pvAMPSContext, unsigned int r_unTraceID);  
void Trace_ClearTraceForTraceID(void* r_pvAMPSContext, unsigned int r_unTraceID);  
  
void Trace_SetTraceLevel(void* r_pvAMPSContext, unsigned int r_unTraceLevel);  
void Trace_ClearTraceLevel(void* r_pvAMPSContext, unsigned int r_unTraceLevel);  
  
void Trace_SetTraceMode(void* r_pvAMPSContext, unsigned int r_unTraceMode);  
void Trace_ClearTraceMode(void* r_pvAMPSContext, unsigned int r_unTraceMode);  
  
int Trace_GetTraceID(void* r_pvAMPSContext);  
  
#ifdef __cplusplus   
   }  
#endif   
  
#endif /*#ifndef __HEADER_AMPS_TRACE_H__*/  
 
#ifndef __HEADER_AMPS_TRACE_H__
#define __HEADER_AMPS_TRACE_H__
 
#include "AMPS_SystemAPI.h"
#include "AMPS_API.h"
 
#ifdef __cplusplus
    extern "C" {
#endif
 
/*跟踪文件名最大长度*/
#define AMPS_SIZE_OF_TRACE_FILE_NAME_PLUS_TIME_STAMP 1024
 
/*每个跟踪文件均以此结尾*/
#define AMPS_TRACE_FILE_NAME "trace.txt"
 
 
typedef struct _AMPSTrace t_AMPSTrace;
 
typedef void(*Trace_Callback)(void* r_pvAMPSTrace, int r_nLineNumber, char* r_pchFileName, const char* r_pchFunctionName, char* r_puchMessage);
 
/*跟踪模块使用的数据结构*/
struct _AMPSTrace
{
void* pvAMPSContext;
 
t_AMPSFile oAMPSFile;
char pchTraceFilePath[AMPS_SIZE_OF_TRACE_FILE_NAME_PLUS_TIME_STAMP];
 
    t_AMPSTimerValue oAMPSTimerValue;
    t_AMPSParameterList oAMPSParameterList; 
 
unsigned int unModuleId;
unsigned int unTraceLevel;
unsigned int unTraceMode;
 
int nTraceID;
};
 
 
int Trace_Init(void* r_pvAMPSContext, char* r_pchFileName, unsigned int r_unTraceLevel, unsigned int r_unTraceMode);
void Trace_Cleanup(void* r_pvAMPSContext);
 
int Trace_OpenFileForTracing(void* r_pvAMPSTrace, char* r_pchFileName);
 
void Trace_SetTraceForTraceID(void* r_pvAMPSContext, unsigned int r_unTraceID);
void Trace_ClearTraceForTraceID(void* r_pvAMPSContext, unsigned int r_unTraceID);
 
void Trace_SetTraceLevel(void* r_pvAMPSContext, unsigned int r_unTraceLevel);
void Trace_ClearTraceLevel(void* r_pvAMPSContext, unsigned int r_unTraceLevel);
 
void Trace_SetTraceMode(void* r_pvAMPSContext, unsigned int r_unTraceMode);
void Trace_ClearTraceMode(void* r_pvAMPSContext, unsigned int r_unTraceMode);
 
int Trace_GetTraceID(void* r_pvAMPSContext);
 
#ifdef __cplusplus
   }
#endif
 
#endif /*#ifndef __HEADER_AMPS_TRACE_H__*/
 
AMPS_Trace.c
 
 
 
[cpp]  
#include "AMPS_Defines.h"   
#include "AMPS_LinkList.h"   
#include "AMPS_SystemAPI.h"   
#include "AMPS_Core.h"   
#include "AMPS_MemMgt.h"   
#include "AMPS_Trace.h"   
#include "AMPS_Core.h"   
#include "AMPS_API.h"   
  
  
/*全局跟踪句柄*/  
t_AMPSTrace* g_poAMPSTrace;  
  
/*跟踪级别*/  
char ppchTraceStr[5][20] =   
{  
"ERROR",  
"WARNING",  
"DEBUG", &nbs
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,