// Find max numeric value in enum public static int GetMaxValueFromEnum <T>() { Array values = Enum.GetValues(typeof(T)); // Doesn't make sence to find max in empty enum MyDebug.AssertDebug(values.Length >= 1); int max = int.MinValue; Type underlyingType = Enum.GetUnderlyingType(typeof(T)); if (underlyingType == typeof(System.Byte)) { foreach (byte value in values) { if (value > max) { max = value; } } } else if (underlyingType == typeof(System.Int16)) { foreach (short value in values) { if (value > max) { max = value; } } } else if (underlyingType == typeof(System.UInt16)) { foreach (ushort value in values) { if (value > max) { max = value; } } } else if (underlyingType == typeof(System.Int32)) { foreach (int value in values) { if (value > max) { max = value; } } } else { // Unhandled underlying type - probably "long" throw new InvalidBranchException(); } return(max); }
// Get actual ident for specified thread. If not specified yet, we assume it's zero. int GetIdentByThread(int threadId) { int retVal; if (m_indentsByThread.TryGetValue(threadId, out retVal) == false) { retVal = 0; } // If retVal is negative, then someone used wrong order of increase ident and decrease ident // E.g. used MyMwcLog.Default.DecreaseIndent(); at the start of a method whereas there should be MyMwcLog.Default.IncreaseIndent(); MyDebug.AssertDebug(retVal >= 0); return(retVal); }
public void DecreaseIndent() { if (m_enabled == false) { return; } MyLogIndentValue indentValue; lock (m_lock) { if (m_enabled == false) { return; } int threadId = GetThreadId(); MyLogIndentKey indentKey = new MyLogIndentKey(threadId, GetIdentByThread(threadId)); // If this fails, then order of IncreaseIndent/DecreaseIndent was wrong, or duplicate, etc MyDebug.AssertDebug(m_indents.ContainsKey(indentKey)); indentValue = m_indents[indentKey]; if (LogForMemoryProfiler) { MyMemoryLogs.MyMemoryEvent memEvent = new MyMemoryLogs.MyMemoryEvent(); memEvent.DeltaTime = (float)(DateTimeOffset.Now - indentValue.LastDateTimeOffset).TotalMilliseconds / 1000.0f; memEvent.ManagedEndSize = GetManagedMemory(); memEvent.ProcessEndSize = GetSystemMemory(); memEvent.ManagedStartSize = indentValue.LastGcTotalMemory; memEvent.ProcessStartSize = indentValue.LastWorkingSet; MyMemoryLogs.EndEvent(memEvent); } } lock (m_lock) { int threadId = GetThreadId(); m_indentsByThread[threadId] = GetIdentByThread(threadId) - 1; } }