private void InitTask() { this.dumpLogTask = ThreadWorker.Run( doWork, TimeSpan.FromSeconds(Config.Instance.WriteInterval).TotalMilliseconds, "XIVLog Worker", ThreadPriority.Lowest); ActGlobals.oFormActMain.OnLogLineRead -= this.OnLogLineRead; ActGlobals.oFormActMain.OnLogLineRead += this.OnLogLineRead; void doWork() { var isNeedsFlush = false; if (string.IsNullOrEmpty(Config.Instance.OutputDirectory) || LogQueue.IsEmpty) { Thread.Sleep(TimeSpan.FromSeconds(Config.Instance.WriteInterval)); return; } if ((DateTime.Now - this.lastFlushTimestamp).TotalSeconds >= Config.Instance.FlushInterval) { isNeedsFlush = true; } if (this.currentLogfileName != this.LogfileName) { if (this.writter != null) { if (this.writeBuffer.Length > 0) { this.writter.Write(this.writeBuffer.ToString()); this.writeBuffer.Clear(); } this.writter.Flush(); this.writter.Close(); this.writter.Dispose(); } if (!Directory.Exists(Config.Instance.OutputDirectory)) { Directory.CreateDirectory(Config.Instance.OutputDirectory); } this.writter = new StreamWriter( new FileStream( this.LogfileName, FileMode.Append, FileAccess.Write, FileShare.Read), new UTF8Encoding(false)); this.currentLogfileName = this.LogfileName; this.RaisePropertyChanged(nameof(this.LogfileName)); this.RaisePropertyChanged(nameof(this.LogfileNameWithoutParent)); } XIVLog.RefreshPCNameDictionary(); while (LogQueue.TryDequeue(out XIVLog xivlog)) { if (this.currentZoneName != xivlog.ZoneName) { this.currentZoneName = xivlog.ZoneName; this.wipeoutCounter = 1; this.fileNo++; isNeedsFlush = true; } if (xivlog.Log.Contains("wipeout") || xivlog.Log.Contains("の攻略を終了した。")) { this.wipeoutCounter++; this.fileNo++; isNeedsFlush = true; } this.writeBuffer.AppendLine(xivlog.ToCSVLine()); Thread.Yield(); } if (isNeedsFlush || this.isForceFlush || this.writeBuffer.Length > 5000) { this.writter.Write(this.writeBuffer.ToString()); this.writeBuffer.Clear(); if (isNeedsFlush || this.isForceFlush) { this.isForceFlush = false; this.lastFlushTimestamp = DateTime.Now; this.writter?.Flush(); } } } }
private void InitTask() { // FFXIV.Framework.config を読み込ませる lock (FFXIV.Framework.Config.ConfigBlocker) { _ = FFXIV.Framework.Config.Instance; } var config = Config.Instance; // WriteIntervalの初期値をマイグレーションする if (config.WriteInterval >= 30) { config.WriteInterval = Config.WriteIntervalDefault; } this.dumpLogTask = ThreadWorker.Run( doWork, TimeSpan.FromSeconds(config.WriteInterval).TotalMilliseconds, "XIVLog Worker", ThreadPriority.Lowest); ActGlobals.oFormActMain.OnLogLineRead -= this.OnLogLineRead; ActGlobals.oFormActMain.OnLogLineRead += this.OnLogLineRead; void doWork() { var isNeedsFlush = false; if (string.IsNullOrEmpty(config.OutputDirectory)) { Thread.Sleep(TimeSpan.FromSeconds(config.WriteInterval)); return; } if (LogQueue.IsEmpty) { if ((DateTime.Now - this.lastWroteTimestamp).TotalSeconds > 10) { this.lastWroteTimestamp = DateTime.MaxValue; isNeedsFlush = true; } else { if (!this.isForceFlush) { Thread.Sleep(TimeSpan.FromSeconds(config.WriteInterval)); return; } } } if ((DateTime.Now - this.lastFlushTimestamp).TotalSeconds >= config.FlushInterval) { isNeedsFlush = true; } if (this.currentLogfileName != this.LogfileName) { if (this.writter != null) { this.writter.Flush(); this.writter.Close(); this.writter.Dispose(); } if (!Directory.Exists(config.OutputDirectory)) { Directory.CreateDirectory(config.OutputDirectory); } this.writter = new StreamWriter( new FileStream( this.LogfileName, FileMode.Append, FileAccess.Write, FileShare.Read, 64 * 1024), new UTF8Encoding(config.WithBOM)); this.currentLogfileName = this.LogfileName; this.RaisePropertyChanged(nameof(this.LogfileName)); this.RaisePropertyChanged(nameof(this.LogfileNameWithoutParent)); } XIVLog.RefreshPCNameDictionary(); while (LogQueue.TryDequeue(out XIVLog xivlog)) { if (this.currentZoneName != xivlog.ZoneName) { this.currentZoneName = xivlog.ZoneName; this.wipeoutCounter = 1; this.fileNo++; isNeedsFlush = true; } if (StopLoggingKeywords.Any(x => xivlog.Log.Contains(x))) { this.wipeoutCounter++; this.fileNo++; isNeedsFlush = true; } // ログをParseする xivlog.Parse(); this.writter.WriteLine(xivlog.ToCSVLine()); this.lastWroteTimestamp = DateTime.Now; Thread.Yield(); } if (isNeedsFlush || this.isForceFlush) { if (isNeedsFlush || this.isForceFlush) { this.isForceFlush = false; this.lastFlushTimestamp = DateTime.Now; this.writter?.Flush(); } } } }