Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ff4fcdb9 authored by Wink Saville's avatar Wink Saville
Browse files

Tweak logging and some code reformatting.

Add logAndAddLogRec method that logs a string and adds it to LogRecords.
Add loge(s, e)
Add missing javadoc to the logging methods
Reformatted the code.

Change-Id: I42f39e45aae77e5b6968f16b6b83f4804ccb7e73
parent c50536b8
Loading
Loading
Loading
Loading
+85 −46
Original line number Diff line number Diff line
@@ -520,7 +520,6 @@ public class StateMachine {
            return mDstState;
        }


        /**
         * @return the original state that received the message.
         */
@@ -656,8 +655,7 @@ public class StateMachine {
         *
         */
        synchronized void add(StateMachine sm, Message msg, String messageInfo, IState state,
                IState orgState,
                IState transToState) {
                IState orgState, IState transToState) {
            mCount += 1;
            if (mLogRecVector.size() < mMaxSize) {
                mLogRecVector.add(new LogRec(sm, msg, messageInfo, state, orgState, transToState));
@@ -672,7 +670,6 @@ public class StateMachine {
        }
    }


    private static class SmHandler extends Handler {

        /** The debug flag */
@@ -730,15 +727,13 @@ public class StateMachine {
             */
            @Override
            public String toString() {
                return "state=" + state.getName() + ",active=" + active
                        + ",parent=" + ((parentStateInfo == null) ?
                                        "null" : parentStateInfo.state.getName());
                return "state=" + state.getName() + ",active=" + active + ",parent="
                        + ((parentStateInfo == null) ? "null" : parentStateInfo.state.getName());
            }
        }

        /** The map of all of the states in the state machine */
        private HashMap<State, StateInfo> mStateInfo =
            new HashMap<State, StateInfo>();
        private HashMap<State, StateInfo> mStateInfo = new HashMap<State, StateInfo>();

        /** The initial state that will process the first message */
        private State mInitialState;
@@ -788,14 +783,14 @@ public class StateMachine {
            if (mIsConstructionCompleted) {
                /** Normal path */
                msgProcessedState = processMsg(msg);
            } else if (!mIsConstructionCompleted &&
                    (mMsg.what == SM_INIT_CMD) && (mMsg.obj == mSmHandlerObj)) {
            } else if (!mIsConstructionCompleted && (mMsg.what == SM_INIT_CMD)
                    && (mMsg.obj == mSmHandlerObj)) {
                /** Initial one time path. */
                mIsConstructionCompleted = true;
                invokeEnterMethods(0);
            } else {
                throw new RuntimeException("StateMachine.handleMessage: " +
                            "The start method not called, received msg: " + msg);
                throw new RuntimeException("StateMachine.handleMessage: "
                        + "The start method not called, received msg: " + msg);
            }
            performTransitions(msgProcessedState, msg);

@@ -830,8 +825,8 @@ public class StateMachine {
                }
            } else if (recordLogMsg) {
                /** Record message */
               mLogRecords.add(mSm, mMsg, mSm.getLogRecString(mMsg), msgProcessedState,
                        orgState, mDestState);
                mLogRecords.add(mSm, mMsg, mSm.getLogRecString(mMsg), msgProcessedState, orgState,
                        mDestState);
            }

            State destState = mDestState;
@@ -852,7 +847,6 @@ public class StateMachine {
                    int stateStackEnteringIndex = moveTempStateStackToStateStack();
                    invokeEnterMethods(stateStackEnteringIndex);


                    /**
                     * Since we have transitioned to a new state we need to have
                     * any deferred messages moved to the front of the message queue
@@ -988,8 +982,8 @@ public class StateMachine {
         * up to the common ancestor state.
         */
        private final void invokeExitMethods(StateInfo commonStateInfo) {
            while ((mStateStackTopIndex >= 0) &&
                    (mStateStack[mStateStackTopIndex] != commonStateInfo)) {
            while ((mStateStackTopIndex >= 0)
                    && (mStateStack[mStateStackTopIndex] != commonStateInfo)) {
                State curState = mStateStack[mStateStackTopIndex].state;
                if (mDbg) mSm.log("invokeExitMethods: " + curState.getName());
                curState.exit();
@@ -1047,9 +1041,9 @@ public class StateMachine {

            mStateStackTopIndex = j - 1;
            if (mDbg) {
                mSm.log("moveTempStackToStateStack: X mStateStackTop="
                      + mStateStackTopIndex + ",startingIndex=" + startingIndex
                      + ",Top=" + mStateStack[mStateStackTopIndex].state.getName());
                mSm.log("moveTempStackToStateStack: X mStateStackTop=" + mStateStackTopIndex
                        + ",startingIndex=" + startingIndex + ",Top="
                        + mStateStack[mStateStackTopIndex].state.getName());
            }
            return startingIndex;
        }
@@ -1091,8 +1085,7 @@ public class StateMachine {
         */
        private final void setupInitialStateStack() {
            if (mDbg) {
                mSm.log("setupInitialStateStack: E mInitialState="
                    + mInitialState.getName());
                mSm.log("setupInitialStateStack: E mInitialState=" + mInitialState.getName());
            }

            StateInfo curStateInfo = mStateInfo.get(mInitialState);
@@ -1132,8 +1125,8 @@ public class StateMachine {
         */
        private final StateInfo addState(State state, State parent) {
            if (mDbg) {
                mSm.log("addStateInternal: E state=" + state.getName()
                        + ",parent=" + ((parent == null) ? "" : parent.getName()));
                mSm.log("addStateInternal: E state=" + state.getName() + ",parent="
                        + ((parent == null) ? "" : parent.getName()));
            }
            StateInfo parentStateInfo = null;
            if (parent != null) {
@@ -1150,8 +1143,8 @@ public class StateMachine {
            }

            // Validate that we aren't adding the same state in two different hierarchies.
            if ((stateInfo.parentStateInfo != null) &&
                    (stateInfo.parentStateInfo != parentStateInfo)) {
            if ((stateInfo.parentStateInfo != null)
                    && (stateInfo.parentStateInfo != parentStateInfo)) {
                throw new RuntimeException("state already added");
            }
            stateInfo.state = state;
@@ -1509,8 +1502,7 @@ public class StateMachine {
     *
     * @return  A Message object from the global pool
     */
    public final Message obtainMessage()
    {
    public final Message obtainMessage() {
        return Message.obtain(mSmHandler);
    }

@@ -1542,8 +1534,7 @@ public class StateMachine {
     * @param obj is assigned to Message.obj.
     * @return  A Message object from the global pool
     */
    public final Message obtainMessage(int what, Object obj)
    {
    public final Message obtainMessage(int what, Object obj) {
        return Message.obtain(mSmHandler, what, obj);
    }

@@ -1561,8 +1552,7 @@ public class StateMachine {
     * @param arg2  is assigned to Message.arg2
     * @return  A Message object from the global pool
     */
    public final Message obtainMessage(int what, int arg1, int arg2)
    {
    public final Message obtainMessage(int what, int arg1, int arg2) {
        return Message.obtain(mSmHandler, what, arg1, arg2);
    }

@@ -1581,8 +1571,7 @@ public class StateMachine {
     * @param obj is assigned to Message.obj
     * @return  A Message object from the global pool
     */
    public final Message obtainMessage(int what, int arg1, int arg2, Object obj)
    {
    public final Message obtainMessage(int what, int arg1, int arg2, Object obj) {
        return Message.obtain(mSmHandler, what, arg1, arg2, obj);
    }

@@ -1805,27 +1794,77 @@ public class StateMachine {
        pw.println("curState=" + getCurrentState().getName());
    }

    /**
     * Log with debug and add to the LogRecords.
     *
     * @param s is string log
     */
    protected void logAndAddLogRec(String s) {
        addLogRec(s);
        log(s);
    }

    /**
     * Log with debug
     *
     * @param s is string log
     */
    protected void log(String s) {
        Log.d(mName, s);
    }

    /**
     * Log with debug attribute
     *
     * @param s is string log
     */
    protected void logd(String s) {
        Log.d(mName, s);
    }

    /**
     * Log with verbose attribute
     *
     * @param s is string log
     */
    protected void logv(String s) {
        Log.v(mName, s);
    }

    /**
     * Log with info attribute
     *
     * @param s is string log
     */
    protected void logi(String s) {
        Log.i(mName, s);
    }

    protected void logd(String s) {
        Log.d(mName, s);
    }

    /**
     * Log with warning attribute
     *
     * @param s is string log
     */
    protected void logw(String s) {
        Log.w(mName, s);
    }

    /**
     * Log with error attribute
     *
     * @param s is string log
     */
    protected void loge(String s) {
        Log.e(mName, s);
    }

    /**
     * Log with error attribute
     *
     * @param s is string log
     * @param e is a Throwable which logs additional information.
     */
    protected void loge(String s, Throwable e) {
        Log.e(mName, s, e);
    }
}