Monthly Archives: January 2013

How to use log4cpp with Visual Studio 2012

Since end of december 2012, log4cpp has a support for VS 2010. You can now compile log4cpp without pain using VS 2010 or VS 2012.
Log4cpp is available on sourceforget.net at http://sourceforge.net/projects/log4cpp.
Log4cpp is small and efficient logging library with a support for configuration file with RollingFileAppender, etc.
First, you have to create a configuration file for describing your loggers wich are called appenders.

#file log4cpp.property

log4cpp.rootCategory=DEBUG, rootAppender
log4cpp.category.MyLogger=DEBUG, aLogger

log4cpp.appender.rootAppender=ConsoleAppender
log4cpp.appender.rootAppender.layout=BasicLayout

log4cpp.appender.aLogger=RollingFileAppender
log4cpp.appender.aLogger.fileName=aLogger.log
log4cpp.appender.aLogger.maxFileSize=10240
log4cpp.appender.aLogger.maxBackupIndex=5
log4cpp.appender.aLogger.layout=PatternLayout
log4cpp.appender.aLogger.layout.ConversionPattern=[%H:%M:%S.%l] %m%n

#end of file

Once your file is ready, you have to use the primitives of log4cpp to make traces available in the file name aLogger.log.


class MyLogger
{
public:
 MyLogger()
 {
 }
 virtual ~MyLogger()
 {
  log4cpp::Category::shutdown();
 }
 bool Init()
 {
  try
  {
   string initFileName = "log4cpp.property";
   log4cpp::PropertyConfigurator::configure(initFileName);
  }
  catch(log4cpp::ConfigureFailure& f)
  {
   std::cout << "Configure Problem" << f.what() << std::endl;
   return false;
  }
  return true;
 }
 void LogDebug(string message)
 {
  log4cpp::Category & mylogger = log4cpp::Category::getInstance("MyLogger");
  myLogger.debug(message);
 }
 void Loginfo(string message)
 {
  log4cpp::Category & mylogger = log4cpp::Category::getInstance("MyLogger");
  myLogger.info(message);
 }
};

With this helper class, you can manipulate your logging entries very easily.


int main()
{
 MyLogger log;
 if( !log.Init() )
  return 0;
 log.LogDebug("Enter main...");
 log.LogInfo("Main...");
 log.LogDebug("Exit main");
}

How to compile Apache log4cxx with Visual Studio 2012

I was searching for a suitable logging library. Previously, I was using log4cpp but I want to use the Apache logging library. First you have to compile:
– Apr -> libapr-1.dll
– AprIconv -> libapriconv-1.dll
– AprUtil -> libaprutil-1.dll
– expat -> expat.dll
and then log4cxx -> log4cxx.dll.
The problem is that log4cxx use a macro for defining a vector<> template based stuff. All the macros named LOG4CXX_LIST_DEF should be moved from the end of the headers to the top of the header. There are about 20 entries to move.
Another problem is to remove the namespace from the KeySet class. Every time there is scope::KeySet, you should write KeySet only.
After 1 or 2 hours fixing the errors, you should be ok to recompile this library. Size of log4cxx.dll is 4MB (debug mode).

Compiling console application from command line using CL.EXE

If you want to compile your console applications from command line, just use the CL.EXE command tool. This is the Microsoft compiler. Imagine you have two files : ConsoleApp1 and Function1.cpp. To compile, just use this:

CL /EHsc ConsoleApp1 Function1

The /EHsc flag is about exception handling.

Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80×86
Copyright (C) Microsoft Corporation.  All rights reserved.

consoleapp1.cpp
function1.cpp
Generating Code…
Microsoft (R) Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:consoleapp1.exe
consoleapp1.obj
function1.obj

The result is a console application named consoleapp1.exe.

Compiling MFC from command line using NMAKE

One interesting point using Visual C++ 2010 samples is contained in the MFC folder. You can build mfc samples using a makefile using this command: nmake -a -f makefile. The makefile file just contains your obj files, and then the RC part. Then you #include mfcsamps.mak and that’s all.

Sample:

PROJ=MODELER1
OBJS=AppInit.obj CalendarBar.obj ChildFrm.obj ClassView.obj \
Data.obj \
DrawingContext.obj DrawingElements.obj \
Element.obj ElementContainer.obj ElementFactory.obj ElementManager.obj  \
FileView.obj MainFrm.obj Modeler1.obj Modeler1Doc.obj  \
Modeler1SourceView.obj Modeler1View.obj OutputWnd.obj PropertiesWnd.obj RibbonListButton.obj SourceCodeView.obj TabbedView.obj \
ScintillaCtrl.obj ScintillaDocView.obj

USES_OLE=1

!include <mfcsamps.mak>

# Update the resource if necessary
Modeler1.res: Modeler1.rc resource.h
    rc $(rcflags) $(rcvars) -r -fo Modeler1.res Modeler1.rc

The project is named Modeler1 and the first part of the makefile contains the obj files. You take your cpp files and you write them using the obj extension. Next, the RC part is dedicated for resources.

The MFCSAMPS.MAK file is include into Microsoft Visual Studio\VC\ATLMFC\INCLUDE folder.