Files
redAcore/LogCore.cpp
Charl Wentzel b4073a166a Major Update:
- Updated all Logged messages, standardised DebugLevel:
  - dlNone   - Show startup and stop
  - dlLow    - Show creation/destruction of Function Blocks and Local IO
  - dlMedium - Show connection events, eg. open/close
  - dlHigh   - Show data flow events
-LogCore:
  - LogMessage and ShowOutput uses va_list
    only run printf if DebugLevel correct
  - Remove global LogStr[] variable
- SelectableCore:
  - Implemented Auto-management of handles
    auto open on startup/fail/close
  - Changed simple Open/Close/Read/Write methods to inline
  - Do not set Select Write trigger for server socket
  - Memory leak, setting address twice on RemoteClient
  - Bug fix, only send handle out data if DebugLevel = dlHigh
  - Bug fix, do not Read/ProcessBuffer if no InputBuffer
2016-05-27 13:32:54 +02:00

104 lines
2.5 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
EDebugLevel DebugLevel = dlLow;
char LogStr[1000]; // Temporary var to create log messages, globally available to save on memory operations
//---------------------------------------------------------------------------
bool LogMessage( EDebugLevel Level, const char * Format, ... )
{
va_list ArgPtr;
// Validate values
if (!Format || !*Format)
return false;
// Check debug level
if (Level > DebugLevel) {
return true;
}
// Show normal output
va_start( ArgPtr, Format );
vprintf( Format, ArgPtr );
printf( "\r\n" );
va_end( ArgPtr );
return true;
}
//---------------------------------------------------------------------------
bool ShowOutput( EDebugLevel Level, const short Show, const char * Buffer, int Len, const char * Format, ... )
{
va_list ArgPtr;
// Validate values
if (!Buffer || !(Show & (OUT_HEX | OUT_NORMAL)))
return false;
if (Len == -1)
Len = strlen( Buffer );
// Check debug level
if (Level > DebugLevel) {
return true;
}
// Show Lead
if (Format && *Format) {
va_start( ArgPtr, Format );
vprintf( Format, ArgPtr );
va_end( ArgPtr );
}
printf( "[%d] ", Len );
// Show Hex output
if (Show & OUT_HEX) {
for (int i=0; i<Len; i++) {
printf( "%02X ", Buffer[i] );
}
printf( "\n" );
}
// 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" );
}
}
return true;
}
//---------------------------------------------------------------------------