public static bool ParseOpts(string[] args, out Opts opts) { opts = null; bool showhelp = false; Opts tmpOpts = new Opts() { }; var cmdOpts = new BeeOptsBuilder() .Add('e', "err", OPTTYPE.VALUE, "filename for error messages", o => tmpOpts.errorFilename = o) .Add('v', "verbose", OPTTYPE.BOOL, "show some debug output", o => tmpOpts.verbose = true) .Add('h', "help", OPTTYPE.BOOL, "show help", o => showhelp = true) .GetOpts(); tmpOpts.Filenames = BeeOpts.Parse(args, cmdOpts, (string unknownOpt) => Console.Error.WriteLine($"unknow option [{unknownOpt}]")); if (showhelp) { Console.WriteLine( "\nusage: PXELogParser [OPTIONS] [Filenames...]" + "\n\nOptions:"); BeeOpts.PrintOptions(cmdOpts); return(false); } opts = tmpOpts; return(true); }
static int Main(string[] args) { Opts opts; if (!Opts.ParseOpts(args, out opts)) { return(99); } if (opts.Filenames.Count == 0) { opts.Filenames = new string[] { "-" }; } Regex one = new Regex( @"^(....-..-.. ..:..:..,...) - INFO - " + @"(Sent message to server for pxe request : (..:..:..:..:..:..)" + @"|SmsBootPackageIdReply recvd from server:.+?Attribute names and their values: Name: (IsAbort|BootPackageId) ,Value: (.+?);" + @".+?Name: MAC ,Value: (..:..:..:..:..:..);.+?Name: RemoteRvpIP ,Value: ([0-9\.]+);.+" + ")", RegexOptions.Compiled); Dictionary <string, DateTime> sessions = new Dictionary <string, DateTime>(comparer: StringComparer.OrdinalIgnoreCase); Action <string> OnErrorHandler; TextWriter errWriter = null; if (String.IsNullOrEmpty(opts.errorFilename)) { OnErrorHandler = null; } else { errWriter = new StreamWriter(new FileStream(opts.errorFilename, FileMode.Create, FileAccess.Write)); OnErrorHandler = errWriter.WriteLine; } using (errWriter) { foreach (string filename in opts.Filenames) { TextReader rdr; if ("-".Equals(filename)) { rdr = Console.In; } else { rdr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 4096, FileOptions.SequentialScan)); } using (rdr) { string line; while ((line = rdr.ReadLine()) != null) { Match m = one.Match(line); if (m.Success) { DateTime logTimestamp = ParseLogTime(m.Groups[1].Value); if (m.Groups[2].Value.StartsWith("Sent")) { HandleStart(ref sessions, startTime: logTimestamp, mac: m.Groups[3].Value, OnError: OnErrorHandler); } else { HandleEnd( ref sessions, endTime: logTimestamp, mac: m.Groups[6].Value, IP: m.Groups[7].Value, bootimage: "IsAbort".Equals(m.Groups[4].Value) ? "abort" : m.Groups[5].Value, OnError: OnErrorHandler); } } } } } } return(0); }