[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [C/C++] skrive logfil til memory med FIFO



Kristian Nørgaard <sslug@sslug> writes:

> Log-meddelelserne skal helst kun være de seneste (f.eks.) 1000
> status-meddelelser , dvs. jeg ville gerne have en slags
> First-In-First-Out funktionalitet, så status-meddelelse nr. 1001
> skubber status-meddelelse nr. 1 ud.

Lav et helt almindeligt array og brug en tæller der tæller op modulo
1000:

char *logmesg[1000];
int lastlog;
void log(char *msg, size_t n) {
    lastlog++;
    lastlog %= 1000;
    free(logmesg[lastlog]);
    logmesg[lastlog] = strndup(msg,n);
}
   

Hvis du dynamisk skal kunne ændre antallet af logbeskeder ville jeg
lave en cyklisk liste:

struct logmsg { char *msg; logmsg *next };
logmsg *lastlog;

void addlogspace(int n) {
    while(n--) {
        logmsg *newlog;
        newlog = (logmsg*) malloc(sizeof(lastlog));
        newlog.next = lastlog.next;
        newlog.msg = NULL;
        lastlog.next = newlog;
    }
}        
        
void initlog(int size) {
    lastlog = (lastlog*) malloc(sizeof(lastlog));
    lastlog.next = lastlog;
    lastlog.msg = NULL;
    addlogspace(size);
}

void log(char *msg, size_t n) {
    lastlog = lastlog.next;
    free(lastlog.msg); 
    lastlog.msg = strndup(msg,n);
}


Det overlades til læseren at lave reentrante versioner eller at lave
versioner der virker.

-- 
 Peter Makholm     |              Emacs is the only modern general-purpose
 sslug@sslug |               operating system that doesn't multitask
 http://hacking.dk |                                                      


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 22:44 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *