Files
redAcore/LogCore.cpp
Charl Wentzel b418fa73e0 Important Update:
- LogCore:
  - Removed global DebugLevel var, now passed as parameter
  - Implemented binary output values
- FunctionCore, SelectCore and SelectableCore:
  - internal DebugLevel param implemented
  - new internal DisplayOutput param (Normal/Hex/Bin)
- BufferCore:
  - Bug fix: allow for empty search string/marker
2016-06-09 06:53:09 +02:00

113 lines
2.8 KiB
C++

/*
* LogCore.cpp
*
* Created on: 17 May 2016
* Author: wentzelc
*/
// redA Libraries
#include "LogCore.h"
// Standard C/C++ Libraries
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
//---------------------------------------------------------------------------
// Global vars
char LogStr[1000]; // Temporary var to create log messages, globally available to save on memory operations
//---------------------------------------------------------------------------
bool LogMessage( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const char * Format, ... )
{
va_list ArgPtr;
// Validate values
if (!Format || !*Format)
return false;
// Check debug level
if (MsgLevel > DebugLevel) {
return true;
}
// Show normal output
va_start( ArgPtr, Format );
vprintf( Format, ArgPtr );
printf( "\r\n" );
va_end( ArgPtr );
return true;
}
//---------------------------------------------------------------------------
bool ShowOutput( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short Show, const char * Buffer, int Len, const char * Format, ... )
{
va_list ArgPtr;
// Validate values
if (!Buffer || !(Show & (OUT_NORMAL | OUT_HEX | OUT_BIN)))
return false;
if (Len == -1)
Len = strlen( Buffer );
// Check debug level
if (MsgLevel > DebugLevel) {
return true;
}
// Show Lead
if (Format && *Format) {
va_start( ArgPtr, Format );
vprintf( Format, ArgPtr );
va_end( ArgPtr );
}
printf( "[%d] ", Len );
// Show Normal output
if (Show & OUT_NORMAL) {
if (Show & OUT_ASIS) {
printf( "%s", Buffer );
}
else {
for (int i=0; i<Len; i++) {
if ((Buffer[i] < 32) || (Buffer[i] > 126)) {
if ((Show & OUT_CRLF) && ((Buffer[i] == '\r') || (Buffer[i] == '\n')))
printf( "%c", Buffer[i] );
else
printf( "." );
}
else {
printf( "%c", Buffer[i] );
}
}
}
if (!(Show & (OUT_ASIS | OUT_CRLF)) || (Buffer[Len-1] != '\n')) {
printf( "\n" );
}
}
// Show Hex output
if (Show & OUT_HEX) {
for (int i=0; i<Len; i++) {
printf( "%02X ", (unsigned char)Buffer[i] );
}
printf( "\n" );
}
// Show Binary output
if (Show & OUT_BIN) {
for (int i=0; i<Len; i++) {
for (int j=0; j<8; j++) {
printf( "%d", (bool)((Buffer[i] << j) & 0x80) );
}
}
printf( "\n" );
}
return true;
}
//---------------------------------------------------------------------------