[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



On Thu, Aug 26, 2004 at 11:13:58AM +0200, Kristian Nørgaard wrote:
> Jeg kunne tænke mig en log-funktionalitet, der én gang i sekundet 
> spørger et stykke hardware om dets status, skriver denne status til 
> memory, og som - i tilfælde af fejl i status - dumper denne memory til 
> en fil.
> 
> 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.
> 
> Jeg kan naturligvis forsøge at implementere det selv ved hjælp af nogle 
> hægtede lister el. lign., men måske findes, der allerede nogle classer 
> med denne funktionalitet?
> 
> Findes der f.eks. en stream af en eller anden slags, hvor jeg kan 
> definere dens kapacitet, og som har denne FIFO funktionalitet?

Du skal selv definere en log_entry_t der implementerer
 "bool is_bad() const"          <- sand hvis fejl
og
 "operator std::string()() const"   <- konvertering til streng

- men så skulle nedenstående virke:

---------------------------------------------
std::list<log_entry_t> entries;
std::ofstream logfile("bad_entries.log");
void log_entry(const log_entry_t &entry)
{
 // Husk entry
 entries.push_back(entry);
 // Begræns til 1000 entries i RAM
 while (entries.size() > 1000) entries.pop_front();
 // Skriv ud hvis dårlig
 if (entry.is_bad())
  logfile << entry << std::endl;
}
---------------------------------------------


F.eks. kunne din log_entry_t se ud som:

---------------------------------------------
struct log_entry_t {
 std::string text;
 bool is_bad() const { return text.find("error") != text.npos; }
 operator std::string()() const { return text; }
};
---------------------------------------------

Det er frit fra fantasien det her, jeg har ikke checket om det compiler
- men ud over skrivefejl skulle det da være tæt på   ;)

Du kan også lave det simplere uden en log_entry_t - men med log_entry_t
typen har du frihed til f.eks. at inkludere flere felter, evt. parse når
du indlæser for at gøre senere behandling af dine entries mere effektiv.

-- 

 / jakob



 
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] *