📄 zvlogoutput.c
字号:
#include "zvLog.h"#include "zvLogOutput.h"#include "zvOutputTable.h"#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <assert.h>/******************************************************************************\* ZEVIOLogOutputInit ** ** Call this function to ensure that this library is linked. ** This function will initialize the Log Output Structure ** *\******************************************************************************/retCode ZEVIOLogOutputInit() { retCode err; ZEVIOLogStructure *log = ZEVIOLogGet(); if( log == NULL ) { printf( "ZEVIOLogOutputInit: ERROR. Could not get log pointer.\n" ); return ZEVIO_ERR_FAIL; } // if the Output is already inited, just return if (log->outS != NULL) { printf("ZEVIOLogOutputInit. Already initialized\n"); return ZEVIO_ERR_FAIL; } log->outS = (ZEVIOLogOutputStructure *) malloc(sizeof(ZEVIOLogOutputStructure)); // if the malloc failed... if (log->outS == NULL) { printf("ZEVIOLogOutputInit ERROR. Failed to allocate space for the log output structure\n"); return ZEVIO_ERR_FAIL; } /* err = ZEVIOLogConfigFTP("ZEVIOare.log", "10.0.0.1", "anonymous", "none"); ZEVIO_ASSERT(err == ZEVIO_SUCCESS); err = ZEVIOLogSetFilter(NULL); ZEVIO_ASSERT(err == ZEVIO_SUCCESS); err = ZEVIOLogConfigDev("/tyCo/1"); ZEVIO_ASSERT(err == ZEVIO_SUCCESS); */ err = ZEVIOLogSetFilter(NULL); ZEVIO_ASSERT(err == ZEVIO_SUCCESS); //err = ZEVIOLogConfigDev("log.out"); //ZEVIO_ASSERT(err == ZEVIO_SUCCESS); return ZEVIO_SUCCESS;}/******************************************************************************\* ZEVIOLogPrintDev (public function) ** ** Prints the entire log contents over the specified device in formatted, ** readable form. The log is suspended while this printout is occurring. ** *\******************************************************************************/retCode ZEVIOLogPrintDev(char *devName) { ZEVIOLogConfigDev(devName); return ZEVIOLogPrint(ZEVIO_DEV_OUT);}/******************************************************************************\* ZEVIOLogConfigDev (public function) ** ** Config the Device. A parameter set to NULL will keep the previous setting. ** *\******************************************************************************/retCode ZEVIOLogConfigDev(char *devName) { ZEVIOLogOutputStructure *logout = ZEVIOLogOutputGet();; if( logout == NULL ) { printf( "ZEVIOLogPrintDev: ERROR. Could not get log output pointer.\n" ); return( ZEVIO_ERR_FAIL ); } if(devName != NULL) { strncpy(logout->dev.device, devName, ZEVIO_LOG_MAX_PARAM_LENGTH); logout->dev.device[ZEVIO_LOG_MAX_PARAM_LENGTH-1] = '\0'; } return ZEVIO_SUCCESS;}/******************************************************************************\* ZEVIOLogGetEventName (public function) ** ** This function returns a string containing the printable name of an event, ** based on the enumerated type for the event. It is used to send a readable ** log dump to the screen or to FTP. Update this function if you add a new ** event type. ** *\******************************************************************************/retCode ZEVIOLogGetEventName( ZEVIO_LOG_EVENT event, char *name ){ int len = strlen(outputTable[event]); memcpy(name, outputTable[event], len); name[len] = '\0';}/******************************************************************************\* printEngineScreen (private function) ** ** This function print a string on the screen (Host screen) ** *\******************************************************************************/int printEngineScreen(char *str) { return printf("%s", str);}/******************************************************************************\* printEngineDev (private function) ** ** This function print a string on the specified device. ** *\******************************************************************************/int printEngineDev(char *str) { ZEVIOLogOutputStructure *logout = ZEVIOLogOutputGet(); // We do not need to test that logout is not NULL since it was already // verified in the calling function return write(logout->dev.fd, str, strlen (str));}/******************************************************************************\* ZEVIOLogPrintKernel (private function) ** ** This function is the kernel of the print functions. It is generic so it can ** into different media that is defined by the print engine ** *\******************************************************************************/retCode ZEVIOLogPrintKernel(){ ZEVIOLogStructure *log; int i, j, k; int index; int (*printEngine)(char *str); ZEVIOLogOutputStructure *logout = (ZEVIOLogGet())->outS; char name[ZEVIO_LOG_MAX_NAME_LENGTH + 1]; char str[ZEVIO_LOG_MAX_NAME_LENGTH + 2]; char *filter; int filterTest; unsigned int entryID; unsigned int entryLevel; unsigned int entrySize; unsigned int lastTime = 0; int currentState; printEngine = logout->printEngine; filter = logout->filter; filterTest = (filter[0] == '='? 1 : 0); log = ZEVIOLogGet(); if( log == NULL ) { printf( "ZEVIOLogPrint: ERROR. Could not get log pointer.\n" ); return( ZEVIO_ERR_FAIL ); } currentState = log->active; if (currentState) ZEVIOLogSuspend(); // print a header printEngine( "LVL Time (us) Delta (us) Event" ); i = (ZEVIO_LOG_MAX_NAME_LENGTH - 4); memset(str, ' ', i); str[i] = '\0'; printEngine(str); printEngine("Parameters\n"); // print all log entries index = log->firstEntry; for( k = 0; k < log->numEntries; k++ ) { // Parse log entry entryID = ZEVIO_LOG_ENTRY_ID( log->pBase[index] ); entryLevel = ZEVIO_LOG_ENTRY_LEVEL( log->pBase[index] ); entrySize = ZEVIO_LOG_ENTRY_SIZE( log->pBase[index] ); ZEVIOLogGetEventName( entryID, name ); if (filter[0] != '\0') { if ( (strstr(name, &(filter[1])) == NULL) == filterTest ) { // If the line does not satisfy the filter index = (index + 2 + entrySize) % log->size; continue; } } // print log level and indent for convenience i = (entryLevel>4 ? 3 : entryLevel-1); memset(str, ' ', i); str[i] = '\0'; printEngine(str); sprintf(str, "%d", entryLevel ); printEngine(str); i = 4 - entryLevel; memset(str, ' ', i); str[i] = '\0'; printEngine(str); // print time stamp in microseconds sprintf(str, "(%10u) ", log->pBase[(index + 1) % log->size] ); printEngine(str); // print delta time since last time stamp, in microseconds sprintf(str, "(%10u) ", log->pBase[(index + 1) % log->size] - lastTime); printEngine(str); lastTime = log->pBase[(index + 1) % log->size]; // print log name, padded to maximum name length printEngine(name); i = ZEVIO_LOG_MAX_NAME_LENGTH - strlen(name) + 1; memset(str, ' ', i); str[i] = '\0'; printEngine(str); // print additional log data as hexadecimal words for( j = 0; j < entrySize; j++ ) { sprintf(str, "0x%08x ", log->pBase[(index + 2 + j) % log->size] ); printEngine(str); } printEngine("\n"); index = (index + 2 + entrySize) % log->size; } if (currentState) ZEVIOLogResume(); return( ZEVIO_SUCCESS );}/******************************************************************************\* ZEVIOLogPrint (public function) ** ** Prints the entire log contents to the screen in formatted, readable form. ** The log is suspended while this printout is occurring. ** *\******************************************************************************/retCode ZEVIOLogPrint(ZEVIO_LOG_OUTPUT out) { ZEVIOLogOutputStructure *logout = ZEVIOLogOutputGet();; if( logout == NULL ) { printf( "ZEVIOLogPrint: ERROR. Could not get log output pointer.\n" ); return( ZEVIO_ERR_FAIL ); } if (out == ZEVIO_SCREEN_OUT) { logout->printEngine = printEngineScreen; } else if(out == ZEVIO_DEV_OUT) { logout->printEngine = printEngineDev; if ((logout->dev.fd = open(logout->dev.device, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH |S_IWOTH)) == -1) { printf( "ZEVIOLogDev: ERROR. Could not open output device.\n" ); return( ZEVIO_ERR_FAIL ); } } else { printf("ZEVIOLogPrint: Output format not supported. out=%d\n", out); return ZEVIO_ERR_FAIL; } // Print into the specified output ZEVIOLogPrintKernel(); if(out == ZEVIO_DEV_OUT) { if (close(logout->dev.fd) == -1) { printf( "ZEVIOLogDev: ERROR. File not closed correctly.\n" ); return( ZEVIO_ERR_FAIL ); } } return ZEVIO_SUCCESS;}/******************************************************************************\* ZEVIOLogSetFilter (public function) ** ** Set a filter. A parameter set to NULL will reset the filtering. ** Filtering string will be automaticaly converted into upper case so case ** will not matter ** *\******************************************************************************/retCode ZEVIOLogSetFilter( char *filter) { ZEVIOLogOutputStructure *logout; int i; char str[ZEVIO_LOG_MAX_PARAM_LENGTH]; logout = ZEVIOLogOutputGet(); if( logout == NULL ) { printf( "ZEVIOLogSetFilter: ERROR. Could not get log output pointer.\n" ); return ZEVIO_ERR_FAIL; } if (filter == NULL || filter[0] == '\0') { logout->filter[0] = '\0'; return ZEVIO_SUCCESS; } if (filter[0] != '=' && filter[0] != '!') { printf("ZEVIOLogSetFilter: ERROR. First character of the filter must be either:\n\t'=' for \"contains\"\n\t'!' for \"does not contains\"\n"); return ZEVIO_ERR_FAIL; } if (strlen(filter) < 2) { printf("ZEVIOLogSetFilter: ERROR. You must enter a string as filtering\n"); return ZEVIO_ERR_FAIL; } str[0] = filter[0]; for (i = 1; i<strlen(filter) && i<ZEVIO_LOG_MAX_PARAM_LENGTH; i++) { if (!isalnum(filter[i]) && filter[i] != '_') { printf("ZEVIOLogSetFilter: ERROR. You must enter a string as filtering\nThe filtering string must contains only alphanumeric characters or '_' character\n"); return ZEVIO_ERR_FAIL; } //str[i] = toupper(filter[i]); str[i] = filter[i]; } str[i] = '\0'; strncpy(logout->filter, str, ZEVIO_LOG_MAX_PARAM_LENGTH); return ZEVIO_SUCCESS;}/******************************************************************************\* ZEVIOLogOutputGet (public function) ** ** This function returns a pointer to the log output data structure. This is ** used in order to read and output the log. ** *\******************************************************************************/ZEVIOLogOutputStructure *ZEVIOLogOutputGet(){ ZEVIOLogStructure *log = ZEVIOLogGet(); ZEVIOLogOutputStructure *logout; if( log == NULL ) { printf( "ZEVIOLogOutputget: ERROR. Could not get log pointer.\n" ); return NULL; } logout = log->outS; return logout;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -