{"id":1351,"date":"2012-04-01T13:40:10","date_gmt":"2012-04-01T11:40:10","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=1351"},"modified":"2012-04-01T13:46:14","modified_gmt":"2012-04-01T11:46:14","slug":"enabling-atltrace-output-in-release-configuration-builds","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/1351","title":{"rendered":"Enabling ATLTRACE output in Release configuration builds"},"content":{"rendered":"<p>The original intent is pretty clear, as <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dhxsse89.aspx\">MSDN states<\/a>:<\/p>\n<blockquote><p>In release builds, <strong>ATLTRACE2<\/strong> compiles to <strong>(void) 0<\/strong>.<\/p><\/blockquote>\n<p>As simple as this, but once in a while you are in a situation where release build fails to work for unknown reason and you need additional information for troubleshooting, and then you remember that you had debug tracing code still nicely available in the source, it is just being stripped out by definition of <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6xkxyz08.aspx\">ATLTRACE<\/a>\/<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dhxsse89.aspx\">ATLTRACE2<\/a> macros for release builds.<\/p>\n<p>To avoid reinvention of the wheel and putting new tracing, it might make sense to just re-enable existing tracing (certainly, if putting debug build binary is out of question, which might be the case in production environment and\/or to avoid the hassle of installing additional runtime).<\/p>\n<p>The macros need to be #undef&#8217;ined and redefined appropriately with or without limiting scope by <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/2bs04eza%28v=vs.100%29.aspx\">push_macro<\/a>\/pop_macro #pragma&#8217;s. The trick with macro has to reach two goals, to pick file name, line and current symbol name using <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/b0084kay%28v=VS.71%29.aspx\">__FILE__<\/a> and friend macros, and also accept variable number of arguments.<\/p>\n<p>The trick ATL does and we can use too is to define a helper class, with constructor taking file name, line and symbol name values, and cast operator () taking actual tracing parameters and arguments. Internally the output can be mapped to <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa363362%28v=vs.85%29.aspx\">OutputDebugString <\/a>API so that output could be seen using external tool such as <a href=\"http:\/\/technet.microsoft.com\/en-us\/sysinternals\/bb896647\">DebugView<\/a>.<\/p>\n<p>When everything is well set, new the macros in question can be defined as follows:<\/p>\n<pre style=\"color: #000000; background: #ffffff;\"><span style=\"color: #004a43;\">#<\/span><span style=\"color: #004a43; font-weight: bold;\">pragma <\/span><span style=\"color: #bb7977; font-weight: bold;\">push_macro(<\/span><span style=\"color: #0000e6; font-weight: bold;\">\"ATLTRACE\"<\/span><span style=\"color: #bb7977; font-weight: bold;\">)<\/span>\r\n<span style=\"color: #004a43;\">#<\/span><span style=\"color: #004a43; font-weight: bold;\">pragma <\/span><span style=\"color: #bb7977; font-weight: bold;\">push_macro(<\/span><span style=\"color: #0000e6; font-weight: bold;\">\"ATLTRACE2\"<\/span><span style=\"color: #bb7977; font-weight: bold;\">)<\/span>\r\n\r\n<span style=\"color: #004a43;\">#<\/span><span style=\"color: #004a43;\">undef<\/span><span style=\"color: #004a43;\"> ATLTRACE<\/span>\r\n<span style=\"color: #004a43;\">#<\/span><span style=\"color: #004a43;\">undef<\/span><span style=\"color: #004a43;\"> ATLTRACE2<\/span>\r\n\r\n<span style=\"color: #004a43;\">#<\/span><span style=\"color: #004a43;\">define<\/span><span style=\"color: #004a43;\"> ATLTRACE2 CAtlTrace<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #004a43;\">__FILE__<\/span><span style=\"color: #808030;\">,<\/span><span style=\"color: #004a43;\"> __LINE__<\/span><span style=\"color: #808030;\">,<\/span><span style=\"color: #004a43;\"> __FUNCTION__<\/span><span style=\"color: #808030;\">)<\/span>\r\n<span style=\"color: #004a43;\">#<\/span><span style=\"color: #004a43;\">define<\/span><span style=\"color: #004a43;\"> ATLTRACE ATLTRACE2<\/span>\r\n\r\n<span style=\"color: #800000; font-weight: bold;\">int<\/span> _tmain<span style=\"color: #808030;\">(<\/span><span style=\"color: #800000; font-weight: bold;\">int<\/span> argc<span style=\"color: #808030;\">,<\/span> _TCHAR<span style=\"color: #808030;\">*<\/span> argv<span style=\"color: #808030;\">[<\/span><span style=\"color: #808030;\">]<\/span><span style=\"color: #808030;\">)<\/span>\r\n<span style=\"color: #800080;\">{<\/span>\r\n    ATLTRACE<span style=\"color: #808030;\">(<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #0000e6;\">First: <\/span><span style=\"color: #0f69ff;\">%d<\/span><span style=\"color: #0f69ff;\">\\n<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #808030;\">,<\/span> __LINE__<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\n    ATLTRACE<span style=\"color: #808030;\">(<\/span><span style=\"color: #800000;\">L\"<\/span><span style=\"color: #0000e6;\">Second: <\/span><span style=\"color: #0f69ff;\">%d<\/span><span style=\"color: #0f69ff;\">\\n<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #808030;\">,<\/span> __LINE__<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\n    ATLTRACE2<span style=\"color: #808030;\">(<\/span>atlTraceGeneral<span style=\"color: #808030;\">,<\/span> <span style=\"color: #008c00;\">2<\/span><span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000;\">\"<\/span><span style=\"color: #0000e6;\">Third: <\/span><span style=\"color: #0f69ff;\">%d<\/span><span style=\"color: #0f69ff;\">\\n<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #808030;\">,<\/span> __LINE__<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\n    ATLTRACE2<span style=\"color: #808030;\">(<\/span>atlTraceGeneral<span style=\"color: #808030;\">,<\/span> <span style=\"color: #008c00;\">2<\/span><span style=\"color: #808030;\">,<\/span> <span style=\"color: #800000;\">L\"<\/span><span style=\"color: #0000e6;\">Fourth: <\/span><span style=\"color: #0f69ff;\">%d<\/span><span style=\"color: #0f69ff;\">\\n<\/span><span style=\"color: #800000;\">\"<\/span><span style=\"color: #808030;\">,<\/span> __LINE__<span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\n    <span style=\"color: #800000; font-weight: bold;\">return<\/span> <span style=\"color: #008c00;\">0<\/span><span style=\"color: #800080;\">;<\/span>\r\n<span style=\"color: #800080;\">}<\/span>\r\n\r\n<span style=\"color: #004a43;\">#<\/span><span style=\"color: #004a43; font-weight: bold;\">pragma <\/span><span style=\"color: #bb7977; font-weight: bold;\">pop_macro(<\/span><span style=\"color: #0000e6; font-weight: bold;\">\"ATLTRACE2\"<\/span><span style=\"color: #bb7977; font-weight: bold;\">)<\/span>\r\n<span style=\"color: #004a43;\">#<\/span><span style=\"color: #004a43; font-weight: bold;\">pragma <\/span><span style=\"color: #bb7977; font-weight: bold;\">pop_macro(<\/span><span style=\"color: #0000e6; font-weight: bold;\">\"ATLTRACE\"<\/span><span style=\"color: #bb7977; font-weight: bold;\">)<\/span><\/pre>\n<p>And the Release configuration output will be:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1352\" title=\"DebugView Output\" src=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2012\/04\/Image001.png\" alt=\"\" width=\"772\" height=\"343\" srcset=\"https:\/\/alax.info\/blog\/wp-content\/uploads\/2012\/04\/Image001.png 772w, https:\/\/alax.info\/blog\/wp-content\/uploads\/2012\/04\/Image001-320x142.png 320w\" sizes=\"auto, (max-width: 772px) 100vw, 772px\" \/><\/p>\n<p>Visual C++ .NET 2010 <a href=\"https:\/\/www.alax.info\/trac\/public\/browser\/trunk\/Utilities\/AtlTrace\">source code<\/a> is available from SVN; in particular <strong>CAtlTrace<\/strong> class is <a href=\"https:\/\/www.alax.info\/trac\/public\/browser\/trunk\/Utilities\/AtlTrace\/AtlTrace.cpp#L9\">here<\/a>.<\/p>\n<p>Bonus reading:<\/p>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/2hfwwtbs%28v=vs.100%29.aspx\">Debugging and Error Reporting Macros<\/a><\/li>\n<li><a href=\"http:\/\/www.cprogramming.com\/tutorial\/visual_studio_trace_log_messages.html\">Debugging with Visual Studio 2005\/2008: Logging and Tracing<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The original intent is pretty clear, as MSDN states: In release builds, ATLTRACE2 compiles to (void) 0. As simple as this, but once in a while you are in a situation where release build fails to work for unknown reason and you need additional information for troubleshooting, and then you remember that you had debug&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/1351\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,13],"tags":[487,38,218,409,410],"class_list":["post-1351","post","type-post","status-publish","format-standard","hentry","category-atl","category-source","tag-atl","tag-c","tag-debug","tag-outputdebugstring","tag-release"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/1351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=1351"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/1351\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=1351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=1351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=1351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}