/* * LogCore.cpp * * Created on: 17 May 2016 * Author: wentzelc */ // Standard C/C++ Libraries #include #include // redA Libraries #include "LogCore.h" #include "DateTimeCore.h" #include "UtilCore.h" //--------------------------------------------------------------------------- // Global vars const char * LogOutputName[] = { "None", "Raw", "Normal", "Hex", "Bin" }; //--------------------------------------------------------------------------- CLogCore::CLogCore( FILE * pOutputFile, int pBufferSize ) { OutputFile = pOutputFile; BufferSize = pBufferSize; TempBuffer = (char*)malloc( BufferSize * sizeof(char) ); } //--------------------------------------------------------------------------- int CLogCore::ReadLogBufSize( CDataMember * LogConfig ) { int NewBufSize; // Get buffer size NewBufSize = LogConfig->GetChInt( "BufferSize", BufferSize, true ); // Update buffer if size different if ((NewBufSize > 0) && (NewBufSize != BufferSize)) { free( TempBuffer ); BufferSize = NewBufSize; TempBuffer = (char*)malloc( BufferSize * sizeof(char) ); } return BufferSize; } //--------------------------------------------------------------------------- EDebugLevel CLogCore::ReadLogLevel( CDataMember * LogConfig ) { const char * TempStr; EDebugLevel LogLevel; // Get debug level LogLevel = dlNone; TempStr = 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; const char * TempStr; ELogOutput LogOutput = loNone; int LogOutOpt = 0; // Get Output format LogOutput = loNone; TempStr = 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) { 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, '.', TempBuffer) ); // 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, " ", TempBuffer) ); fprintf( OutputFile, "\n" ); } else if (OutFormat == loBin) { // Print each byte as 8-bit binary fprintf( OutputFile, "%s", BytesToBinStr(Buffer, Len, " ", TempBuffer) ); fprintf( OutputFile, "\n" ); } return true; } //---------------------------------------------------------------------------