Esempio n. 1
0
 private void CubeGrid_OnBlockAdded(IMySlimBlock obj)
 {
     lock_blocks.AcquireExclusive();
     try { Add(obj); }
     catch (Exception e) { Log.AlwaysLog("Exception: " + e, Logger.severity.ERROR); }
     finally { lock_blocks.ReleaseExclusive(); }
 }
Esempio n. 2
0
        private void CubeGrid_OnBlockAdded(IMySlimBlock obj)
        {
            IMyTerminalBlock asTerm = obj.FatBlock as IMyTerminalBlock;

            if (asTerm == null)
            {
                return;                 // only track IMyTerminalBlock
            }
            lock_CubeBlocks.AcquireExclusive();
            try
            {
                MyObjectBuilderType myOBtype = asTerm.BlockDefinition.TypeId;
                string definition            = asTerm.DefinitionDisplayNameText;

                //log("adding " + termType + " : " + definition, "CubeGrid_OnBlockAdded()", Logger.severity.TRACE);

                ListSnapshots <Ingame.IMyTerminalBlock> setBlocks_Type;
                ListSnapshots <Ingame.IMyTerminalBlock> setBlocks_Def;
                if (!CubeBlocks_Type.TryGetValue(myOBtype, out setBlocks_Type))
                {
                    //log("new lists for " + asTerm.DefinitionDisplayNameText, "CubeGrid_OnBlockAdded()", Logger.severity.TRACE);
                    setBlocks_Type = new ListSnapshots <Ingame.IMyTerminalBlock>();
                    CubeBlocks_Type.Add(myOBtype, setBlocks_Type);
                }
                if (!CubeBlocks_Definition.TryGetValue(definition, out setBlocks_Def))
                {
                    setBlocks_Def = new ListSnapshots <Ingame.IMyTerminalBlock>();
                    CubeBlocks_Definition.Add(definition, setBlocks_Def);
                    addKnownDefinition(definition);
                }

                ////log("checking for dirty lists: " + asTerm.DefinitionDisplayNameText, "CubeGrid_OnBlockAdded()", Logger.severity.TRACE);
                //// replace dirty list
                //if (!setBlocks_Def.IsClean)
                //{
                //	setBlocks_Def = new ListCacher<Ingame.IMyTerminalBlock>(setBlocks_Def);
                //	CubeBlocks_Definition[definition] = setBlocks_Def;
                //}
                //if (!setBlocks_Type.IsClean)
                //{
                //	setBlocks_Type = new ListCacher<Ingame.IMyTerminalBlock>(setBlocks_Type);
                //	CubeBlocks_Type[myOBtype] = setBlocks_Type;
                //}

                //log("adding: " + asTerm.DefinitionDisplayNameText + ", termType = " + myOBtype, "CubeGrid_OnBlockAdded()", Logger.severity.TRACE);
                setBlocks_Type.mutable().Add(asTerm);
                setBlocks_Def.mutable().Add(asTerm);
            }
            catch (Exception e) { alwaysLog("Exception: " + e, "CubeGrid_OnBlockAdded()", Logger.severity.ERROR); }
            finally { lock_CubeBlocks.ReleaseExclusive(); }
        }
Esempio n. 3
0
        /// <summary>
        /// This should only ever be called from main thread.
        /// </summary>
        /// <returns>true if the exclusive lock was acquired, false if it is already held</returns>
        public static bool MainThread_TryAcquireExclusive()
        {
            if (ExclusiveHeld)
            {
                return(false);
            }

            Lock_MainThread.AcquireExclusive();
            ExclusiveHeld = true;
            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// For logging WARNING and higher severity.
        /// </summary>
        /// <param name="level">severity level</param>
        /// <param name="methodName">calling method</param>
        /// <param name="toLog">message to log</param>
        /// <param name="primaryState">class specific, appears before secondary state in log</param>
        /// <param name="secondaryState">class specific, appears before message in log</param>
        public void log(severity level, string methodName, string toLog, string primaryState = null, string secondaryState = null)
        {
            if (closed)
            {
                return;
            }
            lock_log.AcquireExclusive();
            try
            {
                if (logWriter == null)
                {
                    if (MyAPIGateway.Utilities == null || !createLog())
                    {
                        return;                         // cannot log
                    }
                }
                if (f_gridName != null)
                {
                    gridName = f_gridName.Invoke();
                }
                if (primaryState == null)
                {
                    if (f_state_primary != null)
                    {
                        default_primary = f_state_primary.Invoke();
                    }
                    primaryState = default_primary;
                }
                if (secondaryState == null)
                {
                    if (f_state_secondary != null)
                    {
                        default_secondary = f_state_secondary.Invoke();
                    }
                    secondaryState = default_secondary;
                }

                if (toLog == null)
                {
                    toLog = "no message";
                }
                if (numLines >= maxNumLines)
                {
                    return;
                }

                numLines++;
                appendWithBrackets(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff"));
                appendWithBrackets(level.ToString());
                appendWithBrackets(gridName);
                appendWithBrackets(className);
                appendWithBrackets(methodName);
                appendWithBrackets(primaryState);
                appendWithBrackets(secondaryState);
                stringCache.Append(toLog);

                logWriter.WriteLine(stringCache);
                logWriter.Flush();
                stringCache.Clear();
            }
            catch { }
            finally { lock_log.ReleaseExclusive(); }
        }
Esempio n. 5
0
 /// <summary>
 /// This should only ever be called from main thread.
 /// </summary>
 public static void MainThread_AcquireExclusive()
 {
     Lock_MainThread.AcquireExclusive();
 }
Esempio n. 6
0
 /// <summary>
 /// lock this object while performing safeAction
 /// </summary>
 /// <param name="safeAction"></param>
 public void Safe(Action safeAction)
 {
     Lock.AcquireExclusive();
     try { safeAction.Invoke(); }
     finally { Lock.ReleaseExclusive(); }
 }