Click here to Skip to main content
15,886,362 members
Articles / Desktop Programming / MFC

Advanced Logging For All Kind of Applications

Rate me:
Please Sign up or sign in to vote.
4.72/5 (13 votes)
23 Feb 2002CPOL5 min read 167.6K   6.2K   116  
Simple to use classes for logging and tracing
/********************************************************************\
	created:		2001/03/22
	created:		22:3:2001   20:23
	filename: 	c:\my projects\dualmagic\controlappl\include\clog.cpp
	file path:	c:\my projects\dualmagic\controlappl\include
	file base:	clog
	file ext:		cpp
	author:			Alex Kucherenko
	
	purpose:	
\********************************************************************/

#include "stdafx.h"
#include "CLog.h"

//////////////////////////////////////////////////////////////////////////
// Format string using sprintf function 

std::string CLog::LogFormatString( std::string Format, ... )
{
  va_list args;
  va_start( args, Format );
  m_tmpBuffer[0] = 0;
  _vsnprintf( m_tmpBuffer, sizeof( m_tmpBuffer ), Format.c_str(), args );
  va_end( args );

  return std::string( m_tmpBuffer );
}

//////////////////////////////////////////////////////////////////////////
// redirector of simple strings

int CLog::LogString( long Level, const char *szMessage )
{
  return LogString( Level, std::string( szMessage ) );
};

//////////////////////////////////////////////////////////////////////////
// Log String according to level  

int CLog::LogString( long Level, const std::string &Message )
{
  if( Level > m_lLevel ) return 0;

  std::string msgHeader;
  std::string retString;

  switch( Level )
  {
    case 0 : msgHeader = "LOG ERROR";   break;
    case 1 : msgHeader = "LOG WARNING"; break;
    case 2 : msgHeader = "LOG INFO";    break;
    default: 
      msgHeader = LevelText( Level );
  };

  retString = LogFormatString( 
    m_strMessageFormat, 
    GetCurrentProcessId(), 
    GetCurrentThreadId(), 
    ( m_bLogTime ) ? CurrentTime().c_str() : "", 
    msgHeader.c_str(), Message.c_str() );

  m_pStore->WriteString( retString );
  
  if( m_bAutoFlush == true )
    m_pStore->FlushData();

  return 0;
}

//////////////////////////////////////////////////////////////////////////
// log string without any formating and testing

int CLog::LogRawString( const std::string &Message )
{
  m_pStore->WriteString( Message );

  if( m_bAutoFlush == true )
    m_pStore->FlushData();

  return 0;
}

//:> End of file

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO ArtfulBits Inc.
Ukraine Ukraine
Name:Kucherenko Oleksandr

Born:September 20, 1979

Platforms: Win32, Linux; - well known and MS-DOS; Win16; OS/2 - old time not touched;

Hardware: IBM PC

Programming Languages: Assembler (for Intel 80386); Borland C/C++; Borland Pascal; Object Pascal; Borland C++Builder; Delphi; Perl; Java; Visual C++; Visual J++; UML; XML/XSL; C#; VB.NET; T-SQL; PL/SQL; and etc.

Development Environments: MS Visual Studio 2001-2008; MS Visual C++; Borland Delphi; Borland C++Builder; C/C++ any; Rational Rose; GDPro; Together and etc.

Libraries: STL, ATL, WTL, MFC, NuMega Driver Works, VCL; .NET 1.0, 1.1, 2.0, 3.5; and etc.

Technologies: Client/Server; COM; DirectX; DirectX Media; BDE; HTML/DHTML; ActiveX; Java Servlets; DCOM; COM+; ADO; CORBA; .NET; Windows Forms; GDI/GDI+; and etc.

Application Skills: Databases - design and maintain, support, programming; GUI Design; System Programming, Security; Business Software Development. Win/Web Services development and etc.

Comments and Discussions