Amps: source code explanation of the trace Module

Source: Internet
Author: User

Starting from this section, we will analyze the source code of each amps module. Chinese comments are added to the main sections.

The tracing function is similar to the logging function used by software, but the recorded information is more detailed than the log. It can be used to see the running track of the entire code. amps supports the following trace levels:

  • Error
  • Warning
  • Debug
  • Debug_2
  • Info
The following three tracking modes are supported:
  • Terminal interface display mode
  • File record mode
  • Both modes coexist.
The following is the source code: 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/* the maximum length of the trace file name */# define amps_size_of_trace_file_name_plus_time_stamp1024/* Each trace file ends with this */# defineamps_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 ); /* Data Structure Used by the tracking module */struct _ ampstrace {void * pvampscontext; plaintext; charpchtracefilepath [delimiter]; plaintext; t_ampsparameterlist plaintext; unsigned int unmoduleid; unsigned int untracemode; idle;}; int trace_init (void * r_pvampscontext, char * r_pchfilename, unsigned int idle, unsigned int r_untracemode); void trace_cleanup (void * r_pvampscontext ); int trim (void * r_pvampstrace, char * r_pchfilename); void reverse (void * r_pvampscontext, unsigned int r_untraceid); void reverse (void * r_pvampscontext, unsigned int r_untraceid ); void Merge (void * r_pvampscontext, unsigned int merge); void Merge (void * r_pvampscontext, unsigned int merge); 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/* # ifndef _ header_amps_trace_h __*/

Amps_trace.c

# Include "amps_defines.h" # include "amps_linklist.h" # include "example" # include "amps_core.h" # include "example" # include "amps_trace.h" # include "amps_core.h" # include "example" /* global tracking handle */t_ampstrace * g_poampstrace; /* Trace Level */Char ppchtracestr [5] [20] = {"error", "warning", "debug", "debug_2", "info "}; /*************************************** * ************************** Function Name: trace_init Function Description: Initialize the tracking module Module, which is called by core_init, the initial function of the AMPs core module. input parameter: void * r_pvampscontext amps application context data structure char * r_pchfilepath trace file path unsigned int r_untracelevel trace level. value: char ppchtracestr [5] [20] = {"error", "warning", "debug", "debug_2", "info"}; unsigned int r_untracelevel trace record mode, value: typedef Enum {amps_trace_mode_display = 1, amps_trace_mode_file = 2, latency = 3} e_ampstracemode; output parameter: -- Return Value: amps_success: Success amps _ Error_failure: fail *************************************** * ************************/INT trace_init (void * r_pvampscontext, char * r_pchfilepath, unsigned int r_untracelevel, unsigned int r_untracemode) {t_ampstrace * poampstrace = NULL; printf ("trace_init: Entering % s with mode % d. \ n ", r_pchfilepath, r_untracemode); poampstrace = (t_ampstrace *) amps_internalmalloc (sizeof (t_ampstrace); If (null = P Oampstrace) {printf ("trace_init: amps_internalmalloc failed. \ n "); Return signature;} poampstrace-> pvampscontext = r_pvampscontext; poampstrace-> untracelevel = role; Role-> unmoduleid = 0; poampstrace-> untracemode = r_untracemode; poampstrace-> ntraceid = 1; memcpy (poampstrace-> pchtracefilepath, r_pchfilepath, strlen (r_pchfilepath); If (amps_trace_mode_file = poampstrace-> Untracemode) | (amps_trace_mode_both = poampstrace-> untracemode) {printf ("opening file from init \ n"); If (amps_success! = Signature (poampstrace, poampstrace-> pchtracefilepath) {printf ("trace_init: sapi_fileopen failed \ n"); amps_internalfree (poampstrace); Return signature ;}} /* assign the initialized trace information to the global trace handle and save it */g_poampstrace = poampstrace; printf ("trace_init: leaving. \ n "); Return amps_success ;} /*************************************** * ************************** Function Name: trace_cleanup Function Description: cancels the tracking module. The core function is canceled by the AMPs core module. _ Cleanup call input parameter: void * r_pvampscontext amps application context data structure output parameter: -- return value: void *************************************** * ************************/void trace_cleanup (void * r_pvampscontext) {t_ampstrace * poampstrace = g_poampstrace; printf ("trace_cleanup: entering. \ n "); If (null = poampstrace) {return;}/* sets the file mode in the trace running mode, first, close the tracking file */If (amps_trace_mode_file = poampstrace-> untracemode) | (amps_trace_mode_both = poam Pstrace-> untracemode) {printf ("trace_cleanup: Call sapi_fileclose. \ n "); sapi_fileclose (r_pvampscontext, & poampstrace-> oampsfile);}/* release the global trace handle saved during initialization */amps_internalfree (poampstrace); printf (" trace_cleanup: leaving. \ n ");} /*************************************** * ************************** Function Name: amps_trace Function Description: this function can be called where information needs to be recorded. Generally, this function uses the following simplified format: # define trace (nmoduleid, ntracelevel, pmessage ,...) \ Amps_trace (_ line __, _ file __, _ FUNCTION __, nmoduleid, \ ntracelevel, pmessage, _ va_args _) input parameters :: int r_nlinenumber current hitting file row char * r_pchfilename current hitting file name const char * r_pchfunctionname current hitting function name unsigned int r_unmoduleid hitting module number unsigned int hitting tracking level char * r_puchmessage ,. .. output parameter of Trace content: -- return value: void *************************************** * ************************/void amps_trace (I NT r_nlinenumber, char * r_pchfilename, const char * r_pchfunctionname, unsigned int r_unmoduleid, unsigned int r_untracelevel, char * r_puchmessage ,...) {t_ampstrace * poampstrace = empty; unsigned int unmoduleid = 0; unsigned int untracelevel = 0; unsigned int untracemode = 0; If (null = poampstrace) {return ;} unmoduleid = poampstrace-> unmoduleid; untracelev El = poampstrace-> untracelevel; untracemode = poampstrace-> untracemode;/* If the module number entered by hitting is consistent with the module number used during initialization, and the tracking level is consistent, the trace information is just recorded. If no, no */If (unmoduleid & r_unmoduleid) & (untracelevel & r_untracelevel) {int noffset = 0; /* set the file path */char * pchfilename = strrchr (r_pchfilename, '\'); If (null = pchfilename) {pchfilename = r_pchfilename ;} else {pchfilename + = 1;}/* get different moving volumes based on different levels to obtain the Log Level of the corresponding string type */while (r_untrace Level = (r_untracelevel> 1 ))! = 0) {noffset ++;}/* Save the custom trace information to va_list */amps_init_param_list (oampsparameterlist, r_puchmessage ); /* obtain the system time */sapi_getcurrenttime (poampstrace-> pvampscontext, & poampstrace-> oampstimervalue);/* When the mode is terminal display, set the font color */If (amps_trace_mode_display & untracemode) {sapi_setconsoletextcolor (noffset, ppchtracestr [noffset], poampstrace-> oampstimervalue. nhour, poampstrace-> oampstimervalue. nminute, poampstrace-> oamps Timervalue. nsecond, poampstrace-> oampstimervalue. nmillisecond, pchfilename, r_nlinenumber, r_pchfunctionname); vprintf (r_puchmessage, success); empty (noffset);}/* processing in file mode */If (fail & untracemode & null! = Poampstrace-> oampsfile. hfilehandle) {fprintf (poampstrace-> oampsfile. hfilehandle, "%-7.8 S => % 3.02d: % 02d: % 02d: % 03d:", ppchtracestr [noffset], poampstrace-> oampstimervalue. nhour, poampstrace-> oampstimervalue. nminute, poampstrace-> oampstimervalue. nsecond, poampstrace-> oampstimervalue. nmillisecond); fprintf (poampstrace-> oampsfile. hfilehandle, "%-20.20 S: % 5D: %-30.30 S:", pchfilename, r_nlinenumber, r_pchfunc Tionname); // fprintf (poampstrace-> oampsfile. hfilehandle, "% 8.8 S: % 02d: % 02d: % 2D: % 03d:", ppchtracestr [noffset], poampstrace-> oampstimervalue. nhour, poampstrace-> oampstimervalue. nminute, poampstrace-> oampstimervalue. nsecond, poampstrace-> oampstimervalue. nmillisecond); // fprintf (poampstrace-> oampsfile. hfilehandle, "%-20.20 S: % 5D: %-30.30 S:", r_pchfilename, r_nlinenumber, r_pchfunctionname); vfprintf (poamps Trace-> oampsfile. hfilehandle, r_puchmessage, oampsparameterlist); fflush (poampstrace-> oampsfile. hfilehandle);}/* clear va_list */amps_cleanup_param_list (oampsparameterlist );}} /*************************************** * ************************** Function Name: trace_settracefortraceid Function Description: Set the input parameter of the tracing module: void * r_pvampscontext amps application context data structure unsigned int r_untraceid output parameter of the tracing module: -- return value: void ************************************ * ***************************/Void trace_settracefortraceid (void * r_pvampscontext, unsigned int r_untraceid) {t_ampstrace * poampstrace = g_poampstrace; If (null = poampstrace) {return;} poampstrace-> unmoduleid | = r_untraceid ;} /*************************************** * ************************** Function Name: trace_cleartracefortraceid Function Description: Clear the tracking module input parameter: void * r_pvampscontext amps application context data structure unsigned int r_untraceid and Module output parameter: -- return value: void *************************************** * ***********************/void trace_cleartracefortraceid (void * r_pvampscontext, unsigned int r_untraceid) {t_ampstrace * poampstrace = g_poampstrace; If (null = poampstrace) {return;} poampstrace-> unmoduleid & = (~ R_untraceid );} /*************************************** * ************************** Function Name: trace_settracelevel Function Description: Set the tracking level input parameter: void * r_pvampscontext amps application context data structure unsigned int r_untracelevel Trace Level output parameter: -- return value: void *************************************** * ************************/void trace_settracelevel (void * r_pvampscontext, unsigned int r_untracelevel) {t_ampstrace * poampstrace = g_poampstrace; If (null = Poampstrace) {return;} poampstrace-> untracelevel | = r_untracelevel ;} /*************************************** * ************************** Function Name: trace_cleartracelevel Function Description: clear tracking level input parameter: void * r_pvampscontext amps application context data structure unsigned int r_untracelevel Trace Level output parameter: -- return value: void *************************************** * ************************/void trace_cleartracelevel (void * r_pvampscontext, unsigned int r_untrace Level) {t_ampstrace * poampstrace = g_poampstrace; If (null = poampstrace) {return;} poampstrace-> untracelevel & = r_untracelevel ;} /*************************************** * ************************** Function Name: trace_settracemode Function Description: Set the input parameter of the tracing mode: void * r_pvampscontext amps application context data structure unsigned int r_untracemode output parameter of the tracing mode: -- return value: void *************************************** * *************************/void trace_settra Cemode (void * r_pvampscontext, unsigned int r_untracemode) {signature * poampstrace = signature; If (null = poampstrace) {return;} poampstrace-> untracemode | = r_untracemode; /* When file mode is used, open the tracking file */If (amps_trace_mode_file = poampstrace-> untracemode) | (amps_trace_mode_both = poampstrace-> untracemode )) {trace_openfilefortracing (poampstrace, poampstrace-> pchtracefilepath );}}/********************** **************************************** * ** Function Name: trace_cleartracemode Function Description: clear tracking mode input parameter: void * r_pvampscontext amps application context data structure unsigned int r_untracemode output parameter: -- return value: void *************************************** * ************************/void trace_cleartracemode (void * r_pvampscontext, unsigned int r_untracemode) {t_ampstrace * poampstrace = g_poampstrace; If (null = poampstrace) {return;} poampstrace-> Untracemode & = r_untracemode ;} /*************************************** * ************************** Function Name: trace_gettraceid Function Description: get the trace ID input parameter: void * r_pvampscontext amps application context data structure output parameter: -- return value: int trace ID ************************************* * **************************/INT trace_gettraceid (void * r_pvampscontext) {t_ampstrace * poampstrace = g_poampstrace; If (null = poampstrace) {return amps_invalid_trace_id;} PoA Mpstrace-> ntraceid * = 2; Return (poampstrace-> ntraceid );} /*************************************** * ************************** Function Name: trace_openfilefortracing Function Description: Open the tracking file input parameter: void * r_pvampstrace amps tracking Context Data Structure r_pvampstrace tracking file name output parameter: -- return value: amps_success amps_error_failure ************************************** * ************************/INT trace_openfilefortracing (void * r_pvampstrace, char * r_pchfilename) {T_ampstrace * poampstrace = r_pvampstrace; char character [character]; If (null = poampstrace) {return amps_error_failure;} memset (pchfilenamewithtimestamp, 0, random ); sapi_getcurrenttime (poampstrace-> pvampscontext, & poampstrace-> oampstimervalue); sprintf (pchfilenamewithtimestamp, "% S/% d. % d. % d. % d _ % s ", poampstrace-> P Chtracefilepath, poampstrace-> example. nhour, poampstrace-> example. nminute, poampstrace-> example. nsecond, poampstrace-> example. nmillisecond, example); If (example! = Sapi_fileopen (poampstrace-> pvampscontext, & poampstrace-> oampsfile, parameters, amps_true) {printf ("outputs: sapi_fileopen failed \ n"); Return outputs;} return outputs ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.