Files
redAcore/LogCore.cpp
Charl Wentzel aaf3c59727 Important Updates:
- DataTreeCore:
  - Allow types float, int & bool to be read as strings with GetStr()
- LogCore:
  - Add comments to Logging parameters
- WatchDogCore:
  - Call CSelectableCore::Process() in Process() to manage connect
- SelectableCore:
  - Allow SetSocketHandle() to be called if Handle already set as socket
  - added strlcase() method to convert string to lower case
  - Update Handle structure:
    - renamed Address -> HostName
    - renamed PortNo -> PortName
    - added AddressList for all resolved addresses
    - added AddressInfo for active address
  - Add ResolveAddress() method
    - Domain name and protocol port resolving with GetAddrInfo()
  - JSON updated:
    - domain name can be provided instead of IP address
    - protocol can be specified instead of Port No, e.g. "HTTP" / "SSH"
  - Resolve address before connect, or use next resolved address
2017-07-22 09:40:17 +02:00

150 lines
3.9 KiB
C++

/*
* LogCore.cpp
*
* Created on: 17 May 2016
* Author: wentzelc
*/
// redA Libraries
#include "LogCore.h"
#include "DateTimeCore.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
//---------------------------------------------------------------------------
CLogCore::CLogCore( FILE * pOutputFile )
{
OutputFile = pOutputFile;
}
//---------------------------------------------------------------------------
bool CLogCore::Message( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const char * Format, ... )
{
va_list ArgPtr;
// Validate values
if (!Format || !*Format)
return false;
// Check debug level
if (!OutputFile || (MsgLevel > DebugLevel)) {
return true;
}
// Show Date & Time
if (OutputFile->_fileno > 3) {
fprintf( OutputFile, "%s", GetDateTimeStr( "/", ":" ));
}
// Print formated message
va_start( ArgPtr, Format );
vfprintf( OutputFile, Format, ArgPtr );
fprintf( OutputFile, "\n" );
va_end( ArgPtr );
return true;
}
//---------------------------------------------------------------------------
bool CLogCore::Output( EDebugLevel DebugLevel, EDebugLevel MsgLevel, const short Show, const char * Buffer, int Len, const char * Format, ... )
{
va_list ArgPtr;
// Validate values
if (!Buffer || !(Show & (OUT_COUNT | OUT_NORMAL | OUT_HEX | OUT_BIN)))
return false;
// Check debug level
if (!OutputFile || (MsgLevel > DebugLevel)) {
return true;
}
// Get length
if (Len == -1)
Len = strlen( Buffer );
// Show date & time
if (OutputFile->_fileno > 3) {
fprintf( OutputFile, "%s", GetDateTimeStr( "/", ":" ));
}
// Show Lead
if (Format && *Format) {
va_start( ArgPtr, Format );
vfprintf( OutputFile, Format, ArgPtr );
va_end( ArgPtr );
}
// Show byte count
if (Show & OUT_COUNT)
{
// Print byte count
fprintf( OutputFile, " [%d] ", Len );
// EOL if only count wanted
if (Show & OUT_COUNT) {
fprintf( OutputFile, "\n" );
}
}
// Show Normal output
if (Show & OUT_NORMAL)
{
if (Show & OUT_ASIS) {
// Print entire buffer as is (line feeds included)
fprintf( OutputFile, "%s", Buffer );
}
else {
// Ignore \r\n
for (int i=0; i<Len; i++) {
if ((Buffer[i] < 32) || (Buffer[i] > 126)) {
if ((Show & OUT_CRLF) && ((Buffer[i] == '\r') || (Buffer[i] == '\n')))
fprintf( OutputFile, "%c", Buffer[i] );
else
fprintf( OutputFile, "." );
}
else {
fprintf( OutputFile, "%c", Buffer[i] );
}
}
}
// Add EOL if not present or ignored
if (!(Show & (OUT_ASIS | OUT_CRLF)) || (Buffer[Len-1] != '\n')) {
fprintf( OutputFile, "\n" );
}
}
// Show Hex output
if (Show & OUT_HEX)
{
// Print Hex values of individual bytes
for (int i=0; i<Len; i++) {
fprintf( OutputFile, "%02X ", (unsigned char)Buffer[i] );
}
fprintf( OutputFile, "\n" );
}
// Show Binary output
if (Show & OUT_BIN) {
// Print each byte as 8-bit binary
for (int i=0; i<Len; i++) {
for (int j=0; j<8; j++) {
fprintf( OutputFile, "%d", (bool)((Buffer[i] << j) & 0x80) );
}
}
fprintf( OutputFile, "\n" );
}
return true;
}
//---------------------------------------------------------------------------