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); }
private void Start() { Debug.Log(Application.persistentDataPath + "/" + Configuration.Instance.GetAttemptId()); screenCapturePath = Manager.Instance.GetDirectoryFor(DataCapturePaths.ScreenCapture); // Data logger defaults to the same run directory as ScreenCapture dataLogger = new Unity.Simulation.Logger("DataCapture"); }
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_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); }
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); }
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)); }
// 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"); } }
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(); }
private void Update() { // Get total time sim elapsed simElapsed += Time.deltaTime; // Sim has hit duration, flush all data and quit application if (simElapsed >= duration && !quit) { dataLogger.Flushall(); quit = true; Debug.Log("Quitting..."); Unity.Simulation.Logger successLogger = new Unity.Simulation.Logger("_Success"); Application.Quit(); } // Capture Data if last time capture time was over 1 second ago if ((int)simElapsed - lastCapture >= captureInterval && !quit) { Capture(lastCapture); lastCapture = (int)simElapsed; } }
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; } }
public IEnumerator CaptureTest_CaptureLogAlignment() { const int NumFramesToCapture = 10; const double RequestTimeoutInSeconds = NumFramesToCapture * 10.0; Assert.Greater( NumFramesToCapture, 0, $"The number of frames to capture is set to {NumFramesToCapture}, but it must be greater than 0" ); string screenCapturePath = Manager.Instance.GetDirectoryFor(DataCapturePaths.ScreenCapture); var camera = SetupCameraTestWithMaterial(8, GraphicsFormat.R8G8B8A8_UNorm, new Vector3(0.0f, 0.0f, 500.5f)); var logger = new Unity.Simulation.Logger("DataCapture", suffixOption: LoggerSuffixOption.TIME_STAMP); var wrappers = new List <AsyncRequestWrapper>(); var timer = new Stopwatch(); yield return(null); timer.Start(); Debug.Log($"Rendering and capturing {NumFramesToCapture} frames ..."); // Render the frame and capture them. for (int i = 0; i < NumFramesToCapture; ++i) { wrappers.Add(CaptureFrameWithLog(camera, logger, screenCapturePath, "image", i)); camera.Render(); yield return(null); } Debug.Log("Waiting for captures to complete ..."); bool allCompleted = false; // Wait for the tasks to complete. while (timer.Elapsed.TotalSeconds < RequestTimeoutInSeconds) { // This has to a manual polling loop so we can yield, allowing async requests to complete. // Attempting to block the thread here will prevent the requests from progressing. yield return(null); // The default assumption is that all requests have completed. allCompleted = true; foreach (var wrapper in wrappers) { if (!wrapper.Request.completed) { // We don't need to check the remainder of the requests if we // encounter one that has not finished running. allCompleted = false; break; } } if (allCompleted) { // All requests have been completed, so we can proceed. break; } } Assert.IsTrue(allCompleted, "Timed out waiting for capture requests to complete"); Debug.Log($"Time (in seconds) to complete all {NumFramesToCapture} capture requests: {timer.Elapsed.TotalSeconds}"); // Check for errors that may have occured during the capture requests. foreach (var wrapper in wrappers) { Assert.IsFalse(wrapper.Request.error, $"Frame capture request ({wrapper.FrameIndex}) encountered an error"); yield return(null); } // Flush the log to disk. logger.Flushall(); // This yield must be here so the log is actually written to disk. yield return(null); // Read the full contents of the log file. string[] fileLines = File.ReadAllLines(logger.GetPath()); int expectedImageIndex = 0; Debug.Log($"Validating log entries ..."); // Inspect the lines of the log to make sure each one matches the expected image file. foreach (string line in fileLines) { // Decode the json, then extract the image index that was logged for this line. var data = JsonUtility.FromJson <CaptureFrameWithLogDataPoint>(line); int imageIndex = int.Parse(data.imageName.Split('_')[1]); Assert.AreEqual( expectedImageIndex, imageIndex, $"Capture log line ({expectedImageIndex}) references incorrect image: {data.imageName}" ); ++expectedImageIndex; yield return(null); } Assert.AreEqual( NumFramesToCapture, expectedImageIndex, $"Log contained {expectedImageIndex} lines, but expected: {NumFramesToCapture}" ); }