public IEnumerator ProducerBuffer_TrimsEmptySpaces_IfPresentBeforeFlush()
    {
        string path     = Path.Combine(Configuration.Instance.GetStoragePath(), "Logs", "log_0.txt");
        var    inputLog = new TestLog()
        {
            msg = "Test"
        };
        var logger = new Logger("log.txt", 20);

        logger.Log(new TestLog()
        {
            msg = "Test"
        });
        logger.Log(new TestLog()
        {
            msg = "UnityTest"
        });
        while (!System.IO.File.Exists(path))
        {
            yield return(null);
        }
        var fileInfo = new FileInfo(path);

        Assert.AreEqual(JsonUtility.ToJson(inputLog).Length + 1, fileInfo.Length);
    }
    public IEnumerator DataLogger_FlushesToFileSystemOnlyWithMaxBufferSize()
    {
        var logger = new Logger("SimLog", bufferSize: 65536, maxElapsedSeconds: -1);

        logger.Log(new TestLog()
        {
            msg = "Test Simulation Log!"
        });
        yield return(new WaitForSeconds(5));

        var path = Path.Combine(Configuration.Instance.GetStoragePath(), "Logs", "SimLog_0.txt");

        Assert.IsTrue(!File.Exists(path));
        for (int i = 0; i < 100; i++)
        {
            logger.Log(new TestLog()
            {
                msg = "Test Simulation Log"
            });
        }
        logger.Flushall(true);
        Assert.IsTrue(File.Exists(path));

        var fileContents = File.ReadAllLines(path);

        Assert.AreEqual(fileContents.Length, 101);
    }
    public IEnumerator DataLogger_CustomFilenameSuffix()
    {
        var logger = new Logger("testLog", bufferSize: 10, maxElapsedSeconds: -1, customSuffix: () => DateTime.Now.ToString("yy-MM-dd"));
        var path   = Path.Combine(Configuration.Instance.GetStoragePath(), "Logs");

        logger.Log(new TestLog()
        {
            msg = "This is a test log with timestamp and seq"
        });
        logger.Log(new TestLog()
        {
            msg = "This is a another test log"
        });
        logger.Flushall(true);
        var files = Directory.GetFiles(path);

        Assert.IsTrue(files.Length > 0, "No Logs created");
        int start = 0;

        foreach (var file in files)
        {
            var fn = Path.GetFileName(file).Split('_');
            Assert.IsTrue(fn.Length == 3, "Invalid file name: " + file);
            var      timestamp = fn[1];
            var      seq       = fn[2].Split('.')[0];
            DateTime dt;
            Assert.IsTrue(DateTime.TryParseExact(timestamp, "yy-MM-dd",
                                                 CultureInfo.InvariantCulture,
                                                 DateTimeStyles.None, out dt), "Not a valid Data: " + timestamp);
            Assert.IsTrue(start++ == Int32.Parse(seq));
        }
        yield return(null);
    }
    private void Capture(int num)
    {
        string imageName = _camera.name + "_" + num;

        Dictionary <string, int> labelInstances = new Dictionary <string, int>();

        // Call Screen Capture
        var screen = CaptureCamera.Capture(_camera, request =>
        {
            string path = screenCapturePath + "/" + imageName + ".jpg";

            // Convert the screen capture to a byte array
            Array image = CaptureImageEncoder.Encode(request.data.colorBuffer as Array, 640, 480, GraphicsFormat.R8G8B8A8_UNorm,
                                                     CaptureImageEncoder.ImageFormat.Jpg, true);

            // Write the screen capture to a file
            var result = FileProducer.Write(path, image);

            // Wait for Async screen capture request to return and then log data point
            if (result)
            {
                labelInstances.Add("Cube", 100);
                labelInstances.Add("Sphere", 111);
                labelInstances.Add("Cylinder", 131);
                string temp = JsonConvert.SerializeObject(labelInstances);
                InstanceCount instanceCount = new InstanceCount(imageName, temp);
                // Log data point to file
                dataLogger.Log(instanceCount);

                return(AsyncRequest.Result.Completed);
            }

            return(AsyncRequest.Result.Error);
        });
    }
    public IEnumerator DataLogger_TimestampSuffix()
    {
        var logger = new Logger("testLog", maxElapsedSeconds: -1, suffixOption: LoggerSuffixOption.TIME_STAMP);
        var path   = Path.Combine(Configuration.Instance.GetStoragePath(), "Logs");

        logger.Log(new TestLog()
        {
            msg = "This is a test log with timestamp"
        });
        logger.Flushall(true);
        var files = Directory.GetFiles(path);

        Assert.IsTrue(files.Length > 0, "No Logs created");
        foreach (var file in files)
        {
            var fn        = Path.GetFileName(file).Split('_')[1];
            var timestamp = fn.Split('.')[0];
            Assert.IsTrue(timestamp.Length > 0, "Invalid file name: " + file);
            DateTime dt;
            Assert.IsTrue(DateTime.TryParseExact(timestamp, "yyyy-MM-ddThh-mm-ss",
                                                 CultureInfo.InvariantCulture,
                                                 DateTimeStyles.None, out dt), "Not a valid Data: " + timestamp);
        }
        yield return(null);
    }
