/* * Arrays */ private static void Log <T>(Redwood.RedwoodChannels channels, string description, T[] array) { Redwood.StartTrack(description); if (array == null) { channels.Log("(array is null)"); } else { if (array.Length == 0) { channels.Log("(empty)"); } else { int index = 0; foreach (T item in array) { if (Dispatchable(item)) { Log(channels, "Index " + index, item); } else { channels.Logf("Index %d: %s", index, item); } index++; } } } Redwood.EndTrack(description); }
/* * Iterables (includes Collection, List, Set, etc.) */ private static void Log <T>(Redwood.RedwoodChannels channels, string description, IEnumerable <T> iterable) { Redwood.StartTrack(description); if (iterable == null) { channels.Log("(iterable is null)"); } else { int index = 0; foreach (T item in iterable) { if (Dispatchable(item) && item != iterable) { Log(channels, "Index " + index, item); } else { channels.Logf("Index %d: %s", index, item == iterable ? "...<infinite loop>" : item); } index++; } if (index == 0) { channels.Log("(empty)"); } } Redwood.EndTrack(description); }
/// <summary>Simple test case.</summary> public static void Main(string[] args) { if (args.Length > 0 && args[0].Equals("redwood")) { Redwood.Log(Redwood.Dbg, "at the top"); Redwood.StartTrack("Adaptor test controlled by redwood"); Logger topLogger = Logger.GetLogger(Logger.GlobalLoggerName); topLogger.Warning("I'm warning you!"); topLogger.Severe("Now I'm using my severe voice."); topLogger.Info("FYI"); Redwood.Log(Redwood.Dbg, "adapting"); JavaUtilLoggingAdaptor.Adapt(); topLogger.Warning("I'm warning you in Redwood!"); JavaUtilLoggingAdaptor.Adapt(); // should be safe to call this twice topLogger.Severe("Now I'm using my severe voice in Redwood!"); topLogger.Info("FYI: Redwood rocks"); // make sure original java.util.logging levels are respected topLogger.SetLevel(Level.Off); topLogger.Severe("We shouldn't see this message."); Redwood.Log(Redwood.Dbg, "at the bottom"); Redwood.EndTrack("Adaptor test controlled by redwood"); } else { // Reverse mapping Logger topLogger = Logger.GetLogger(Logger.GlobalLoggerName); // Can be Logger.getGlobal() in jdk1.7 // topLogger.addHandler(new ConsoleHandler()); Logger logger = Logger.GetLogger(typeof(JavaUtilLoggingAdaptor).FullName); topLogger.Info("Starting test"); logger.Log(Level.Info, "Hello from the class logger"); Redwood.Log("Hello from Redwood!"); Redwood.RootHandler().AddChild(RedirectOutputHandler.FromJavaUtilLogging(topLogger)); Redwood.Log("Hello from Redwood -> Java!"); Redwood.Log("Hello from Redwood -> Java again!"); logger.Log(Level.Info, "Hello again from the class logger"); Redwood.StartTrack("a track"); Redwood.Log("Inside a track"); logger.Log(Level.Info, "Hello a third time from the class logger"); Redwood.EndTrack("a track"); logger.Log(Level.Info, "Hello a fourth time from the class logger"); } }
// TODO: make prettyLog work in the situation of loops in the object graph /// <summary><inheritDoc/></summary> public virtual void PrettyLog(Redwood.RedwoodChannels channels, string description) { Redwood.StartTrack(description); // sort keys by class name IList <Type> sortedKeys = new List <Type>(this.KeySet()); sortedKeys.Sort(IComparer.Comparing(null)); // log key/value pairs foreach (Type key in sortedKeys) { string keyName = key.GetCanonicalName().Replace("class ", string.Empty); object value = this.Get(key); if (PrettyLogger.Dispatchable(value)) { PrettyLogger.Log(channels, keyName, value); } else { channels.Logf("%s = %s", keyName, value); } } Redwood.EndTrack(description); }
/* * Mappings */ private static void Log <K, V>(Redwood.RedwoodChannels channels, string description, IDictionary <K, V> mapping) { Redwood.StartTrack(description); if (mapping == null) { channels.Log("(mapping is null)"); } else { if (mapping.IsEmpty()) { channels.Log("(empty)"); } else { // convert keys to sorted list, if possible IList <K> keys = new LinkedList <K>(); foreach (K key in mapping.Keys) { keys.Add(key); } keys.Sort(null); // log key/value pairs int entryCounter = 0; foreach (K key_1 in keys) { V value = mapping[key_1]; if (!Dispatchable(key_1) && Dispatchable(value)) { Log(channels, key_1.ToString(), value); } else { if (Dispatchable(key_1) || Dispatchable(value)) { Redwood.StartTrack("Entry " + entryCounter); if (Dispatchable(key_1)) { Log(channels, "Key", key_1); } else { channels.Logf("Key %s", key_1); } if (Dispatchable(value)) { Log(channels, "Value", value); } else { channels.Logf("Value %s", value); } Redwood.EndTrack("Entry " + entryCounter); } else { channels.Logf("%s = %s", key_1, value); } } entryCounter++; } } } Redwood.EndTrack(description); }