Click here to Skip to main content
15,879,095 members
Articles / Programming Languages / C++
Article

AMLog, easy logging

Rate me:
Please Sign up or sign in to vote.
3.11/5 (13 votes)
8 Jun 20022 min read 104.7K   954   41   25
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


Written By
Software Developer (Senior)
Denmark Denmark
Huh! Wink | ;-)

Comments and Discussions

 
QuestionCan you specify the license type of this source? Pin
vinothkumar.ekambaram@gmail.com23-Mar-18 2:34
vinothkumar.ekambaram@gmail.com23-Mar-18 2:34 
QuestionImmediate write the log line to the disk without buffering Pin
sdancer7517-May-16 19:46
sdancer7517-May-16 19:46 
QuestionUAC in windows 7 prevents me to save the log file Pin
sdancer7510-May-16 8:16
sdancer7510-May-16 8:16 
GeneralClosing log file in destructor may bring problems. Pin
fatbean.li10-Nov-10 21:57
fatbean.li10-Nov-10 21:57 
GeneralThank you very much! Pin
AndersChen7-Aug-08 21:14
AndersChen7-Aug-08 21:14 
GeneralProblem with minisecond Pin
minhaolee25-Mar-07 22:23
minhaolee25-Mar-07 22:23 
Generalwell done Pin
raylau022-Nov-05 16:12
raylau022-Nov-05 16:12 
Generalvery nice Pin
MGutmann28-Jul-05 23:21
MGutmann28-Jul-05 23:21 
GeneralThere has a good tracing tool, try it now. Pin
autodebug5-Apr-04 14:36
autodebug5-Apr-04 14:36 
GeneralProblem with if -- else Pin
Neville Franks4-Dec-03 15:44
Neville Franks4-Dec-03 15:44 
GeneralRe: Problem with if -- else Pin
Anders Molin5-Dec-03 1:39
professionalAnders Molin5-Dec-03 1:39 
GeneralRe: Problem with if -- else Pin
Neville Franks5-Dec-03 10:05
Neville Franks5-Dec-03 10:05 
GeneralRe: Problem with if -- else Pin
Anders Molin5-Dec-03 13:35
professionalAnders Molin5-Dec-03 13:35 
GeneralRe: Problem with if -- else Pin
jal01235-Feb-04 6:03
jal01235-Feb-04 6:03 
QuestionMultithreaded environment? Pin
10-Jun-02 10:58
suss10-Jun-02 10:58 
AnswerRe: Multithreaded environment? Pin
Anders Molin15-Jun-02 8:21
professionalAnders Molin15-Jun-02 8:21 
GeneralAnother suggestion Pin
Ralph Wetzel9-Jun-02 10:20
Ralph Wetzel9-Jun-02 10:20 
GeneralRe: Another suggestion Pin
Anders Molin15-Jun-02 8:24
professionalAnders Molin15-Jun-02 8:24 
GeneralLog file size Pin
26-May-02 19:11
suss26-May-02 19:11 
GeneralRe: Log file size Pin
Anders Molin26-May-02 21:43
professionalAnders Molin26-May-02 21:43 
GeneralVery Cool, and one suggestion Pin
David Patrick14-May-02 2:41
David Patrick14-May-02 2:41 
GeneralRe: Very Cool, and one suggestion Pin
Anders Molin14-May-02 9:46
professionalAnders Molin14-May-02 9:46 
Thanks for your ideas, I have changed the format of the log today. Now it's more readeable, and takes less space on the disk. Smile | :)

David Patrick wrote:
3) I find it useful to include the source file/line number info on all log entries not just developer info.

Yea, I have been thinking about that, but personally I dont want filenames/linenumbers in my ordinary logfiles. I just want it when I really need it Wink | ;-)
I don't want people need to see what my files are called, and how many lines they have, that's why I have DeveloperInfo, and only tells a user to turn it on if it's really needed...

- Anders

Money talks, but all mine ever says is "Goodbye!"
GeneralRe: Very Cool, and one suggestion Pin
Hugo Hallman13-Jun-03 1:42
Hugo Hallman13-Jun-03 1:42 
GeneralCool Pin
Niklas L14-May-02 2:03
Niklas L14-May-02 2:03 
GeneralRe: Cool Pin
Anders Molin14-May-02 9:41
professionalAnders 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.