Look at the Log Module in amps. There are two ways to write logs. One is to write logs directly using encapsulated API functions, and the other is the log callback function registered at the application layer, use the event management mechanism to write.
Amps_log.h
#ifndef __HEADER_AMPS_LOG_H__#define __HEADER_AMPS_LOG_H__#include <stdio.h>#include "AMPS_SystemAPI.h"#include "AMPS_Core.h"#include "AMPS_Components.h"#ifdef __cplusplus extern "C" {#endif#define AMPS_LOG_MAX_SIZE_OF_WRITE_BUFFER (1024)typedef struct _AMPSLogContext t_AMPSLogContext;struct _AMPSLogContext{ void* pvAMPSContext;void* pvAMPSNetworkMsg;};void* Log_Init(void* r_pvAMPSContext, char* r_pchFileName);void Log_Cleanup(void* r_pvAMPSLogContext);//AMPS_API int AMPS_Log(void* r_pvAMPSContext, const char* r_puchMessage , ...);int Log_HandleFileWriteEvent(t_AMPSContext* r_poAMPSContext, t_AMPSSNMEvent* r_poAMPSSNMEvent, t_AMPSNetworkMsg* r_poAMPSNetworkMsg);#ifdef __cplusplus }#endif#endif /*#ifndef __HEADER_AMPS_LOG_H__*/
Amps_log.c
# Include "amps_defines.h" # include "amps_linklist.h" # include "example" # include "amps_core.h" # include "amps_memmgt.h" # include "amps_log.h "/********* **************************************** * **************** Function Name: log_init Function Description: log module initialization input parameter: void * r_pvampscontext APMs application context char * r_pchfilename log file name output parameter: return value: void * log handle ************************************ * ****************************/void * log_init (Void * r_pvampscontext, char * r_pchfilename) {t_ampslogcontext * poampslogcontext = NULL; invalid variable; t_ampsnetworkmsg * poampsnetworkmsg = NULL; trace (log_trace_id (r_pvampscontext. \ n "); poampslogcontext = (t_ampslogcontext *) amps_internalmalloc (sizeof (t_ampslogcontext); If (null = poampslogcontext) {trace (log_trace_id (r_pvampspscontext ), _ Level_error, "amps_internalmalloc failed. \ n "); return NULL;}/* Open in append mode (if not, create) log file */poampslogcontext-> pvampsnetworkmsg = sapi_fileopeninterface (r_pvampscontext, (const char *) r_pchfilename, amps_true, failed); If (null = poampslogcontext-> pvampsnetworkmsg) {trace (log_trace_id (r_pvampscontext), failed, "sapi_fileopenforwriting failed. \ n "); return NULL ;} Poampsnetworkmsg = (t_ampsnetworkmsg *) poampslogcontext-> pvampsnetworkmsg;/* register a log write event */register (r_pvampscontext, poampsnetworkmsg, log_handlefilewriteevent ); /* register an I/O Event Notification */register (r_pvampscontext, & oam?pollevent, poampsnetworkmsg); Register (r_pvampscontext, & oam=pollevent); Register (r_pvampscontext, & oam=pollevent ); /* If (amps_success! = Parse (r_pvampscontext, poampsnetworkmsg-> nhandle, & oam1_pollevent) {trace (log_trace_id (r_pvampscontext), amps_trace_level_error, "invalid failed \ n"); return NULL ;} */poampslogcontext-> pvampscontext = r_pvampscontext; trace (log_trace_id (r_pvampscontext), amps_trace_level_info, "leaving % P. \ n ", poampslogcontext); Return poampslogcontext ;} /********************************** * ****************************** Function name: log_cleanup function description: log Module destruction input parameter: void * r_pvampscontext APMs application context output parameter: return value: na *************************************** * ************************/void log_cleanup (void * r_pvampslogcontext) {t_ampslogcontext * poampslogcontext = r_pvampslogcontext; void * pvampscontext = poampslogcontext> pvampscontext; trace (log_trace_id (pvampscontext), amps_trace_level_info, "entering % P. \ N ", poampslogcontext);/* close the file */disconnect (pvampscontext, poampslogcontext-> pvampsnetworkmsg); disconnect (r_pvampslogcontext); trace (log_trace_id (pvampscontext), locate," leaving. \ n ");} /*************************************** * ************************** Function Name: amps_log Function Description: logging point function input parameter: void * r_pvampscontext APMs application context const char * r_puchmessage log content... variable Length Parameter output parameter: Return Value: int ** **************************************** * *********************/INT amps_log (void * r_pvampscontext, const char * r_puchmessage ,...) {t_ampslogcontext * poampslogcontext = publish (publish); t_ampsnetworkmsg * poampsnetworkmsg = poampslogcontext-> pvampsnetworkmsg; Publish success; void * handle = NULL; int timeout = 0; int timeout = 0; int nretur Nvalue = amps_error_failure; int nbyteswritten = 0; trace (log_trace_id (r_pvampscontext), amps_trace_level_info, "entering. \ n "); callback (oampsparameterlist, r_puchmessage); pvmessagetosend = amps_internalmalloc (callback); If (null = pvmessagetosend) {trace (log_trace_id (r_pvampscontext), callback, "amps_internalmalloc failed for pvmessagetosend. \ n "); Return response;}/* assemble log information */nsizeofmessage = parse (r_pvampscontext, pvmessagetosend, & found, r_puchmessage); If (0> = nsizeofmessage) {trace (log_trace_id (r_pvampscontext ), amps_trace_level_error, "sapi_writetostring failed. \ n "); amps_internalfree (pvmessagetosend); Return amps_error_failure;}/* get the message queue length */nlengthofqueue = queue_getlistbasedqueuelength (r_pvampscontext Context, & Poampsnetworkmsg-> oampslistbasedqueue); If (0 <nlengthofqueue) {If (amps_success! = Trace (r_pvampscontext, poampsnetworkmsg, & poampsnetworkmsg-> messages, pvmessagetosend, nsizeofmessage, 0, null) {trace (amps_trace_id (r_pvampscontext), callback, "failed. \ n "); Return amps_error_failure;} return amps_success;}/* write log */nreturnvalue = sapi_filewrite (r_pvampscontext, poampsnetworkmsg, pvmessagetosend, nsizeofmesregular E, & nbyteswritten); If (response = nreturnvalue) {trace (log_trace_id (r_pvampscontext), amps_trace_level_warning, "file is marked non-blocking and the write operation wocould block. \ n ");} elseif (amps_success = nreturnvalue) {trace (log_trace_id (r_pvampscontext), amps_trace_level_debug," sapi_filewrite is successfull. \ n "); amps_internalfree (pvmessagetosend);} elseif (amps_error_failur E = nreturnvalue) {trace (log_trace_id (r_pvampscontext), amps_trace_level_error, "sapi_filewrite failed. \ n "); amps_internalfree (pvmessagetosend); Messages (oampsparameterlist); Return queue;} Queue (Queue); trace (log_trace_id (r_pvampscontext), queue," leaving \ n "); return amps_success ;} /*************************************** ** * ********************** Function name: log_handlefilewriteevent Function Description: log Event Callback Function input parameter :: void * r_pvampscontext APMs application context t_ampssnmevent * r_poampssnmevent event context t_ampsnetworkmsg * r_poampsnetworkmsg output parameter: return value: int *************************************** * ***********************/INT log_handlefilewriteevent (t_ampscontext * r_poampscontext, t_ampssnmevent * r_poampssnmevent, t_ampsnetworkmsg * r_poampsnetworkmsg) {int nleng Thofqueue = 0; void * pvmessagetosend = NULL; intnsizeofmessage = 0; intnmessageoffset = 0; int nreturnvalue = success; int timeout = 0; trace (log_trace_id (r_poampscontext), fail, "event is epollout. \ n ");/* Get the length of the event queue */nlengthofqueue = Queue (r_poampscontext, & r_poampsnetworkmsg-> Queue); For (; nlengthofqueue> 0; nlengthofqueue --){/ * Retrieve messages and related information from the event queue */If (amps_success! = Parse (r_poampscontext, r_poampsnetworkmsg, & r_poampsnetworkmsg-> messages, & pvmessagetosend, & nsizeofmessage, & nmessageoffset, null) {trace (log_trace_id (r_poampscontext), callback, "failed \ n"); continue;} If (null = pvmessagetosend) | (0 >= nsizeofmessage) {trace (log_trace_id (r_poampscontext), amps_trace_level_error, "Nu Ll pointers \ n "); continue;}/* write log */nreturnvalue = sapi_filewrite (r_poampscontext, response, pvmessagetosend, nsizeofmessage, & response); If (response = nreturnvalue) {trace (log_trace_id (r_poampscontext), locate, "file is marked non-blocking and write operation wowould block \ n"); Return amps_success;} elseif (amps_success = nreturnvalue) {trace (log_tra Ce_id (r_poampscontext), amps_trace_level_debug, "sapi_filewrite is successfull. \ n "); amps_internalfree (pvmessagetosend);} elseif (response = nreturnvalue) {trace (log_trace_id (r_poampscontext), amps_trace_level_error," sapi_filewrite failed. \ n "); amps_internalfree (pvmessagetosend); return amps_success ;}/ * destroy the registered I/O event */If (amps_success! = Parse (r_poampscontext, r_poampsnetworkmsg, timeout) {trace (log_trace_id (r_poampscontext), amps_trace_level_error, "Operation is failed \ n");} return amps_success ;}