Question

I have two structs like so:

public struct KeyLog
{
    Keys key;
    DateTime time;
}

public struct MouseLog
{
    MouseEvents mouse;
    Point coordinates;
    DateTime time;
}

For every keyboard key press and mousebutton click I wish to save this data but I do not know which way would be the most efficient to store/handle it in? Are there better ways to handle the data - I'm not sure if using two structs is better than merging both into one?

Edit: I'm doing a keyboard & mouse statistic application that'll store the amount of key press & mouse clicks as well as what button was pressed, where and when for my computer and I would want to save this data every time a button is pressed. Not necessarily write to disk every time but at least store it in memory till I want to save it to disk.

Edit: I thought that if I keep the two structs separate I won't create too much dead data when I store them, and then I can easily search/sort if I keep them separate. Thoughts?

Was it helpful?

Solution

A BinaryFormatter would give you the smallest compression. Also if you change them to classes, you could have a base class with a DateTime time field.

OTHER TIPS

Merging them will make them easy to program with..

BUt as you are more conscious about saving disk space

You could use a generic collection of MouseLog and Keylog

eg List List for in memory representation

AS for writing it to a disk you could put them in a class and save them as one Big object

like

  [serializable]
    class BigObject 
    {

    List<Mouselog> MouseLogLst;


    List<KeyLog>  KeyLogLst ;

    }

and use Binary Formmater to store them

I often save it sequentially into a binary file with a very simple data access layer. There are a few approaches you can use here but I like this one the most, it's rather simple and it's fast. You can even create an index table to entries in the file very easily and much more.

class Foo {
  private int _someInteger;
  private string _someString;

  public virtual void Serialize(BinaryWriter writer) {
    writer.Write(2); // A simple versioning, start at 0, then increment.
    writer.Write(_someString);
    writer.Write(_someInteger);
  }

  public virtual void Deserialize(BinaryReader reader) {
    int version = reader.ReadInt32();
    switch (version) {
      case 2: // the string was only added in version 2 of the class serialization.
        _someString = reader.ReadString(); 
        goto case 1;
      case 1: // continue at case 1.
        _someInteger = reader.ReadInt32();
        break; // break here, because version 0 was very different.
      case 0: // in version 0 of the class there was actually a double, but it was always an integral number, so this basically is a conversion case.
        _someInteger = (int)reader.ReadDouble();
        break;
    }
  }
}

Just take care you read them from the file in the same order as you write them.

You could then just pass it the readers and writers from a stream, e.g.

Foo fooInstance = new Foo();
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fileStream);

fooInstance.Deserialize(reader);

It's so simple and you don't have to expose all fields and properties with set-accessors for the stupid XML serializer classes that gives you problems all the time.

Unless there is a good reason to keep them separate you should merge them. KISS applies here I believe.

er.... where are you planning to store it? If you're just wanting an in-memory log that you can refer to, a List<X> should be fine.

If you want to stream it to disk, you'll want to serialize it. Binary serialization is the most efficient in terms of total disk space, but XML serialization is "human readable".

Then there's database storage...

Do you have to worry about Unicode or can this be ASCII? I would say XML is your friend. Do not merge the two, but make both classes, which are both Children of an abstract EventLog. EventLog class must force a toString() method on you, maybe a few others. Then have a list of EventLogs, where each EventLog is a KeyLog or a MouseLog. I wonder if you are reinventing the wheel ...

I would store it in one struct, beacuse it will most likely save you time as a programmer. It will be simpler to deal with, especially when saving/loading the file later on. There is little to be gained from using structs which you have described.

And if that should become a performance problem, profile the code see where any bottlenecks are and then fix them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top