1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.IO;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
|
---|
7 | using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
|
---|
8 |
|
---|
9 | namespace OfficeOpenXml.FormulaParsing.Logging
|
---|
10 | {
|
---|
11 | internal class TextFileLogger : IFormulaParserLogger
|
---|
12 | {
|
---|
13 | private StreamWriter _sw;
|
---|
14 | private const string Separator = "=================================";
|
---|
15 | private int _count;
|
---|
16 | private DateTime _startTime = DateTime.Now;
|
---|
17 | private Dictionary<string, int> _funcs = new Dictionary<string, int>();
|
---|
18 | private Dictionary<string, long> _funcPerformance = new Dictionary<string, long>();
|
---|
19 | internal TextFileLogger(FileInfo fileInfo)
|
---|
20 | {
|
---|
21 | _sw = new StreamWriter(fileInfo.FullName);
|
---|
22 | }
|
---|
23 |
|
---|
24 | private void WriteSeparatorAndTimeStamp()
|
---|
25 | {
|
---|
26 | _sw.WriteLine(Separator);
|
---|
27 | _sw.WriteLine("Timestamp: {0}", DateTime.Now);
|
---|
28 | _sw.WriteLine();
|
---|
29 | }
|
---|
30 |
|
---|
31 | private void WriteAddressInfo(ParsingContext context)
|
---|
32 | {
|
---|
33 | if (context.Scopes.Current != null && context.Scopes.Current.Address != null)
|
---|
34 | {
|
---|
35 | _sw.WriteLine("Worksheet: {0}", context.Scopes.Current.Address.Worksheet ?? "<not specified>");
|
---|
36 | _sw.WriteLine("Address: {0}", context.Scopes.Current.Address.Address ?? "<not available>");
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void Log(ParsingContext context, Exception ex)
|
---|
41 | {
|
---|
42 | WriteSeparatorAndTimeStamp();
|
---|
43 | WriteAddressInfo(context);
|
---|
44 | _sw.WriteLine(ex);
|
---|
45 | _sw.WriteLine();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void Log(ParsingContext context, string message)
|
---|
49 | {
|
---|
50 | WriteSeparatorAndTimeStamp();
|
---|
51 | WriteAddressInfo(context);
|
---|
52 | _sw.WriteLine(message);
|
---|
53 | _sw.WriteLine();
|
---|
54 | }
|
---|
55 |
|
---|
56 | public void Log(string message)
|
---|
57 | {
|
---|
58 | WriteSeparatorAndTimeStamp();
|
---|
59 | _sw.WriteLine(message);
|
---|
60 | _sw.WriteLine();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public void LogCellCounted()
|
---|
64 | {
|
---|
65 | _count++;
|
---|
66 | if (_count%500 == 0)
|
---|
67 | {
|
---|
68 | _sw.WriteLine(Separator);
|
---|
69 | var timeEllapsed = DateTime.Now.Subtract(_startTime);
|
---|
70 | _sw.WriteLine("{0} cells parsed, time {1} seconds", _count, timeEllapsed.TotalSeconds);
|
---|
71 |
|
---|
72 | var funcs = _funcs.Keys.OrderByDescending(x => _funcs[x]).ToList();
|
---|
73 | foreach (var func in funcs)
|
---|
74 | {
|
---|
75 | _sw.Write(func + " - " + _funcs[func]);
|
---|
76 | if (_funcPerformance.ContainsKey(func))
|
---|
77 | {
|
---|
78 | _sw.Write(" - avg: " + _funcPerformance[func]/_funcs[func] + " milliseconds");
|
---|
79 | }
|
---|
80 | _sw.WriteLine();
|
---|
81 | }
|
---|
82 | _sw.WriteLine();
|
---|
83 | _funcs.Clear();
|
---|
84 |
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | public void LogFunction(string func)
|
---|
89 | {
|
---|
90 | if (!_funcs.ContainsKey(func))
|
---|
91 | {
|
---|
92 | _funcs.Add(func, 0);
|
---|
93 | }
|
---|
94 | _funcs[func]++;
|
---|
95 | }
|
---|
96 |
|
---|
97 | public void LogFunction(string func, long milliseconds)
|
---|
98 | {
|
---|
99 | if (!_funcPerformance.ContainsKey(func))
|
---|
100 | {
|
---|
101 | _funcPerformance[func] = 0;
|
---|
102 | }
|
---|
103 | _funcPerformance[func] += milliseconds;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public void Dispose()
|
---|
107 | {
|
---|
108 | _sw.Close();
|
---|
109 | _sw.Dispose();
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|