med-material / ArduinoLogger

Source code for an Arduino Logger (Windows/Mac) used for the advanced human computer interaction (AMMI) course at AAU (Unity 2018.3.2f1)
MIT License
3 stars 2 forks source link

Fixing read-only issue, fixing bugs/adding features and adding LoggingExample #48

Closed Vincent-SD closed 2 years ago

Vincent-SD commented 2 years ago

The documentation of the loggingManager can be downloaded here

See below for an overview of this documentation:

Logging Manager

Table of contents

What is it

The logging manager is a set of C# functions that can be used to log easily data into csv files and into a MySQL database. It can be considered as an API that you can include in your Unity projects.

How to use

Import into project

Import the Logging Manager

First of all, you'll need to get the last release of the Logging Manager. This version can be download here. //TODO put link

Then, in your Unity project, go to Assets - Import Package - Custom Package and select the Logging Manager you just downloaded.

Create a LoggingManager game object

Create an Empty GameObject that you can call LoggingManager. Add the LoggingManager script to the game object by going to Add Component and writing LoggingManager.

Use in your project

Recover the LoggingManager

To use the LoggingManager in your code, you'll need to recover it. You can do this like this :

LoggingManager = GameObject.Find("LoggingManager").GetComponent<LoggingManager>();

Start logging

Now that you have set up the LoggingManager, you are able to log data. First, you need to create the Log connection using a Label:

LoggingManager.CreateLog("myLabel");

Then, you can start logging your data using 2 functions. If you want to log only one column, use:

LoggingManager.Log("myLabel","myColumn",myValue);

If you want to log more than one column, use:

Dictionary<string, object> logs = new Dictionary<string, object>();
logs.Add("column1",value1);
logs.Add("column2",value2);
LoggingManager.Log("myLabel",logs);

In each case, right after you call the Log function, the log row will be ended and common colums like TimeStamp, Framecount, SessionID and Email will be added to the logs.

NOTE: The SessionID is a unique randomly generated string. All logs created in the session will have the same SessionID. To generate a new one, you can call the LoggingManager.NewFileStamp() function.

Save the logs

When you are done logging, you can save your logs. The way the logs will be saved depends on how you configured the LoggingManager in Unity (see the Parameters section).

The saves functions take as parameters the label of the logs you want to save and a "shouldClear" boolean. If set to true, the logs will be cleared after you save them.

To save one specific collection, you can use:

LoggingManager.SaveLog("collectionLabel", true);

You can also decide to only save to CSV or MySQL. To do that, simply specify the target when calling the save function:

LoggingManager.SaveLog("collectionLabel", true, TargetType.CSV);
LoggingManager.SaveLog("collectionLabel", true, TargetType.MySql);

You can also save all your logs by calling:

LoggingManager.SaveAllLogs(true);

Of course you can specify the target you want to save here too:

LoggingManager.SaveAllLogs(true, TargetType.CSV);

Behavior

Header definition

The header of a log is defined by the names of its columns. In the LoggingManager, the headers are defined when you log your first row using the Log() function.

Let's take a loot at this example:

Dictionary<string, object> logs = new Dictionary<string, object>();
logs.Add("column1",value1);
logs.Add("column2",value2);
logs.Add("column3",value3);
LoggingManager.Log("myLabel",logs);

Here, the headers are column1, column2 and column3. If you add a new header later in your code, the LoggingManager will add the new column and set all its previous values to NULL. headers

NOTE: It is a good practice to define all headers when logging the first row. Adding new headers later in the code, although supported, may result in loss of performance. Also, doing this will disable the log string over time feature if it is enabled.

Parameters

On the LoggingManager game object, you can configure some parameters. Those parameters will change the behavior of the logging process and you can edit them by ticking the boxes or editing the text fields.

Enable CSV save

Tick the case if you want to save the logs into csv files.

Enable MySQL save

Tick the case if you want to send the logs to the database.

Create meta collection

If set to true, the LoggingManager will create a meta log automatically. A meta log is a log that contains all information that need to be logged only once. It can be for instance the age of the player or a comment about the session.

Each meta log will also have a SessionID column created automatically. This SessionID will be the same as the one created in your regular logs.

If you want to log data in the Meta logs, simply do this using the Log function:

Dictionary<string, object> logs = new Dictionary<string, object>();
logs.Add("age",22);
logs.Add("comment","this is a comment");
LoggingManager.Log("Meta",logs);

Log string over time

When you log your data, the data have to be exported as a string to be saved into a CSV file or sent to a MySQL database. This will be our "datastring" and it will be in the following format:

TimeStamp,positionX;positionY;positionZ\n2022-01-19 11:30:34.8123;100,50,100\n2022-01-19 11:30:35.8426;110,50,110...

This datastring would produce a CSV file like this:

TimeStamp positionX positionY positionZ
2022-01-19 11:30:34.8123 100 50 100
2022-01-19 11:30:35.8426 110 50 110

To log this data, we have two possibilities.

Export the log string at the end of the logging process

In this case, the log string will be exported at the end of the logging process. That means that the program will have to iterate over all the saved logs and export them.

The advantage of this method is that it does not consume any CPU power during your session. The disadvantage is that this process can take a long time depending of the quantity of data you want to save.

Create the log string over time during of the logging process

In this case, the log string will be exported over time during the logging process. This will result in a significantly faster saving time but this may have an impact on the performance of the session.

NOTE: If you want to use the log string over time feature, you will not be able to add new headers while logging (see header definition). If you do so anyway, the feature will be disabled and the string will be exported normally at the end of the process.

Specify target directory

By default, logs will be saved in "My documents". You can specify the path and the prefix of the file by editing those values. The generated logs file will be called yyyy_MM_dd_HH_mm_ss_ffff_label.csv.