Exemple #1
0
        /// <summary>
        /// Initialises this instance.
        /// </summary>
        private void EnsureInitialised()
        {
            if (!_inited)
            {
                // get flight program
                flightProgramScript = PartScript.GetModifier <FlightProgramScript>();
                if (flightProgramScript == null)
                {
                    Debug.LogError("Logger script has no flight program: deactivating");
                    enabled = false;
                    return;
                }

                server = new LoggerServer(Data.Hostname, Data.Port);

                _inited = true;
            }
        }
        /// <summary>
        /// Initialises this instance.
        /// </summary>
        private Boolean EnsureInitialized()
        {
            if (!this.initialized)
            {
                Debug.Log($"Initializing {nameof(VariableLoggerScript)} {Version} on craft {this.PartScript.CraftScript.CraftNode.Name}");

                // get flight program
                this.flightProgramScript = this.PartScript.GetModifier <FlightProgramScript>();
                if (this.flightProgramScript == null)
                {
                    Debug.LogError("Logger script has no flight program: deactivating");
                    this.enabled = false;
                }
                else
                {
                    var process = (Process)_processField.GetValue(this.flightProgramScript);

                    // Inject tracing log service.
                    var tracer = new TracingLogService(this, process.LogService);
                    // Set the default ILogService for new threads
                    _logServiceProperty.SetValue(process, tracer);
                    // Update all existing threads
                    foreach (var thread in process.Threads)
                    {
                        thread.Context.Log = tracer;
                    }

                    if (!String.IsNullOrEmpty(this.Data.Hostname))
                    {
                        Debug.Log($"Logging Data To udp://{this.Data.Hostname}:{this.Data.Port}");
                        this.server = new UdpServer(this.Data.Hostname, this.Data.Port);
                    }

                    if (!String.IsNullOrEmpty(this.Data.Path))
                    {
                        var absPath = Path.Combine(Application.persistentDataPath, this.Data.Path);

                        var dir  = Path.GetDirectoryName(absPath);
                        var file = Path.GetFileNameWithoutExtension(absPath);
                        var ext  = Path.GetExtension(absPath);

                        if (String.IsNullOrEmpty(file) && !String.IsNullOrEmpty(ext))
                        {
                            file = ext;
                            ext  = "";
                        }

                        if (String.IsNullOrEmpty(file))
                        {
                            Debug.LogError($"The path specified does not include a filename: '{absPath}'");
                        }
                        else if (!Directory.Exists(Path.GetDirectoryName(dir)))
                        {
                            Debug.LogError($"The path specified is within a directory does not exists: '{absPath}'");
                        }
                        else
                        {
                            var actualPath = Path.Combine(dir, $"{file}-{this.PartScript.CraftScript.CraftNode.NodeId}{ext}");
                            Debug.Log($"Logging Data To '{actualPath}'");
                            this.logFileWriter = new StreamWriter(actualPath, append: true);
                        }
                    }

                    // Convert from frequency in milliseconds to frequency in seconds
                    this.frequency = this.Data.Frequency / 1000f;

                    this.initialized = true;
                    Debug.Log($"{nameof(VariableLoggerScript)} successfully initialized.");
                }
            }

            return(this.initialized);
        }