Example #1
0
        protected virtual FileStreamWriter GetFileStream(EventHandler <FileLoggerCallbackArgs> callback)
        {
            var wrapper = !FilePath.IsNullOrEmpty() && FileStreams.ContainsKey(FilePath)
                                ? FileStreams[FilePath] as FileStreamWriter
                                : null;

            if (wrapper != null)
            {
                if (wrapper.FileStream.Length < Settings.MaxFileSize)
                {
                    wrapper.SaveToFile(Settings.LineDelimiter);
                    return(wrapper);
                }
                FileStreams.Remove(wrapper.FilePath);
                wrapper.Close();
            }

            FilePath = GetNewFilePath();
            wrapper  = new FileStreamWriter(FilePath, callback)
            {
                IsFileCompressionEnabled = Settings.UseGzipCompression,
                GzipFileExt = Settings.GzipFileExtension
            };
            wrapper.Callback += OnFileStreamAction;
            FileStreams.Add(FilePath, wrapper);

            if (!Settings.OpeningLine.IsNullOrEmpty())
            {
                wrapper.SaveToFile(Settings.OpeningLine);
            }
            return(wrapper);
        }
Example #2
0
 protected virtual void Dispose(FileStreamWrapperBase wrapper)
 {
     wrapper.Callback -= OnFileStreamAction;
     if (FileStreams.ContainsKey(wrapper.FilePath))
     {
         FileStreams.Remove(wrapper.FilePath);
     }
     wrapper.Dispose();
 }
Example #3
0
        public void LoadFromFile(string filePath, EventHandler <FileLoggerCallbackArgs> callback)
        {
            try
            {
                if (!Settings.IsEnabled)
                {
                    const string errMsg =
                        "reading from log file is aborted. Logger is disabled.\r\nTo enable logger set 'Settings.IsEnabled = true'.";
#if UNITY_3D || UNITY3D
                    UnityEngine.Debug.LogWarning(errMsg);
#else
                    Debug.WriteLine(errMsg);
#endif
                    return;
                }

                ArgumentValidator.AssertNotNullOrEmpty(filePath, "filePath");

                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException(filePath);
                }
                if (FileStreams.ContainsKey(filePath))
                {
                    throw new OperationCanceledException("File with path '" + filePath + "' is in process.");
                }

                var wrapper = new FileStreamReader(filePath, callback)
                {
                    IsFileCompressionEnabled = Settings.UseGzipCompression
                };
                wrapper.Callback += OnFileStreamAction;
                FileStreams.Add(filePath, wrapper);
                wrapper.LoadFromFile();
            }
            catch (Exception e)
            {
                FileStreamWrapperBase.InvokeFileActionEvent(callback, null, filePath, null, null, FileLoggerAction.Read,
                                                            FileLoggerResult.Error, e);

                if (callback == null)
                {
                    throw;
                }
            }
        }