示例#6
0
    private void Capture(int num)
    {
        string imageName = _camera.name + "_" + num;

        // Define Data point object outside async call
        DataPoint dataPoint = new DataPoint(_cube.name, _cube.transform.rotation, simElapsed, imageName);

        // Call Screen Capture
        var screen = CaptureCamera.Capture(_camera, request =>
        {
            string path = screenCapturePath + "/" + imageName + ".jpg";

            // Convert the screen capture to a byte array
            Array image = CaptureImageEncoder.Encode(request.data.colorBuffer as Array, 640, 480, GraphicsFormat.R8G8B8A8_UNorm,
                                                     CaptureImageEncoder.ImageFormat.Jpg, true);

            // Write the screen capture to a file
            var result = FileProducer.Write(path, image);

            // Wait for Async screen capture request to return and then log data point
            if (result)
            {
                // Log data point to file
                dataLogger.Log(dataPoint);

                return(AsyncRequest.Result.Completed);
            }

            return(AsyncRequest.Result.Error);
        });
    }
示例#7
0
    private AsyncRequestWrapper CaptureFrameWithLog(
        Camera camera,
        Unity.Simulation.Logger logger,
        string screenCapturePath,
        string frameFileNameRoot,
        int frameIndex
        )
    {
        // Construct the output file name for the image.
        string frameFileBaseName = $"{frameFileNameRoot}_{frameIndex}";
        string frameFilePath     = $"{screenCapturePath}{Path.DirectorySeparatorChar}{frameFileBaseName}.jpg";

        void LogData()
        {
            logger.Log(new CaptureFrameWithLogDataPoint(frameFileBaseName));
        }

        // Write the frame entry to the log. Write the log line outside the request callback when the
        // execution context is threaded since threaded requests will be executed asynchronously.
        if (IsExecutionContextThreaded())
        {
            LogData();
        }

        var req = CaptureCamera.Capture(
            camera,
            request =>
        {
            // Write the frame entry to the log. We can write the log line within the request callback when
            // the execution context is *not* threaded since they will be executed sequentially.
            if (!IsExecutionContextThreaded())
            {
                LogData();
            }

            // Get the color buffer data and convert it to an image file.
            byte[] imgColorData = (byte[])request.data.colorBuffer;
            byte[] imgFileData  = (byte[])CaptureImageEncoder.EncodeArray(
                imgColorData,
                32,
                32,
                GraphicsFormat.R8G8B8A8_UNorm,
                CaptureImageEncoder.ImageFormat.Jpg
                );

            // Attempt to write the image file to disk.
            bool fileWritten = FileProducer.Write(frameFilePath, imgFileData);
            return((fileWritten) ? AsyncRequest.Result.Completed : AsyncRequest.Result.Error);
        },
            flipY: false
            );

        return(new AsyncRequestWrapper(req, frameIndex));
    }
    /*
     *  Instantiates copies of Cube,
     *  moves them to a new location in the scene,
     *  and creates and logs dataPoint object.
     */
    public void ReplicateCube(GameObject cube, int replicateNum)
    {
        for (int i = 0; i < replicateNum; i++)
        {
            Vector3    cubePosition = new Vector3((i + 1) * 2.0F, 0, 0);
            GameObject newCube      = Instantiate(cube, cubePosition, Quaternion.identity);
            newCube.name = newCube.name + "_" + i;

            //Create a new data point
            ObjectPosition cubeDataPoint = new ObjectPosition(cubePosition, newCube.name);
            cubeLogger.Log(cubeDataPoint);
        }
    }
