Click here to Skip to main content
15,884,237 members
Articles / Programming Languages / C++

LogDriver - A simplified log4j using C++

Rate me:
Please Sign up or sign in to vote.
4.73/5 (7 votes)
4 Jan 2002BSD5 min read 188.7K   3.5K   63  
Some Logging Classes for C++ on the Windows Platform
// $Log: LogAppender.cpp,v $
// Revision 1.4  2002-01-05 11:02:58-06  wsenn
// fixed copyright year
//
// Revision 1.3  2002-01-05 10:44:30-06  wsenn
// added copyright and permission notice
//
// Revision 1.2  2001-11-14 21:39:25-06  wsenn
// added formatMessage method
// and setSource/getSource methods
//
// Revision 1.1  2001-11-13 19:28:26-06  wsenn
// added CConsoleLogAppender and changed the vector to a vector of pointers
// for downstream polymorphism
//
// Revision 1.0  2001-11-11 14:16:39-06  wsenn
// Initial revision
//
//
// $Id: LogAppender.cpp,v 1.4 2002-01-05 11:02:58-06 wsenn Exp wsenn $
//
// COPYRIGHT AND PERMISSION NOTICE
//
// Copyright (c) 2002 Will Senn
//
// All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, provided that the above
// copyright notice(s) and this permission notice appear in all copies of
// the Software and that both the above copyright notice(s) and this
// permission notice appear in supporting documentation.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
// OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL
// INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING
// FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
// NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
// WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// Except as contained in this notice, the name of a copyright holder
// shall not be used in advertising or otherwise to promote the sale, use
// or other dealings in this Software without prior written authorization
// of the copyright holder.
//

#include <string>
#include <ctime>
#include <cstdio>

#include "../inc/LogAppender.h"

using namespace std;

CLogAppender::CLogAppender() {
	mThreshold = nsCLog::info;
	mStrSource = "unknown";
	mStrFormat = "%d_%t| %s | %e | %m";	//"mmddyyyy_hhmmss.mss: severity | source | message"
}

//commonly used constructor
CLogAppender::CLogAppender(nsCLog::eSeverity threshold, string strFormat) {
	mThreshold = threshold;
	mStrSource = "unknown";
	mStrFormat = strFormat;
}

//destructor defined in header
CLogAppender::~CLogAppender() {
	//todo
}

bool CLogAppender::formatMessage(nsCLog::eSeverity severity, string message, string & outstring) {
	//string outstring = mStrFormat;

	int nDatePosition,
		nTimePosition,
		nSeverityPosition,
		nSourcePosition,
		nMessagePosition;
	
    struct tm *today;
    time_t ltime;

	char datestr[80];
	char timestr[80];

	time( &ltime );
	today = localtime( &ltime );
	sprintf(datestr, "%02d%02d%04d",
		today->tm_mon + 1,
		today->tm_mday,
		today->tm_year + 1900);
	sprintf(timestr, "%02d%02d%02d",
		today->tm_hour,
		today->tm_min,
		today->tm_sec);

	outstring = mStrFormat;
	if(string::npos != (nDatePosition = outstring.find("%d"))) {
		//gotta date tag at nDatePosition
		outstring.replace(nDatePosition, 2, datestr);
	}
	if(string::npos != (nTimePosition = outstring.find("%t"))) {
		//gotta time tag at nDatePosition
		outstring.replace(nTimePosition, 2, timestr);
	}
	if(string::npos != (nSeverityPosition = outstring.find("%e"))) {
		//gotta severity tag at nDatePosition
		string strSeverity;
		switch(severity) {
			case nsCLog::debug:
				strSeverity = "debug  ";
				break;
			case nsCLog::info:
				strSeverity = "info   ";
				break;
			case nsCLog::warning:
				strSeverity = "warning";
				break;
			case nsCLog::error:
				strSeverity = "error  ";
				break;
			case nsCLog::fatal:
				strSeverity = "fatal  ";
				break;
			default:
				strSeverity = "unknown";
				break;
		}
		outstring.replace(nSeverityPosition, 2, strSeverity);
	}
	if(string::npos != (nSourcePosition= outstring.find("%s"))) {
		//gotta source tag at nDatePosition
		outstring.replace(nSourcePosition, 2, mStrSource);
	}
	if(string::npos != (nMessagePosition= outstring.find("%m"))) {
		//gotta message tag at nDatePosition
		outstring.replace(nMessagePosition, 2, message);
	}

//		printf("%s\n", instring.c_str());
//	cout << outstring << endl;

	return true;
}

//accessors
string CLogAppender::getSource() {
	return mStrSource;
}

//mutators
bool CLogAppender::setSource(string & source) {
	mStrSource = source;
	return true;
}

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 BSD License


Written By
Instructor / Trainer Tarleton State University
United States United States
Will Currently serves as Assistant Professor of Computer Information Systems. Previously he led research, development and product delivery efforts on a global scale. He has a long career as a software architect and developer and enjoys programming in any language on any platform for any good purpose.

Comments and Discussions