Files
redAcore/LogCore.cpp
Charl Wentzel 0046edc251 Important Update:
- Implemented new Util library:
  - Convert Bytes to Hex/Bin/Safe string
  - Convert HexString to Bytes
  - Search String for substring function
- Use Util library in LogCore for String conversions
2019-04-15 17:26:52 +02:00

203 lines
6.0 KiB
C++

/*
* LogCore.cpp
*
* Created on: 17 May 2016
* Author: wentzelc
*/
// Standard C/C++ Libraries
#include <string.h>
#include <stdarg.h>
// redA Libraries
#include "LogCore.h"
#include "DateTimeCore.h"
#include "UtilCore.h"
//---------------------------------------------------------------------------
// Global vars
char LogStr[1000]; // Temporary var to create log messages, globally available to save on memory operations
const char * LogOutputName[] = { "None", "Raw", "Normal", "Hex", "Bin" };
//---------------------------------------------------------------------------
CLogCore::CLogCore( FILE * pOutputFile )
{
OutputFile = pOutputFile;
}
//---------------------------------------------------------------------------
EDebugLevel CLogCore::ReadLogLevel( CDataMember * LogConfig )
{
char * TempStr;
EDebugLevel LogLevel;
// Get debug level
LogLevel = dlNone;
TempStr = (char*)LogConfig->GetChStr( "Level", "Medium", true );
if (TempStr)
{
if (!strcasecmp( TempStr, "None" ))
LogLevel = dlNone;
else if (!strcasecmp( TempStr, "Low" ))
LogLevel = dlLow;
else if (!strcasecmp( TempStr, "Medium" ))
LogLevel = dlMedium;
else if (!strcasecmp( TempStr, "High" ))
LogLevel = dlHigh;
else
LogLevel = dlMedium;
}
return LogLevel;
}
//---------------------------------------------------------------------------
// ls nibble = format, ms nibble = options.
int CLogCore::ReadLogOutput( CDataMember * LogConfig )
{
CDataMember * ItemConfig;
char * TempStr;
ELogOutput LogOutput = loNone;
int LogOutOpt = 0;
// Get Output format
LogOutput = loNone;
TempStr = (char*)LogConfig->GetChStr( "Output", "Normal", true );
if (TempStr)
{
if (!strcasecmp( TempStr, "None" )) // Do not print output (may use option OUT_COUNT)
LogOutput = loNone;
else if (!strcasecmp( TempStr, "Raw" )) // Print all output as is
LogOutput = loRaw;
else if (!strcasecmp( TempStr, "Normal" )) // Print normal, but replace special chars with "."
LogOutput = loNormal;
else if (!strcasecmp( TempStr, "Bin" )) // Print as Binary value ('1' and '0')
LogOutput = loBin;
else if (!strcasecmp( TempStr, "Hex" )) // Print as Hexadecimal value (0-9,A-F)
LogOutput = loHex;
else
LogOutput = loNormal;
}
// Set debug output
ItemConfig = LogConfig->GetChElement( "Options", 0, true );
while (ItemConfig)
{
if ((TempStr = (char*)ItemConfig->GetStr( "Normal" )))
{
if (!strcasecmp( TempStr, "Count" )) // Print number of characters
LogOutOpt |= OUT_COUNT;
else if (!strcasecmp( TempStr, "NoCRLF" )) // With Normal, replace CR/LF with "."
LogOutOpt |= OUT_NOCRLF;
}
ItemConfig = ItemConfig->GetNextPeer();
}
// Combine Output format & options
LogOutOpt = (LogOutput | LogOutOpt)? (LogOutput | LogOutOpt) : loNormal;
return LogOutOpt;
}
//---------------------------------------------------------------------------
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 OutputFormat, const char * Buffer, int Len, const char * Format, ... )
{
va_list ArgPtr;
int OutFormat = OutputFormat & 0x0F;
bool ShowCount = OutputFormat & OUT_COUNT;
bool NoCrLf = OutputFormat & OUT_NOCRLF;
// Validate values
if (!Buffer || (!OutFormat && !ShowCount))
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 (ShowCount)
{
// Print byte count
fprintf( OutputFile, " [%d] ", Len );
if (!OutFormat) fprintf( OutputFile, "\n" ); // EOL if only count wanted
}
// Show output
if (OutFormat == loRaw)
{
// Print entire buffer as is (line feeds included)
fprintf( OutputFile, "%.*s\n", Len, Buffer );
}
else if (OutFormat == loNormal)
{
// Remove special char
fprintf( OutputFile, "%s", BytesToSafeStr(Buffer, Len, NoCrLf, '.') );
// Add EOL if not present or ignored
if (NoCrLf || (Buffer[Len-1] != '\n')) {
fprintf( OutputFile, "\n" );
}
}
else if (OutFormat == loHex)
{
// Print Hex values of individual bytes
fprintf( OutputFile, "%s", BytesToHexStr(Buffer, Len, " ") );
fprintf( OutputFile, "\n" );
}
else if (OutFormat == loBin)
{
// Print each byte as 8-bit binary
fprintf( OutputFile, "%s", BytesToBinStr(Buffer, Len, " ") );
fprintf( OutputFile, "\n" );
}
return true;
}
//---------------------------------------------------------------------------