示例#9
0
    // Start is called before the first frame update
    void Start()
    {
        objDict = new Dictionary <string, GameObject>();
        objDict.Add("cube", CubePrefab);
        objDict.Add("sphere", SpherePrefab);

        // Create a specific logger for AppParams for debugging purposes
        paramLogger       = new Unity.Simulation.Logger("ParamReader");
        cubeLogger        = new Unity.Simulation.Logger("CubeLogger");
        simElapsedSeconds = 0;

        // NOTE: AppParams can be loaded anytime except during `RuntimeInitializeLoadType.BeforeSceneLoad`
        // If the simulation is running locally load app_param_0.json
        if (!Configuration.Instance.IsSimulationRunningInCloud())
        {
            string app_param_name = AppParamToggle ? "mountain_app_param_1.json" : "mountain_app_param_0.json";
            Configuration.Instance.SimulationConfig.app_param_uri =
                "file://" + Application.dataPath + "/StreamingAssets/" + app_param_name;
            Debug.Log(Configuration.Instance.SimulationConfig.app_param_uri);
        }

        appParams = Configuration.Instance.GetAppParams <MountainParam>();

        // Check if AppParam file was passed during command line execution
        if (appParams != null)
        {
            // Log AppParams to Player.Log file and Editor Console
            Debug.Log(appParams.ToString());

            // Log AppParams to DataLogger
            paramLogger.Log(appParams);
            paramLogger.Flushall();

            // Update the screen capture interval through an app-param
            float screenCaptureInterval = Mathf.Min(Mathf.Max(0, appParams.screenCaptureInterval), 100.0f);
            GameObject.FindGameObjectsWithTag("DataCapture")[0].GetComponent <CameraGrab>()._screenCaptureInterval =
                screenCaptureInterval;

            // Set the Simulation exit time.
            quitAfterSeconds = appParams.quitAfterSeconds;
            red          = appParams.red;
            green        = appParams.green;
            blue         = appParams.blue;
            whichObjects = appParams.whichObjects;
        }
        else
        {
            Debug.Log("NULL");
        }
    }
示例#10
0
    /*
     *  Instantiates copies of Cube,
     *  moves them to a new location in the scene,
     *  and creates and logs dataPoint object.
     */
    public void ReplicateCube(int cubeNum)
    {
        GameObject newCube = GameObject.Instantiate(objDict[appParams.whichObjects]);

        newCube.transform.position = new Vector3(Random.Range(-0.0f, 1.0f), 5.71f, -5.7f);
        newCube.name = newCube.name + "_" + cubeNum;
        newCube.GetComponent <Renderer>().material.color = new Color32((Byte)red, (Byte)green, (Byte)blue, 255);


        //Create a new data point
        ObjectPosition cubeDataPoint = new ObjectPosition(newCube.transform.position, newCube.name);

        cubeLogger.Log(cubeDataPoint);
    }
    public IEnumerator DataLogger_FlushesToFileSystem_WithElapsedTimeSet()
    {
        var logger = new Logger("TestLog.txt", maxElapsedSeconds: 2);

        logger.Log(new TestLog()
        {
            msg = "Test 123"
        });
        yield return(new WaitForSeconds(3));

        var path = Path.Combine(Configuration.Instance.GetStoragePath(), "Logs", "TestLog_0.txt");

        Assert.IsTrue(File.Exists(path));
    }
    // Create and Log a vector
    void Start()
    {
        // Print the location where data will be written
        Debug.Log(Application.persistentDataPath + "/" + Configuration.Instance.GetAttemptId());

        // Create new data logger with output files named DataCapture
        dataLogger = new Unity.Simulation.Logger("DataCapture");

        Vector3 examplePosition = new Vector3(0, 1, 2);

        //Create a new data point
        ObjectPosition objectPosition = new ObjectPosition(examplePosition, "ExampleObjectName");

        dataLogger.Log(objectPosition);

        // Flush written objectPosition to file
        dataLogger.Flushall();
    }
    void Start()
    {
        // Create a specific logger for AppParams for debugging purposes
        paramLogger       = new Unity.Simulation.Logger("ParamReader");
        cubeLogger        = new Unity.Simulation.Logger("CubeLogger");
        simElapsedSeconds = 0;

        // NOTE: AppParams can be loaded anytime except during `RuntimeInitializeLoadType.BeforeSceneLoad`
        // If the simulation is running locally load app_param_0.json
        if (!Configuration.Instance.IsSimulationRunningInCloud())
        {
            Configuration.Instance.SimulationConfig.app_param_uri = "file://" + Application.dataPath + "/StreamingAssets/app_param_0.json";
            Debug.Log(Configuration.Instance.SimulationConfig.app_param_uri);
        }

        appParams = Configuration.Instance.GetAppParams <CubeAppParam>();

        // Check if AppParam file was passed during command line execution
        if (appParams != null)
        {
            // Log AppParams to Player.Log file and Editor Console
            Debug.Log(appParams.ToString());

            // Log AppParams to DataLogger
            paramLogger.Log(appParams);
            paramLogger.Flushall();

            // Update the screen capture interval through an app-param
            float screenCaptureInterval = Mathf.Min(Mathf.Max(0, appParams.screenCaptureInterval), 100.0f);
            GameObject.FindGameObjectsWithTag("DataCapture")[0].GetComponent <CameraGrab>()._screenCaptureInterval = screenCaptureInterval;
            // ReplicateCube
            ReplicateCube(GameObject.FindGameObjectsWithTag("Cube")[0], appParams.replicateCube);

            // Set the Simulation exit time.
            quitAfterSeconds = appParams.quitAfterSeconds;
        }
    }