1

I use log4cplus-1.1.2 in Linux. It seems that no way to set thread_name and thread_name2 in struct per_thread_data.

The definition of struct per_thread_data as followed in file internal.h at line 111-130:

//! Per thread data.
struct per_thread_data
{
    per_thread_data ();
    ~per_thread_data ();

    tostringstream macros_oss;
    tostringstream layout_oss;
    DiagnosticContextStack ndc_dcs;
    MappedDiagnosticContextMap mdc_map;
    log4cplus::tstring thread_name;
    log4cplus::tstring thread_name2;
    gft_scratch_pad gft_sp;
    appender_sratch_pad appender_sp;
    log4cplus::tstring faa_str;
    log4cplus::tstring ll_str;
    spi::InternalLoggingEvent forced_log_ev;
    std::FILE * fnull;
    log4cplus::helpers::snprintf_buf snprintf_buf;
};

log4cplus will allocate such struct for each thread for logging.

Call LOG4CPLUS_EXPORT log4cplus::tstring const & getCurrentThreadName() and LOG4CPLUS_EXPORT log4cplus::tstring const & getCurrentThreadName2() are the only way to access thread_name and thread_name2 respectively. However, the user cannot modify they values.

wilx
  • 17,697
  • 6
  • 59
  • 114
sfzhang
  • 669
  • 9
  • 18
  • It seems to be better that the return type of `getCurrentThreadName()` and `getCurrentThreadName2()` should not be `const`, and the users can modify them. – sfzhang Aug 22 '14 at 07:59
  • That is an interesting idea. If you implement a reasonable change/patch with this feature, I will include it in log4cplus. Instead of using non-const reference, implement it as a `setCurrentThreadName{,2}()`. – wilx Aug 22 '14 at 08:35
  • OK! I will try it. @VáclavZeman – sfzhang Aug 22 '14 at 08:46
  • It would be best if you forked [log4cplus Github](https://github.com/log4cplus/log4cplus) and provided your patch as a pull request there. – wilx Aug 22 '14 at 09:16

1 Answers1

0

Setting thread name in log4cplus is now possible, since log4cplus 1.1.3-RC4 and since log4cplus 1.2.0-RC3 by using log4cplus::thread::setCurrentThreadName() and log4cplus::thread::setCurrentThreadName2().

wilx
  • 17,697
  • 6
  • 59
  • 114