Platform independent extensible log class

Environment: VC6, BC55, Win2000 SP2, g++, Redhat Linux 7.1

Introduction:

The ability to log is commonly needed in every software project on every platform. I wrote this class to save time.

There are two basic log classes provided for easy use. One is CFileLog, which implements a file logging system. The other is CRegFileLog that implements a registry controled file logging system. The whole logging system is quit easy to extended for any propose.

How to use:

Just include MjLog.h and construct an MjTools::CFileLog or MjTools::CRegFileLog instance. When you want to add a log message, call yourObject.AddLog("Log message").

Demo code:

#include "MjLog.h"
int main()
{
  MjTools::CFileLog m_Log("test.log");
  std::string a="aaa";
  m_Log.Clear();
  m_Log.AddLog("Abc");
  m_Log.AddLog(a);
  MjTools::CFileLog m_Log1=m_Log;
  m_Log1.AddLog("From Log1");
#ifdef WIN32 //RegistryLogControl only valid in Windows system
  // construct a registry key controled log object. If 
  // the specified registry key is found,the log is enabled
  MjTools::CRegFileLog m_regLog("reglog.log",
         "HKEY_LOCAL_MACHINE\\Software\\YourControlKeyName");
  m_regLog.AddLog("reglog");
  m_regLog.Pause();
  m_regLog.AddLog("reglog1");
  m_regLog.Resume();
  m_regLog.AddLog("reglog2");
#endif
  return 0;
}

How to compile:

The source code itself can be compiled and executed. You can use command line tool to compile it.

Under VC++:

CL /D"_TEST_" MjLog.cpp 

This one may cause a link error. I don't know why.But if you use a win32 console project, no error occurs

Under BCC:

bcc32 /D_TEST_ mjlog.cpp

Under Linux:

g++ /D_TEST_ MjLog.cpp

Future Updates:

1. Make the class thread_safe.
2. Still thinking...

Downloads

Download source - 2.41 Kb


Comments

  • very crude solution

    Posted by Legacy on 07/24/2003 12:00am

    Originally posted by: Matthew Pasko

    Keep in mind a quick solution to this type of problem would be to simply find the log file (FindFirstFile (CE)), look at the file size, and if the file size is half of the total space of how big the log should grow, move the file name (rename) the file to an archive log file. As time progresses, the old data overwritten and the log file(s) grows only so big.

    Obvious crude solution, but effective for some applications.. and a snap to write.
    ;)

    Reply
  • growth of log, over write old info!

    Posted by Legacy on 04/16/2002 12:00am

    Originally posted by: Matthew Pasko

    There is often limited space on a device for logging. How about function to limit growth size of the log to be adjustable and over writes old log information?
    This would be great..


    Reply
  • fix for link errors (VC++)

    Posted by Legacy on 02/06/2002 12:00am

    Originally posted by: Edmond Nolan

    Problem
    
    Under VC++:

    CL /D"_TEST_" MjLog.cpp

    This one may cause a link error. I don't know why.
    But if you use a win32 console project, no error occurs


    create the following environment variables

    INCLUDE \PATH\TO\HEADER\FILES
    LIB \PATH\TO\LIB\FILES

    use:

    CL /D"_TEST_" -GX MjLog.cpp

    to avoid: warning C4530: C++ exception handler used, but unwind semantics are not enabled

    Reply
  • Why would you want to add "Logging" overhead to the Registry?

    Posted by Legacy on 12/09/2001 12:00am

    Originally posted by: Hector Santos

    Maybe I miss something here, but why would you want to add "Logging" overhead to the Registry?

    Just curious

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: May 7, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT This eSeminar will explore three popular games engines and how they empower developers to create exciting, graphically rich, and high-performance games for Android® on Intel® Architecture. Join us for a deep dive as experts describe the features, tools, and common challenges using Marmalade, App Game Kit, and Havok game engines, as well as a discussion of the pros and cons of each engine and how they fit into your development …

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't let …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds