You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-dev@logging.apache.org by Thorsten Schöning <ts...@am-soft.de> on 2010/11/26 13:30:37 UTC

Re: get method name of logging caller...

Guten Tag Thorsten Schöning,
am Freitag, 15. Oktober 2010 um 12:53 schrieben Sie:

> is there any way to get the method name of the logging caller in the
> log message? In some places %M is mentioned, but it doesn't seem to
> work and is not in the documentation of class PatternLayout.

Hello,

%M is not mentioned in the docs of PatternLayout, but it's made
available already. I didn't get any suitable result because, as Curt
Arnold mentioned, my compiler wasn't detected by the macros defining
__LOG4CXX_FUNC__. I changed this in locatininfo.h to recognize the
Borland compiler and %M and %C seem to work as expacted.

I created two patches, one to add Borland compiler in locationinfo.h
and the other to update the documentation of PatternLayout to state
that C and M may be supported, depending on the compiler used. Maybe
you are interested to merge.

Index: locationinfo.h
===================================================================
--- locationinfo.h      (Revision 1657)
+++ locationinfo.h      (Arbeitskopie)
@@ -109,28 +109,32 @@
 
         /** Caller's method name. */
         const char * methodName;
-        
 
+
       };
   }
 }
 
-  #if !defined(LOG4CXX_LOCATION)
-#if defined(_MSC_VER)
-#if _MSC_VER >= 1300
+#if !defined(LOG4CXX_LOCATION)
+  #if defined(_MSC_VER)
+    #if _MSC_VER >= 1300
       #define __LOG4CXX_FUNC__ __FUNCSIG__
-#endif
-#else
-#if defined(__GNUC__)
+    #endif
+  #else
+    #if defined(__GNUC__)
       #define __LOG4CXX_FUNC__ __PRETTY_FUNCTION__
+    #else
+      #if defined(__BORLANDC__)
+        #define __LOG4CXX_FUNC__ __FUNC__
+      #endif
+    #endif
 #endif
-#endif
 #if !defined(__LOG4CXX_FUNC__)
-#define __LOG4CXX_FUNC__ ""
+  #define __LOG4CXX_FUNC__ ""
 #endif
-      #define LOG4CXX_LOCATION ::log4cxx::spi::LocationInfo(__FILE__, \
-           __LOG4CXX_FUNC__,                                                         \
-           __LINE__)
-  #endif
+#define LOG4CXX_LOCATION ::log4cxx::spi::LocationInfo(__FILE__,         \
+                                                      __LOG4CXX_FUNC__, \
+                                                      __LINE__)
+#endif
 
 #endif //_LOG4CXX_SPI_LOCATION_LOCATIONINFO_H


Index: patternlayout.h
===================================================================
--- patternlayout.h     (Revision 1657)
+++ patternlayout.h     (Arbeitskopie)
@@ -104,6 +104,29 @@
         </td>
         </tr>
 
+        <tr>
+          <td align="center">
+            <b>C</b>
+            <p><b>class</b></p>
+          </td>
+
+          <td>
+            Used to output the class of the issuer of the logging event if the
+            compiler used supports a macro to retrieve the method of the
+            currently compiled line and if the LOG4CXX_TRACE-like macros are
+            used to issue a logging request. In this case the macro LOG4CXX_*
+            is expanded at compile time to generate location info of the
+            logging event and adds the method name, besides file and line, if
+            available. In most cases the provided method contains the classname
+            and can therefore be retrieved form the location info as needed.
+
+            <p>
+              Currently supported compilers are those from Microsoft, GNU-C and
+              Borland.
+            </p>
+          </td>
+        </tr>
+
    <tr> <td align=center><b>d</b></td> <td>Used to output the date of
          the logging event. The date conversion specifier may be
          followed by a set of braces containing a
@@ -148,6 +171,30 @@
         </tr>
 
         <tr>
+          <td align="center">
+            <b>M</b>
+            <p><b>method</b></p>
+          </td>
+
+          <td>
+            Used to output the method of the issuer of the logging event if the
+            compiler used supports a macro to retrieve the method of the
+            currently compiled line and if the LOG4CXX_TRACE-like macros are
+            used to issue a logging request. In this case the macro LOG4CXX_*
+            is expanded at compile time to generate location info of the
+            logging event and adds the method name, besides file and line, if
+            available. In most cases the provided method contains the classname
+            which is ignored in every attempt to retrieve the method from the
+            location info.
+
+            <p>
+              Currently supported compilers are those from Microsoft, GNU-C and
+              Borland.
+            </p>
+          </td>
+        </tr>
+
+        <tr>
         <td align=center><b>n</b></td>
 
         <td>Outputs the platform dependent line separator character or


Mit freundlichen Grüßen,

Thorsten Schöning

-- 
Thorsten Schöning
AM-SoFT IT-Systeme - Hameln | Potsdam | Leipzig
 
Telefon: Potsdam: 0331-743881-0
E-Mail:  tschoening@am-soft.de
Web:     http://www.am-soft.de

AM-SoFT GmbH IT-Systeme, Konsumhof 1-5, 14482 Potsdam
Amtsgericht Potsdam HRB 21278 P, Geschäftsführer: Andreas Muchow