![]() |
implement a simple thread-safe debug printf logger under MS VC++
Currently I need to implement a thread-safe application logger under MS Visual C++. The main purpose is to redirect debug_printf and exception strings (thown by C++ classes) to files so I can find out what's wrong with my Win32 service program, and the max log file size can be controlled so that disk space will not be exhausted by too many debug output. __FILE__ , __LINE__ and date/time are
mandatory parameters for the logger class or function. Unfortunately MS C++ compiler is not C99-compatible so it doesn't support variable-argument macros using __VA_ARGS__ as GCC does. After some search, I found some useful info, but some of those implementations are not thread-safe, for example, the following two printf() can be interrupted in multi-threaded environment, and the log file size can't be controlled. http://jxta-c.jxta.org/source/browse/jxta-c/include/Attic/jxta_debug.h?logsort=date&search=&hideattic=1&sortby=file&hidecvsroot=1&diff_format=h&r1=1.2 Code:
#define JXTA_LOG(x) \The above Russian post is quite good, especially the CMyPrintf and MYprintfFN implementations. But it seems that they can't be used in c++ "throw" environment. For example, I want to log the following exception string to file with __FILE__ and __LINE__. Code:
if ((f = fopen(FileName, "rb")) == NULL)I have implement similar logger, but it is not so elegant and can't be used in throw environment either. Code:
class Logger |
Hi,
maybe you can take a look at hxxp://www.codeproject.com/debug/dbg.asp ... ... even if this one doesn't fit your needs, you can take some ideas from there Kerstin |
Thanks for the info.
I read the CDebugPrintf source, it doesn't embed __FILE__/__LINE__ into log lines. My conclusion is that only Macros with a variable number of arguments can do the job. That is, only C99-compatible c++ compilers. Actually I have implemented such a logger with GCC(what a splendid compiler!) I have tested Intel c++ compiler v8.1, it's a pity that its "/Qc99" option only works with pure C programs, neither c++ programs nor c/c++ mixed programs. http://softwareforums.intel.com/ids/board/message?board.id=16&message.id=1643#M1643 I will try to see whether GCC for Windows can compile my MFC program. |
Hi,
May this is helpful (or useless) : xxxx://logging.apache.org/log4cxx/ If you are familiar with Log4j you won't be disappointed. |
| All times are GMT +8. The time now is 14:55. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX