/// <summary> /// Makes sure that <c>loggerA</c> and <c>loggerB</c> have different colors assigned for their outputs to the Console buffer. /// </summary> /// <param name="loggerA">The logger a.</param> /// <param name="loggerB">The logger b.</param> /// <returns>true if the loggers have different colors, false if one of loggers is not in the current output or if otherwise failed to update color of <c>loggerB</c></returns> public virtual Boolean makeSureHaveDifferentColors(IConsoleControl loggerA, IConsoleControl loggerB) { if (!colorRegistar.ContainsKey(loggerA)) { return(false); } if (!colorRegistar.ContainsKey(loggerB)) { return(false); } if (colorRegistar[loggerA] != colorRegistar[loggerB]) { return(true); } Int32 limit = 10; while (colorRegistar[loggerA] == colorRegistar[loggerB]) { limit--; colorRegistar[loggerB] = colorSelector.next(1); if (limit < 0) { return(false); } } return(true); }
/// <summary> /// Sets as only output - removes all the rest /// </summary> /// <param name="loger">The loger.</param> public virtual void setAsOnlyOutput(IConsoleControl loger, String prefix = "") { colorRegistar.Clear(); prefixRegistar.Clear(); allowList.Clear(); setAsOutput(loger, prefix); }
/// <summary> /// Checks if this loger has output permission. Checks only the globaly defined list, not members of <see cref="IConsoleControl"/> /// </summary> /// <param name="loger">The loger.</param> /// <returns></returns> public virtual Boolean checkOutputPermission(IConsoleControl loger) { try { return(allowList.Contains(loger)); } catch (Exception ex) { Console.WriteLine("Error occured during permission check"); return(true); } }
/// <summary> /// Writes to console. /// </summary> /// <param name="message">The message.</param> /// <param name="loger">The loger.</param> /// <param name="breakline">if set to <c>true</c> [breakline].</param> /// <param name="altColor">Color of the alt.</param> public virtual void writeToConsole(String message, IConsoleControl loger, Boolean breakline, Int32 altColor = -1) { lock (writeToLogWritterLock) { if (!checkOutputPermission(loger)) { return; } try { if (loger != lastLogerToWrite) { Console.WriteLine("--"); } Console.ForegroundColor = colorRegistar[loger]; if (altColor > 0) { Console.ForegroundColor = colorSelector.getAlternative(Console.ForegroundColor, altColor); } if (breakline) { List <String> lines = preprocessMessage(message, loger); foreach (String ln in lines) { writeLine(ln, loger); //Thread.Sleep(1); //if (logFileWriteOn) logWritter.WriteLine(ln); } } else { writeLine(message, loger); } lastLogerToWrite = loger; Console.ForegroundColor = ConsoleColor.White; } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.White; } } }
#pragma warning disable CS1574 // XML comment has cref attribute 'log(string)' that could not be resolved /// <summary> /// Allows access to the console output bugger to the <c>logger</c> and sets its color and prefix. If you call this method on logger that was already set to output, it will result in changed color /// </summary> /// <param name="logger">The logger assigned to the console output</param> /// <param name="prefix">The prefix that will appear in front of each <see cref="IAceLogable.log(string)"/> call. It will update it if already registered prefix exist</param> /// <param name="preferedColor">If not <see cref="ConsoleColor.Black"/> - it will assign specified color to the logger. Otherwise, if <see cref="ConsoleColor.Black"/> is specified, it will assign automatically a color from the <see cref="colorSelector"/></param> public virtual void setAsOutput(IConsoleControl logger, String prefix = "", ConsoleColor preferedColor = ConsoleColor.Black) #pragma warning restore CS1574 // XML comment has cref attribute 'log(string)' that could not be resolved { lock (setAsOutputLock) { if (!colorRegistar.ContainsKey(logger)) { if (preferedColor != ConsoleColor.Black) { colorRegistar.Add(logger, preferedColor); } else { colorRegistar.Add(logger, colorSelector.next()); } } else { if (preferedColor != ConsoleColor.Black) { colorRegistar[logger] = preferedColor; } else { colorRegistar[logger] = colorSelector.next(); } } if (!allowList.Contains(logger)) { allowList.Add(logger); } if (!prefix.isNullOrEmpty()) { prefix = prefix.toWidthExact(8, " ") + ": "; } if (!prefixRegistar.ContainsKey(logger)) { prefixRegistar.Add(logger, prefix); } else { prefixRegistar[logger] = prefix; } logger.VAR_AllowInstanceToOutputToConsole = true; } }
/// <summary> /// Preprocesses the message. /// </summary> /// <param name="message">The message.</param> /// <param name="loger">The loger.</param> /// <returns></returns> protected virtual List <String> preprocessMessage(String message, IConsoleControl loger) { List <String> lines = new List <string>(); foreach (String line in message.breakLines(false)) { String li = line; //if (convertToDos) li = li.toDosCharacters(toDosCharactersMode.toCleanChars); if (!String.IsNullOrWhiteSpace(li)) { lines.Add(li.ensureStartsWith(prefixRegistar[loger])); } } return(lines); }
/// <summary> /// Removes this loger from output permission /// </summary> /// <param name="loger">The loger.</param> public virtual void removeFromOutput(IConsoleControl loger) { //writeToConsole(loger.GetType().Name + ": went off", loger, true); lock (removeFromOutputLock) { if (colorRegistar.ContainsKey(loger)) { colorRegistar.Remove(loger); //, colorSelector.next()); } if (allowList.Contains(loger)) { allowList.Remove(loger); loger.VAR_AllowInstanceToOutputToConsole = false; } } }
protected virtual void writeLine(String ln, IConsoleControl loger) { if (reg_highlight.IsMatch(ln)) { var mchs = reg_highlight.Matches(ln); var splits = reg_highlight.Split(ln); Int32 i = -1; foreach (String sp in splits) { Console.ForegroundColor = colorRegistar[loger]; if (i == 1) { Console.ForegroundColor = colorSelector.getAlternative(Console.ForegroundColor, i); } i = -i; //Thread.Sleep(1); //if (logFileWriteOn) logWritter.WriteLine(ln); if (sp == Enumerable.Last <string>(splits)) { Console.WriteLine(sp); } else { Console.Write(sp); } } } else { Console.WriteLine(ln); } if (logFileWriteOn) { logWritter.Write(ln); } }
public void AddControl(IConsoleControl control) { control.Initialize(main); ((List <IConsoleControl>) this.Controls).Add(control); }
/// <summary> /// Replaces the two loggers in the console output /// </summary> /// <param name="oldLogger">The old loger.</param> /// <param name="newLogger">The new loger.</param> public virtual void replaceOutput(IConsoleControl oldLogger, IConsoleControl newLogger) { removeFromOutput(oldLogger); setAsOutput(newLogger); }