private ASMAddLabelResult AddLabelGeneric(uint pc, string label) { ASMAddLabelResult result = new ASMAddLabelResult(); try { label = ASMStringHelper.RemoveSpaces(label).ToUpper(); if (!PersistentLabelDict.ContainsKey(label)) { LabelDict.Add(label, pc); if (label.StartsWith(PersistentLabelPrefix)) { PersistentLabelDict.Add(label, pc); } } else { PersistentLabelDict[label] = pc; LabelDict[label] = pc; } } catch { result.ErrorCode = 1; result.ErrorMessage = "Error adding label " + label + ": Label already exists!\r\n"; return(result); } result.ErrorCode = 0; result.PC = pc; return(result); }
public void ClearLabelDict() { LabelDict.Clear(); foreach (KeyValuePair <string, uint> pair in PersistentLabelDict) { LabelDict.Add(pair.Key, pair.Value); } }
private ParsedMetrics ConvertCsvLine(ICsvReaderRow line, LabelDict labels) { if (_readers.Count != line.Count) { throw new ParserError(); } var result = new ParsedMetrics(labels); foreach (var(reader, column) in _readers.Zip(line, (a, b) => new KeyValuePair <ColumnReader, string>(a, b))) { reader?.Invoke(result, column); } return(result); }
public uint LabelToUnsigned(string label, bool skipAssertion = false) { string newLabel = ASMStringHelper.RemoveSpaces(label).ToUpper(); string lowerLabel = newLabel.ToLower(); if (((lowerLabel.StartsWith("%hi(")) || (lowerLabel.StartsWith("%lo("))) && (lowerLabel.Substring(3).Contains(")"))) { string newValue = lowerLabel.Substring(4, lowerLabel.IndexOf(")") - 4).Trim(); uint uValue = GetAnyUnsignedValue(newValue, skipAssertion); return(lowerLabel.StartsWith("%hi(") ? ProcessHi(uValue) : ProcessLo(uValue)); } if (!skipAssertion) { ASMDebugHelper.assert(LabelDict.ContainsKey(newLabel), "Label not found: " + label); } return((uint)(LabelDict.ContainsKey(newLabel) ? LabelDict[newLabel] : 0)); }
//clean input and parse labels public List <string> GetCleanInput(out LabelDict dict) { List <string> rv = new List <string>(); string line; dict = new LabelDict(); using (var sr = new StreamReader(mFileName)) { while ((line = sr.ReadLine()) != null) { //remove empty lines if (String.IsNullOrWhiteSpace(line)) { continue; } //cleaning the line line = RemoveAfterSubstring(line, "#"); line = RemoveAfterSubstring(line, "//"); line = line.Trim(); line = line.ToLower(); //remove empty lines if (String.IsNullOrWhiteSpace(line)) { continue; } //is a label if (line.EndsWith(":")) { string label = line.Substring(0, line.Length - 1); dict[label] = new Label(label, rv.Count * 4); } else { rv.Add(line); } } } return(rv); }
public static void ParseStream(Stream stream, string environment, string target, IList <ColumnReader> readers, IDictionary <string, MetricBase> metrics, int msTimeout, CancellationToken cancellationToken) { if (string.IsNullOrEmpty(environment)) { throw new ArgumentException("Environment must not be empty", nameof(environment)); } var envDict = new LabelDict(environment); var envTargetDict = new LabelDict(environment); envTargetDict.Set("target", target); foreach (var entry in new LogParser(stream, readers, environment).ReadAll(msTimeout, cancellationToken)) { if (entry == null) { if (cancellationToken.IsCancellationRequested) { break; } MetricBase.ParserErrors.WithLabels(envDict).Add(1); MetricBase.ParserErrorsPerTarget.WithLabels(envTargetDict).Add(1); continue; } MetricBase.LinesParsed.WithLabels(entry.Labels).Add(1); var labels = new LabelDict(entry.Labels); labels.Set("target", target); MetricBase.LinesParsedPerTarget.WithLabels(labels).Add(1); foreach (var(name, amount) in entry.Metrics) { metrics[name].WithLabels(entry.Labels).Add(amount); } if (cancellationToken.IsCancellationRequested) { break; } } }
public ParseInstruction(List <string> lines, LabelDict labels) { int literal = 0; string instruction; foreach (string line in lines) { // Split line into two strings delimited by spaces // strs[0] = instruction, instr[1] = literal string[] strs = line.Split(' '); instruction = strs[0]; if (strs.Length == 1) { literal = 0; } else { try { literal = ParseInt(strs[strs.Length - 1]); } catch (FormatException) { try { literal = labels[strs[strs.Length - 1]].Address; // - instrList.Count * 4; } catch (InvalidLabelException) { } } } switch (instruction) { case "exit": curInstr = new Exit(literal); instrList.Add(curInstr.ByteCode); break; case "swap": curInstr = new Swap(); instrList.Add(curInstr.ByteCode); break; case "inpt": curInstr = new Inpt(); instrList.Add(curInstr.ByteCode); break; case "nop": curInstr = new Nop(); instrList.Add(curInstr.ByteCode); break; case "pop": curInstr = new Pop(); instrList.Add(curInstr.ByteCode); break; case "add": curInstr = new Add(); instrList.Add(curInstr.ByteCode); break; case "sub": curInstr = new Sub(); instrList.Add(curInstr.ByteCode); break; case "mul": curInstr = new Mul(); instrList.Add(curInstr.ByteCode); break; case "div": curInstr = new Div(); instrList.Add(curInstr.ByteCode); break; case "rem": curInstr = new Rem(); instrList.Add(curInstr.ByteCode); break; case "and": curInstr = new And(); instrList.Add(curInstr.ByteCode); break; case "or": curInstr = new Or(); instrList.Add(curInstr.ByteCode); break; case "xor": curInstr = new Xor(); instrList.Add(curInstr.ByteCode); break; case "neg": curInstr = new Neg(); instrList.Add(curInstr.ByteCode); break; case "not": curInstr = new Not(); instrList.Add(curInstr.ByteCode); break; case "goto": curInstr = new Goto(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "ifeq": curInstr = new Ifeq(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "ifne": curInstr = new Ifne(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "iflt": curInstr = new Iflt(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "ifgt": curInstr = new Ifgt(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "ifle": curInstr = new Ifle(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "ifge": curInstr = new Ifge(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "ifez": curInstr = new Ifez(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "ifnz": curInstr = new Ifnz(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "ifmi": curInstr = new Ifmi(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "ifpl": curInstr = new Ifpl(literal - instrList.Count * 4); instrList.Add(curInstr.ByteCode); break; case "dup": curInstr = new Dup(literal); instrList.Add(curInstr.ByteCode); break; case "print": curInstr = new Print(); instrList.Add(curInstr.ByteCode); break; case "dump": curInstr = new Dump(); instrList.Add(curInstr.ByteCode); break; case "push": curInstr = new Push(literal); instrList.Add(curInstr.ByteCode); break; default: break; } } }
public ParsedMetrics([NotNull] LabelDict labels) { Labels = new LabelDict(labels); }
private LogParser(Stream stream, IList <ColumnReader> readers, string environment) { _stream = stream; _readers = readers; _envLabel = new LabelDict(environment); }
public void Run() { Logger.Info($"Scraper thread for {_filename} on {_host} became alive"); var envHostDict = new LabelDict(_environment); envHostDict.Set("host", _host); var connected = MetricBase.Connected.WithLabels(envHostDict) as Gauge; Debug.Assert(connected != null); try { while (!CancellationTokenSource.IsCancellationRequested) { try { connected.Set(0); try { Logger.Info($"Trying to establish connection to {_host}"); using (var client = CreateClient()) { client.Connect(); connected.Set(1); Logger.Info($"Starting tailing {_filename} on {_host}"); var cmd = client.CreateCommand($"tail -n0 --follow=name \"{_filename}\" 2>/dev/null"); var tmp = cmd.BeginExecute(); ((PipeStream)cmd.OutputStream).BlockLastReadBuffer = true; try { LogParser.ParseStream(cmd.OutputStream, _environment, _host, _readers, _metrics, _readTimeoutMs, CancellationTokenSource.Token); cmd.EndExecute(tmp); if (cmd.ExitStatus != 0) { Logger.Warn($"Tail command failed with exit code {cmd.ExitStatus} on {_host}"); } else { Logger.Info($"Tail command finished successfully on {_host}"); } } catch (StreamStarvationException) { Logger.Warn($"SSH stream starvation for {_filename} on {_host}"); cmd.CancelAsync(); } } } catch (SshOperationTimeoutException ex) { Logger.Error($"Timeout on {_host}: {ex.Message}"); } catch (SshConnectionException ex) { Logger.Error($"Failed to connect to {_host}: {ex.Message}"); } catch (SshAuthenticationException ex) { Logger.Error($"Failed to authenticate for {_host}: {ex.Message}"); } catch (SocketException ex) { Logger.Error($"Error on socket for {_host} (check firewall?): {ex.Message}"); } catch (Exception ex) { Logger.Fatal(ex, $"Unhandled exception on {_host}: {ex.Message}"); } connected.Set(0); Logger.Info($"Will retry connecting to {_host} in 30 seconds"); if (CancellationTokenSource.Token.WaitHandle.WaitOne(TimeSpan.FromSeconds(30))) { break; } } finally { connected.Set(0); } } } finally { connected.Drop(); } }