Netduino home hardware projects downloads community

Jump to content


Photo

Creating Log Files.


Best Answer ziggurat29, 06 March 2013 - 03:49 PM

I think ash is wanting to drop old log lines and add new ones to a simple textual log file.

 

The short answer is typically 'no, there is not a simple way to do this', because a files is a simple linear sequence of byte, much like a hunk of memory, only on secondary storage.  So, just as with a hunk of memory, to do it you would need to copy all the stuff down over what you are dropping, and then append what is the new record.  To cope with that limitation, two approaches are sometmes taken, either:

 

a )  use some storage that is structured, like a database, then the record ordering is logical rather than physical, and you can avoid moving the stuff to effectively drop the oldest and add the newest.  You could use something like sqlite etc on desktop if you really wanted to do that, but this is embedded so that approach is not feasible.

 

or

 

b )  use a log rotation scheme.  basically you write to a log file until it reaches a certain size, then open a new one, continuing from there.  At the same time and just prior to opening the new file. you delete the oldest one.  Usually you have some parameters such as 'how many old logs to keep' and 'how big will a log get before you move on to a new one'.  If you think about it, this is effectively the same as approach 'a', except that you are using the filesystem directory as your quasi-database, and you are working with groups of log lines (in a file) instead of individual ones.

 

 

-dave

Go to the full post


  • Please log in to reply
3 replies to this topic

#1 ash

ash

    Advanced Member

  • Members
  • PipPipPip
  • 66 posts

Posted 06 March 2013 - 02:51 PM

I've a log file which i append to every so often. (Thats the easy bit)

 

What i want to do is if its gets above a certain size then delete the old 1 st line before add the latest line to the bottom of the file.

 

Is there a way of doing this? i cant exactly read the whole file as i will run out memory :(



#2 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1961 posts
  • LocationBreda, the Netherlands

Posted 06 March 2013 - 03:14 PM

Does this work? I haven't tested this because I don't have a netduino with me at this moment, but I think this should work:

string filename = "SDlogfile.txt";FileStream stream = File.OpenRead(filename);long filesize = stream.Length;

"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#3 ziggurat29

ziggurat29

    Advanced Member

  • Members
  • PipPipPip
  • 244 posts

Posted 06 March 2013 - 03:49 PM   Best Answer

I think ash is wanting to drop old log lines and add new ones to a simple textual log file.

 

The short answer is typically 'no, there is not a simple way to do this', because a files is a simple linear sequence of byte, much like a hunk of memory, only on secondary storage.  So, just as with a hunk of memory, to do it you would need to copy all the stuff down over what you are dropping, and then append what is the new record.  To cope with that limitation, two approaches are sometmes taken, either:

 

a )  use some storage that is structured, like a database, then the record ordering is logical rather than physical, and you can avoid moving the stuff to effectively drop the oldest and add the newest.  You could use something like sqlite etc on desktop if you really wanted to do that, but this is embedded so that approach is not feasible.

 

or

 

b )  use a log rotation scheme.  basically you write to a log file until it reaches a certain size, then open a new one, continuing from there.  At the same time and just prior to opening the new file. you delete the oldest one.  Usually you have some parameters such as 'how many old logs to keep' and 'how big will a log get before you move on to a new one'.  If you think about it, this is effectively the same as approach 'a', except that you are using the filesystem directory as your quasi-database, and you are working with groups of log lines (in a file) instead of individual ones.

 

 

-dave



#4 ash

ash

    Advanced Member

  • Members
  • PipPipPip
  • 66 posts

Posted 06 March 2013 - 04:39 PM

Dave thanks very much option b seems to the best and the most easy to implement :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2010-2014 Secret Labs LLC  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.