예제 #1
0
        public static LPLogger CreateLogger(ELogType eLogType, string sDir, string sFileName,
                                            ELogMode eLogMode, int nOutputMask, string sPrefix, int nLogLevel, bool bUseLock)
        {
            bool     bResult = false;
            LPLogger lg      = null;

            switch (eLogType)
            {
            case ELogType.File:
            {
                lg = new LPFileLogger();
                if (PTF_ERROR(lg != null))
                {
                    goto Exit0;
                }

                bResult = lg.Init(sDir, sFileName, eLogMode, nOutputMask, sPrefix, nLogLevel, bUseLock);
                if (PTF_ERROR(bResult))
                {
                    goto Exit0;
                }
            }
            break;

            case ELogType.Invalid:
            case ELogType.Net:
            case ELogType.Total:
            default:
                if (PTF_ERROR(false))
                {
                    goto Exit0;
                }
                break;
            }

            return(lg);

Exit0:

            if (lg != null)
            {
                PTF_ERROR(lg.UnInit());
                lg = null;
            }
            return(null);
        }
예제 #2
0
        private static void _ThreadProc(Object param)
        {
            bool bResult                 = false;
            int  nSleepMicSec            = 10;       // 每次睡眠毫秒数
            int  nFlushIntervalSec       = 3000;     // flush间隔(毫秒)
            int  nLastFlushTimeMicSecInc = 0;        // 上次flush后的时间增量(毫秒)
            int  nStopRunWaitCount       = 0;

            int          nOnceReadableLen = 0;
            LPFileLogger oFileLogger      = null;
            LPLoopBuf    oLoopBuf         = null;

            int nLen = 0;

            byte[] lenBytes = null;

            oFileLogger = (LPFileLogger)param;
            if (PTF_ERROR(oFileLogger != null))
            {
                goto Exit0;
            }
            oLoopBuf = oFileLogger.m_oLoopBuf;
            if (PTF_ERROR(oLoopBuf != null))
            {
                goto Exit0;
            }

            lenBytes = new byte[sizeof(int)];
            if (PTF_ERROR(lenBytes != null))
            {
                goto Exit0;
            }

            while (oFileLogger.m_bRun || oLoopBuf.GetOnceReadableLen() > 0)
            {
                //防止LPLoopBuf出错造成不退出(等待500 * 10毫米)
                if (!oFileLogger.m_bRun)
                {
                    ++nStopRunWaitCount;
                    if (nStopRunWaitCount > 500)
                    {
                        PTF_ERROR(false);
                        break;
                    }
                }

                while (oLoopBuf.GetTotalReadableLen() > sizeof(int))
                {
                    bResult = oLoopBuf.Read(lenBytes, 0, sizeof(int), false);
                    PTF_ERROR(bResult);

                    nLen = BitConverter.ToInt32(lenBytes, 0);
                    if (oLoopBuf.GetTotalReadableLen() < nLen)
                    {
                        break;
                    }

                    oLoopBuf.FinishRead(sizeof(int));
                    oFileLogger._UpdateFilePointer();

                    nOnceReadableLen = oLoopBuf.GetOnceReadableLen();

                    if ((oFileLogger.m_nOutputMask & (int)EOutputType.File) > 0)
                    {
                        if (nOnceReadableLen < nLen)
                        {
                            oFileLogger.m_oFile.Write(oLoopBuf.BufBytes,
                                                      oLoopBuf.ReadInx, nOnceReadableLen);
                            oFileLogger.m_oFile.Write(oLoopBuf.BufBytes,
                                                      0, nLen - nOnceReadableLen);
                        }
                        else
                        {
                            oFileLogger.m_oFile.Write(oLoopBuf.BufBytes,
                                                      oLoopBuf.ReadInx, nLen);
                        }
                    }

                    oLoopBuf.FinishRead(nLen);

                    oFileLogger.m_bDirty = true;
                }

                if (oFileLogger.m_bErrorLog)
                {
                    oLoopBuf.FinishRead(oLoopBuf.GetTotalReadableLen());
                    oFileLogger.m_bErrorLog = false;
                }

                nLastFlushTimeMicSecInc += nSleepMicSec;
                if (nLastFlushTimeMicSecInc > nFlushIntervalSec)
                {
                    nLastFlushTimeMicSecInc = 0;
                    oFileLogger._Flush();
                }

                if (oLoopBuf.GetTotalReadableLen() <= sizeof(int))
                {
                    Thread.Sleep(nSleepMicSec);
                }
            }

Exit0:

            oFileLogger._Flush();

            return;
        }