R2 Study Record

Source: Internet
Author: User

Setting up the Libevent library
1. Rewrite log behavior

#include <event2/Event.h>#include<stdio.h>Static voidDISCARD_CB (intSeverityConst Char*msg) {    /*This is callback does nothing.*/}StaticFILE *logfile =NULL;Static voidWRITE_TO_FILE_CB (intSeverityConst Char*msg) {    Const Char*s; if(!logfile)return; Switch(severity) { Case_event_log_debug:s="Debug";  Case_event_log_msg:s="msg";  Case_event_log_warn:s="warn";  Case_event_log_err:s="Error"; default:/*never reached*/s="?"; } fprintf (LogFile,"[%s]%s\n", S, msg);}/*Turn off all logging from Libevent.*/voidSuppress_logging (void) {event_set_log_callback (DISCARD_CB);}/*Redirect all Libevent logs messages to the C stdio file ' F '.*/voidSet_logfile (FILE *f) {logfile=F; Event_set_log_callback (WRITE_TO_FILE_CB);}

2. Override the exception exit action:

void (*EVENT_FATAL_CB) (int  err); void event_set_fatal_callback (EVENT_FATAL_CB CB);

3. Override the memory management method:

void event_set_mem_functions (void * (*malloc_fn) (size_t sz),void * (*REALLOC_FN) (void *ptr, size_t sz), void (*FREE_FN) (void *ptr));

4. Locks and Threads
Libevent default support for Win and Unix-like line libraries, if you want to implement the line libraries you need to implement:
Locks
Locking
Unlocking
Lock allocation
Lock destruction
Conditions
Condition variable Creation
Condition variable Destruction
Waiting on a condition variable
Signaling/broadcasting to a condition variable
Threads
Thread ID Detection

#defineEvthread_write 0x04#defineEvthread_read 0x08#defineEvthread_try 0x10#defineEvthread_locktype_recursive 1#defineEvthread_locktype_readwrite 2#defineEvthread_lock_api_version 1structEvthread_lock_callbacks {intlock_api_version;        unsigned supported_locktypes; void*(*alloc)        (unsigned locktype); void(* Free)(void*Lock, unsigned locktype); int(*Lock) (unsigned mode,void*Lock); int(*unlock) (unsigned mode,void*Lock);};intEvthread_set_lock_callbacks (Const structEvthread_lock_callbacks *);voidEvthread_set_id_callback (unsignedLong(*ID_FN) (void));structEvthread_condition_callbacks {intcondition_api_version; void*(*alloc_condition)        (unsigned condtype); void(*free_condition) (void*cond); int(*signal_condition) (void*cond,intbroadcast); int(*wait_condition) (void*cond,void*Lock,                        Const structTimeval *timeout);};intEvthread_set_condition_callbacks (Const structEvthread_condition_callbacks *);

R2 Study Record

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.