Click here to Skip to main content
12,445,274 members (52,787 online)
Click here to Skip to main content
Add your own
alternative version

Stats

88.3K views
844 downloads
42 bookmarked
Posted

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 | ;-)

You may also be interested in...

Pro
Pro

Comments and Discussions

 
QuestionImmediate write the log line to the disk without buffering Pin
sdancer7517-May-16 19:46
membersdancer7517-May-16 19:46 
QuestionUAC in windows 7 prevents me to save the log file Pin
sdancer7510-May-16 8:16
membersdancer7510-May-16 8:16 
GeneralClosing log file in destructor may bring problems. Pin
fatbean.li10-Nov-10 21:57
memberfatbean.li10-Nov-10 21:57 
GeneralProblem with minisecond Pin
minhaolee25-Mar-07 22:23
memberminhaolee25-Mar-07 22:23 
Generalwell done Pin
raylau022-Nov-05 16:12
memberraylau022-Nov-05 16:12 
Generalvery nice Pin
MessnerEW28-Jul-05 23:21
memberMessnerEW28-Jul-05 23:21 
GeneralThere has a good tracing tool, try it now. Pin
autodebug5-Apr-04 14:36
memberautodebug5-Apr-04 14:36 
GeneralProblem with if -- else Pin
Neville Franks4-Dec-03 15:44
memberNeville Franks4-Dec-03 15:44 
GeneralRe: Problem with if -- else Pin
Anders Molin5-Dec-03 1:39
memberAnders Molin5-Dec-03 1:39 
GeneralRe: Problem with if -- else Pin
Neville Franks5-Dec-03 10:05
memberNeville Franks5-Dec-03 10:05 
GeneralRe: Problem with if -- else Pin
Anders Molin5-Dec-03 13:35
memberAnders Molin5-Dec-03 13:35 
GeneralRe: Problem with if -- else Pin
jal01235-Feb-04 6:03
memberjal01235-Feb-04 6:03 
QuestionMultithreaded environment? Pin
A Reader10-Jun-02 10:58
memberA Reader10-Jun-02 10:58 
AnswerRe: Multithreaded environment? Pin
Anders Molin15-Jun-02 8:21
memberAnders Molin15-Jun-02 8:21 
GeneralAnother suggestion Pin
Ralph Wetzel9-Jun-02 10:20
memberRalph Wetzel9-Jun-02 10:20 
GeneralRe: Another suggestion Pin
Anders Molin15-Jun-02 8:24
memberAnders Molin15-Jun-02 8:24 
GeneralLog file size Pin
Remco Overman26-May-02 19:11
memberRemco Overman26-May-02 19:11 
GeneralRe: Log file size Pin
Anders Molin26-May-02 21:43
memberAnders Molin26-May-02 21:43 
GeneralVery Cool, and one suggestion Pin
David Patrick14-May-02 2:41
memberDavid Patrick14-May-02 2:41 
GeneralRe: Very Cool, and one suggestion Pin
Anders Molin14-May-02 9:46
memberAnders Molin14-May-02 9:46 
GeneralRe: Very Cool, and one suggestion Pin
Hugo Hallman13-Jun-03 1:42
sussHugo Hallman13-Jun-03 1:42 
GeneralCool Pin
Niklas Lindquist14-May-02 2:03
memberNiklas Lindquist14-May-02 2:03 
GeneralRe: Cool Pin
Anders Molin14-May-02 9:41
memberAnders Molin14-May-02 9:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.160811.3 | Last Updated 9 Jun 2002
Article Copyright 2002 by Anders Molin
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid