⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zvlog.c

📁 一个内存日志系统
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include <stdarg.h>#include "zvLog.h"/******************************************************************************\* Static Data                                                                  *\******************************************************************************/ZEVIOLogStructure *zevioLog = NULL;static unsigned int GetTime( void ){    struct timeval tv;    gettimeofday(&tv, NULL);    return tv.tv_usec + 1000000*tv.tv_sec;}/******************************************************************************\* ZEVIOLogInit (public function)                                                  **                                                                              ** This function initializes the C-Ware global log.  It is also capable of      ** re-initializing it if necessary.  It allocates logSize bytes for the log as  ** a ring buffer.                                                               **                                                                              *\******************************************************************************/retCode ZEVIOLogInit( unsigned int logSize ){    //unsigned int allocationSize;    // Protect against memory leaks from reinitialization    if( zevioLog != NULL )    {      free(zevioLog);    }    // If size is sufficient for one entry, allocate log memory    if( logSize >= sizeof( ZEVIOLogEvent ) )    {        zevioLog  = (ZEVIOLogStructure *)malloc( sizeof(ZEVIOLogStructure) + logSize );                                                                if( zevioLog == NULL )        {            return( ZEVIO_ERR_FAIL );        }                                                         //Assume the pBase is the first element in the ZEVIOLogStructure                                                        zevioLog->pBase = (unsigned int*)((int)zevioLog + sizeof(ZEVIOLogStructure));                // Activate log        zevioLog->active = 1;            }    else    {        // Activate log        zevioLog->active = 0;    }            // Initialize log information    zevioLog->size = logSize / sizeof( unsigned int );    zevioLog->numEntries = 0;    zevioLog->firstEntry = 0;    zevioLog->nextEntry = 0;        zevioLog->wraparound = 1;    zevioLog->overflow = 0;    zevioLog->outS = NULL;        if(zevioLog->active == 1)    {        ZEVIOLogAddEntry( ZEVIO_EVENT_LOG_INITIALIZED, ZEVIO_LOG_MAJOR, 0, 0, 0, 0, 0, 0, 0 );    }        return( ZEVIO_SUCCESS );}/******************************************************************************\* zevioLogConfig (public function)                                                **                                                                              ** This function is used to configure the buffer                                **                                                                              *\******************************************************************************/retCode ZEVIOLogConfig(int wraparound) {    zevioLog->wraparound = wraparound;     return ZEVIO_SUCCESS;}/******************************************************************************\* zevioLogClearBuffer (public function)                                           **                                                                              ** This function reset the buffer so it is empty                    **                                                                              *\******************************************************************************/retCode ZEVIOLogClearBuffer() {    zevioLog->numEntries = 0;    zevioLog->firstEntry = 0;    zevioLog->nextEntry = 0;    zevioLog->overflow = 0;      return ZEVIO_SUCCESS;};/******************************************************************************\* ZEVIOLogAddEntry (public function)                                              **                                                                              ** This function adds an entry to the log.  It is called using the macros       ** ZEVIO_LOG0 through ZEVIO_LOG6.  It generates the entry using the ID and level      ** passed as parameters, the current time, and the number of additional         ** parameters specified.                                                        **                                                                              *\******************************************************************************/retCode ZEVIOLogAddEntry( ZEVIO_LOG_EVENT event, unsigned int level, unsigned int numParams,                    unsigned int param1, unsigned int param2, unsigned int param3,                    unsigned int param4, unsigned int param5, unsigned int param6 ){    unsigned int temp[6];    temp[0] = param1;    temp[1] = param2;    temp[2] = param3;    temp[3] = param4;    temp[4] = param5;    temp[5] = param6;    return ZEVIOLogAddEntryByRef( event, level, numParams, temp );}/******************************************************************************\* ZEVIOLogAddEntryV (public function)                                             **                                                                              ** This function adds an entry to the log.  It is called using the macros       ** ZEVIO_LOGV. It generates the entry using the ID and level passed as             ** parameters, the current time, and the number of additional parameters        ** specified.                                                                   **                                                                              *\******************************************************************************/retCode ZEVIOLogAddEntryV( ZEVIO_LOG_EVENT event, unsigned int level, unsigned int numParams, ... ){    unsigned int  temp[numParams];    va_list ap;    int     i;        va_start(ap, numParams);        for (i=0; i<numParams; i++)     {        temp[i] = va_arg(ap, unsigned int);    }        va_end(ap);        return ZEVIOLogAddEntryByRef( event, level, numParams, temp );}/******************************************************************************\* ZEVIOLogAddEntryByRef (public function)                                         **                                                                              ** This function adds an entry to the log.  It is called using the macro        ** ZEVIO_LOGP.  It generates the entry using the ID and level passed as parameters ** plus the current time and the specified number of parameter words from the   ** params pointer.                                                              **                                                                              *\******************************************************************************/retCode ZEVIOLogAddEntryByRef( ZEVIO_LOG_EVENT event, unsigned int level, unsigned int numParams,                         unsigned int *params ){    unsigned int time;    unsigned int entry;    unsigned int freeSpace;    unsigned int index;    int i;    // Add entry only if log is active, not in overflow, and running on the System Processor    if( zevioLog->active && !zevioLog->overflow )    {      // parameter check        if( ((numParams + 2) > zevioLog->size) || (numParams > 0xFFF) || (params == NULL) )        {            numParams = 0;        }        // precalculate entry and time        entry = (event & 0x0000FFFF) | ((numParams & 0x00000FFF) << 16) | (level << 28);        time = GetTime();        // If we are not in wraparound mode then we need to test if we are enought memory        if(!(zevioLog->wraparound))         {                        freeSpace = zevioLog->firstEntry - zevioLog->nextEntry;                        if( zevioLog->firstEntry <= zevioLog->nextEntry )            {                freeSpace += zevioLog->size;            }            // It has to have enough space for the BUFFERFULL log            if (freeSpace < (4 + numParams))             {                           // Create the BUFFERFULL entry                entry = (ZEVIO_EVENT_LOG_BUFFERFULL) | (ZEVIO_LOG_MAJOR << 28);                                index = zevioLog->nextEntry;                zevioLog->pBase[index] = entry;                                index = (zevioLog->nextEntry + 1) % zevioLog->size;                zevioLog->pBase[index] = time;                                // Update log data                zevioLog->nextEntry = (zevioLog->nextEntry + 2) % zevioLog->size;                zevioLog->numEntries++;                                // Desactivate the log tool                zevioLog->active = 0;                zevioLog->overflow = 1;                                return ZEVIO_ERR_FAIL;            }        }                // If we are in warparound mode then we may need to free space        else         {                // Free old entries until there is enough space for the new entry            while( 1 )            {                freeSpace = zevioLog->firstEntry - zevioLog->nextEntry;                if( zevioLog->firstEntry <= zevioLog->nextEntry )                {                    freeSpace += zevioLog->size;                }                            if( freeSpace > (2 + numParams) )                {                    break;                }                            zevioLog->firstEntry = zevioLog->firstEntry + 2 + ZEVIO_LOG_ENTRY_SIZE( zevioLog->pBase[zevioLog->firstEntry] );                zevioLog->firstEntry %= zevioLog->size;                zevioLog->numEntries--;            }        }                    // Create the new entry        index = zevioLog->nextEntry;        zevioLog->pBase[index] = entry;            index = (zevioLog->nextEntry + 1) % zevioLog->size;        zevioLog->pBase[index] = time;            for( i = 0; i < numParams; i++ )        {            index = (zevioLog->nextEntry + 2 + i) % zevioLog->size;            zevioLog->pBase[index] = params[i];        }            // Update log data        zevioLog->nextEntry = (zevioLog->nextEntry + 2 + numParams) % zevioLog->size;        zevioLog->numEntries++;            return ZEVIO_SUCCESS;    }    else     {        return ZEVIO_ERR_FAIL;    }}/******************************************************************************\* zevioLogSuspend (public function)                                               **                                                                              ** This function suspends the log, preventing new entries from being added.     ** This is useful in order to output the log without the log changing.  It      ** would also be possible to use it for other reasons, such as suspending the   ** log as soon as a fatal error occurs.                                         **                                                                              *\******************************************************************************/retCode ZEVIOLogSuspend(){        ZEVIOLogAddEntry( ZEVIO_EVENT_LOG_SUSPEND, ZEVIO_LOG_MAJOR, 0, 0, 0, 0, 0, 0, 0 );    zevioLog->active = 0;    return ZEVIO_SUCCESS;}/******************************************************************************\* ZEVIOLogResume (public function)                                                **                                                                              ** This function resumes logging, if the log was previously suspended.          **                                                                              *\******************************************************************************/retCode ZEVIOLogResume(){    if( (zevioLog->size == 0) || (zevioLog->overflow) )    {        return ZEVIO_ERR_FAIL;    }    zevioLog->active = 1;    return ZEVIOLogAddEntry( ZEVIO_EVENT_LOG_RESUME, ZEVIO_LOG_MAJOR, 0, 0, 0, 0, 0, 0, 0 );}/******************************************************************************\* ZEVIOLogGet (public function)                                                   **                                                                              ** This function returns a pointer to the log data structure.  This us used in  ** order to read and output the log.                                            **                                                                              *\******************************************************************************/ZEVIOLogStructure *ZEVIOLogGet(){       return( zevioLog );}/******************************************************************************\* ZEVIOLogShow (public function)                                                   **                                                                              ** This function returns a pointer to the log data structure.  This us used in  ** order to read and output the log.                                            **                                                                              *\******************************************************************************/void ZEVIOLogShow(){    ZEVIOLogStructure *zevioLog = ZEVIOLogGet();        printf("zevioLog\n");    printf("Base           :0x%08x\n", (int)zevioLog);    printf("Size           :0x%08x\n", zevioLog->size * sizeof(unsigned int) + sizeof(ZEVIOLogStructure) );    printf("Buffer Start   :0x%08x\n", (int)zevioLog->pBase);    printf("Buffer End     :0x%08x\n", (int)zevioLog->pBase + zevioLog->size*sizeof(unsigned int));    printf("numEntries     :0x%08x\n", (int)zevioLog->numEntries);    printf("firstEntry     :0x%08x\n", (int)zevioLog->firstEntry);    printf("wraparound     :0x%08x\n", (int)zevioLog->wraparound);    printf("overflow       :0x%08x\n", (int)zevioLog->overflow);    printf("outS           :0x%08x\n", (int)zevioLog->outS);    printf("nextEntry      :0x%08x\n", (int)zevioLog->firstEntry);        fflush(stdout);        return;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -