Ejemplo n.º 1
0
        public static async Task <LogParseState> ParseLogAsync(ISource source, Func <Task> onProgressAsync, CancellationToken cancellationToken)
        {
            LogParseState result = null;

            try
            {
                var pipe         = new Pipe();
                var fillPipeTask = source.FillPipeAsync(pipe.Writer, cancellationToken);
                var readPipeTask = LogParser.ReadPipeAsync(pipe.Reader, cancellationToken);
                do
                {
                    await Task.WhenAny(readPipeTask, Task.Delay(5000, cancellationToken)).ConfigureAwait(false);

                    if (!readPipeTask.IsCompleted)
                    {
                        await onProgressAsync().ConfigureAwait(false);
                    }
                } while (!readPipeTask.IsCompleted && !cancellationToken.IsCancellationRequested);
                result = await readPipeTask.ConfigureAwait(false);

                await fillPipeTask.ConfigureAwait(false);

                result.TotalBytes = source.LogFileSize;

                if (result.FilterTriggers.Any())
                {
                    var(f, c) = result.FilterTriggers.Values.FirstOrDefault(ft => ft.filter.Actions.HasFlag(FilterAction.IssueWarning));
                    if (f == null)
                    {
                        (f, c) = result.FilterTriggers.Values.FirstOrDefault(ft => ft.filter.Actions.HasFlag(FilterAction.RemoveContent));
                    }
                    if (f == null)
                    {
                        (f, c) = result.FilterTriggers.Values.FirstOrDefault();
                    }
                    result.SelectedFilter        = f;
                    result.SelectedFilterContext = c;
                }
#if DEBUG
                Config.Log.Debug("~~~~~~~~~~~~~~~~~~~~");
                Config.Log.Debug("Extractor hit stats:");
                foreach (var stat in result.ExtractorHitStats.OrderByDescending(kvp => kvp.Value))
                {
                    if (stat.Value > 100000)
                    {
                        Config.Log.Fatal($"{stat.Value}: {stat.Key}");
                    }
                    else if (stat.Value > 10000)
                    {
                        Config.Log.Error($"{stat.Value}: {stat.Key}");
                    }
                    else if (stat.Value > 1000)
                    {
                        Config.Log.Warn($"{stat.Value}: {stat.Key}");
                    }
                    else if (stat.Value > 100)
                    {
                        Config.Log.Info($"{stat.Value}: {stat.Key}");
                    }
                    else
                    {
                        Config.Log.Debug($"{stat.Value}: {stat.Key}");
                    }
                }

                Config.Log.Debug("~~~~~~~~~~~~~~~~~~~~");
                Config.Log.Debug("Syscall stats:");
                int serialCount = result.Syscalls.Count, moduleCount = 0, functionCount = 0;
                foreach (var moduleStats in result.Syscalls.Values)
                {
                    moduleCount += moduleStats.Count;
                    foreach (var funcStats in moduleStats.Values)
                    {
                        functionCount += funcStats.Count;
                    }
                }
                Config.Log.Debug("Product keys: " + serialCount);
                Config.Log.Debug("Modules: " + moduleCount);
                Config.Log.Debug("Functions: " + functionCount);
                Config.Log.Debug("Saving syscall information...");
                var sw = Stopwatch.StartNew();
#endif
                await SyscallInfoProvider.SaveAsync(result.Syscalls).ConfigureAwait(false);

#if DEBUG
                Config.Log.Debug("Saving syscall information took " + sw.Elapsed);
#endif
            }
            catch (Exception e)
            {
                Config.Log.Error(e, "Log parsing failed");
            }
            return(result);
        }