Click here to Skip to main content
Click here to Skip to main content

AMLog, easy logging

, 8 Jun 2002
Rate this:
Please Sign up or sign in to vote.
A small utility to make easy logging to files

Introduction

One night when I could not sleep, I was thinking about that I really needed a small utility for logging. It should be easy to use, nothing more than an #include "AMLog.h" in StdAfx.h. I also wanted it to be able to log line-numbers and filenames, so it needed to be done with macro's, and not functions, because I didn't want to write __FILE__ and __LINE__ every time I called a logging function. I also wanted different log-levels, that could be changed by the user. I make a lot of server applications, and if a client has a problem with one of my applications, I just tell him to set the log-level to "Developer Log", and send me the log-file. One last thing I really really wanted was to be able to log using printf-style. All the logging functions supports this, so you can write an log-entry like this:

AMLOGINFO("The value of i is: %d, and s is: %s", i, s) 

It also works with UNICODE, because I do a lot of work with Unicode enabled applications. The logging functions are thread-safe. All logging is synchronized through a Critical Section, so it's completely safe to use in multithreaded applications.

How To Use

AMLog is actually easy to use. Just #include "AMLog.h" in StdAfx.h, and call AMLOG_SETFILENAME("test.log"), and you are ready to go. The AMLOG_SETFILENAME macro just needs a filename, then it append the complete path where the executable file is. Default there is no logging done. AMLog supports four different log-levels that you can shift between by runtime: Nothing, Error, Info and DeveloperInfo. There are 3 different macro's you can use to log stuff to the log-file: AMLOGINFO, AMLOGERROR and AMLOGDEVINFO. AMLOGERROR always writes to the log file if log level is different from Nothing. AMLOGINFO only writes to the log if log level is Info or DeveloperInfo. AMLOGDEVINFO only writes to the log if log-level is DeveloperInfo.

You can use the following macro's to set the loglevel, AMLOG_SETLOGLEVEL_NOTHING, AMLOG_SETLOGLEVEL_ERROR, AMLOG_SETLOGLEVEL_INFO, AMLOG_SETLOGLEVEL_DEVELOPERINFO. Just write AMLOG_SETLOGLEVEL_ERROR; if you just want to log errors.

If you want the stuff you log also to go to the debuggers "out" window, like TRACE() statements do, you just have to define #define AMLOG_TRACE in StdAfx.h, before you include AmLog.h. You can also get the output written to stdout, if you make console applications, just define #define AMLOG_STDOUT.

The Log Format

The log format is pretty basic, it converts all CRLF pairs to a "|" to keep each log entry on a single line. If log-level is DeveloperInfo, source filenames and line numbers are added to the log file for making debugging more easy. Here is the output from the test program included with the source.

Note that the output below has been wrapped to prevent scrolling. The actual output will not wrap.

2002-05-14 21:39:08	Error	"i" is now: 0, Loglevel is: Error
2002-05-14 21:39:08	Error	"i" is now: 1, Loglevel is: Error
2002-05-14 21:39:08	Error	"i" is now: 2, Loglevel is: Error
2002-05-14 21:39:08	Error	"i" is now: 3, Loglevel is: Error
2002-05-14 21:39:08	Error	"i" is now: 4, Loglevel is: Error
2002-05-14 21:39:08	Info	"i" is now: 0, Loglevel is: Info
2002-05-14 21:39:08	Error	"i" is now: 0, Loglevel is: Info
2002-05-14 21:39:08	Info	"i" is now: 1, Loglevel is: Info
2002-05-14 21:39:08	Error	"i" is now: 1, Loglevel is: Info
2002-05-14 21:39:08	Info	"i" is now: 2, Loglevel is: Info
2002-05-14 21:39:08	Error	"i" is now: 2, Loglevel is: Info
2002-05-14 21:39:08	Info	"i" is now: 3, Loglevel is: Info
2002-05-14 21:39:08	Error	"i" is now: 3, Loglevel is: Info
2002-05-14 21:39:08	Info	"i" is now: 4, Loglevel is: Info
2002-05-14 21:39:08	Error	"i" is now: 4, Loglevel is: Info
2002-05-14 21:39:08	Info	amlogtestapp.cpp, 10	
    "i" is now: 0, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	Error	amlogtestapp.cpp, 11	
    "i" is now: 0, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	DevInfo	amlogtestapp.cpp, 12	
    "i" is now: 0, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	Info	amlogtestapp.cpp, 10	
    "i" is now: 1, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	Error	amlogtestapp.cpp, 11	
    "i" is now: 1, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	DevInfo	amlogtestapp.cpp, 12	
    "i" is now: 1, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	Info	amlogtestapp.cpp, 10	
    "i" is now: 2, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	Error	amlogtestapp.cpp, 11	
    "i" is now: 2, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	DevInfo	amlogtestapp.cpp, 12	
    "i" is now: 2, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	Info	amlogtestapp.cpp, 10	
    "i" is now: 3, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	Error	amlogtestapp.cpp, 11	
    "i" is now: 3, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	DevInfo	amlogtestapp.cpp, 12	
    "i" is now: 3, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	Info	amlogtestapp.cpp, 10	
    "i" is now: 4, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	Error	amlogtestapp.cpp, 11	
    "i" is now: 4, Loglevel is: DeveloperInfo
2002-05-14 21:39:08	DevInfo	amlogtestapp.cpp, 12	
    "i" is now: 4, Loglevel is: DeveloperInfo

The last 15 lines is written with Loglevel DeveloperInfo. Well, that's all there is to it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Anders Molin
Software Developer (Senior)
Denmark Denmark
Huh! Wink | ;-)

Comments and Discussions

 
GeneralClosing log file in destructor may bring problems. Pinmemberfatbean.li10-Nov-10 22:57 
GeneralProblem with minisecond Pinmemberminhaolee25-Mar-07 23:23 
Generalwell done Pinmemberraylau022-Nov-05 17:12 
Generalvery nice PinmemberMessnerEW29-Jul-05 0:21 
GeneralThere has a good tracing tool, try it now. Pinmemberautodebug5-Apr-04 15:36 
GeneralProblem with if -- else PinmemberNeville Franks4-Dec-03 16:44 
GeneralRe: Problem with if -- else PinmemberAnders Molin5-Dec-03 2:39 
GeneralRe: Problem with if -- else PinmemberNeville Franks5-Dec-03 11:05 
GeneralRe: Problem with if -- else PinmemberAnders Molin5-Dec-03 14:35 
GeneralRe: Problem with if -- else Pinmemberjal01235-Feb-04 7:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 9 Jun 2002
Article Copyright 2002 by Anders Molin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid