bool log_closeLogfile(void) { assert(NULL != logFileHandle); if (fclose(logFileHandle) != 0) { logFileHandle = NULL; #if 0 == LOGGING_API_USES_VARIADIC_MACROS log_logMessage((LOGLEVEL_ERROR, "Unable to close logfile: %s", strerror(errno))); #else log_logMessage(LOGLEVEL_ERROR, "Unable to close logfile: %s", strerror(errno)); #endif // LOGGING_API_USES_VARIADIC_MACROS return true; } logFileHandle = NULL; return false; } // log_closeLogfile()
void* StandardFunction_printString(int argc, void* argv[]){ // Write output if(argc != 1) { log_logMessage(ERROR, "printInt_proc", "Unexpected argument count - expected 1, got %d", argc); return 0; } //TODO: Some kind of unescaping so we can have e.g. \n in the string? printf("%s", (char*)argv[0]); return 0; }
/** * Check if a given element is in a list. */ bool IteratedList_containsElement(IteratedList_PNTR l, void *element){ if(l==NULL){ log_logMessage(ERROR, ITERATED_LIST_NAME, ITERATED_LIST_NULL_POINTER); return(false); } if(IteratedList_isEmpty(l)){ return(false); } IteratedListNode_PNTR current = l->first; while (current != NULL){ if(current->payload == element) return(true); current = current->tail; } return(false); }
bool unittest_ringbuffer(void) { uint8_t byteBuffer[128]; unsigned length; ringbuffer_t rb = { byteBuffer, sizeof(byteBuffer), 0, 0 }; log_logMessage(LOGLEVEL_INFO, "Testing ringbuffer"); // Initialize the buffer. ringbuffer_init(&rb); // Write bytes to the buffer, then read them. for (length = 0;length <= sizeof(byteBuffer) + 1;length ++) { ringbuffer_status_t rs; unsigned rl, wl; // Write 0..length bytes to the ringbuffer. for (wl = 0;wl < length;wl ++) { rs = ringbuffer_put(&rb, (length + wl) & 0xff); if (wl < sizeof(byteBuffer)) { expectTrue(ring_ok == rs); expectTrue(ringbuffer_length(&rb) == (wl + 1)); } else { expectTrue(ring_full == rs); expectTrue(ringbuffer_length(&rb) == sizeof(byteBuffer)); } } // for wl if (wl > sizeof(byteBuffer)) { wl = sizeof(byteBuffer); } // Read 1..length+1 bytes from the ringbuffer. for (rl = 0;rl <= length;rl ++) { int c = ringbuffer_get(&rb); if (rl < wl) { expectTrue(c >= 0); expectTrue(((length + rl) & 0xff) == c); expectTrue(ringbuffer_length(&rb) == (wl - rl - 1)); } else { expectTrue(c < 0); expectTrue(ringbuffer_length(&rb) == 0); } c = ringbuffer_peek(&rb); if (rl + 1 < wl) { expectTrue(c >= 0); expectTrue(((length + rl + 1) & 0xff) == c); } else { expectTrue(c < 0); } } // for rl } // for length return true; } // unittest_ringbuffer()
void log_logData(log_level_t level, char const *pData, size_t nrOfBytes, char const *prefixStr, size_t hexWidth) { #ifndef FTR_LOG_GLOBAL_BUFFER static char debugBuffer[FTR_LOG_BUFFER_SIZE]; #endif // !FTR_LOG_GLOBAL_BUFFER bool firstLine = true; unsigned prefixLen, i; assert((level > LOGLEVEL_NONE) && (level <= LOGLEVEL_ALWAYS)); assert(NULL != pData); assert(NULL != prefixStr); assert(hexWidth*3 + 3 < sizeof(debugBuffer)); // Special handling for no data. if (0 == nrOfBytes) { #if 0 == LOGGING_API_USES_VARIADIC_MACROS log_logMessage((level, "%s (None)", prefixStr)); #else log_logMessage(level, "%s (None)", prefixStr); #endif // LOGGING_API_USES_VARIADIC_MACROS return; } prefixLen = (unsigned) strlen(prefixStr); while (nrOfBytes > 0) { // Calculate the most bytes that will fit into the buffer. size_t chunkSize = hexWidth; if (nrOfBytes < chunkSize) { chunkSize = nrOfBytes; } if (hexbuf2String(pData, chunkSize, debugBuffer, sizeof(debugBuffer), USE_CRLF, false, // CRLF, no ASCII (unsigned) hexWidth, // linewidth false, 0) // show no offset, offset = 0 == NULL) { #if 0 == LOGGING_API_USES_VARIADIC_MACROS log_logMessage((LOGLEVEL_ERROR, "tcp_log_data(): unable to hexify buffer")); #else log_logMessage(LOGLEVEL_ERROR, "tcp_log_data(): unable to hexify buffer"); #endif // LOGGING_API_USES_VARIADIC_MACROS return; } // Remove the trailing CRLF because log_logMessage() will append it again. debugBuffer[strlen(debugBuffer) - 2] = '\0'; if (firstLine) { #if 0 == LOGGING_API_USES_VARIADIC_MACROS log_logMessage((level, "%s %s", prefixStr, debugBuffer)); #else log_logMessage(level, "%s %s", prefixStr, debugBuffer); #endif // LOGGING_API_USES_VARIADIC_MACROS firstLine = false; } else { log_logLevelStart(level); for (i = prefixLen + 1;i > 0;i --) { log_logMessageContinue_impl(level, " "); } log_logMessageContinue_impl(level, debugBuffer); log_logMessageContinue_impl(level, "\n"); } nrOfBytes = nrOfBytes - chunkSize; pData = pData + chunkSize; } // while nrOfBytes > 0 } // log_logData()