Bug 1552 - Storing log name inside LogComponent class (NS_LOG) as std::string
Storing log name inside LogComponent class (NS_LOG) as std::string
Status: RESOLVED FIXED
Product: ns-3
Classification: Unclassified
Component: core
ns-3-dev
All All
: P5 enhancement
Assigned To: Mathieu Lacage
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2012-12-12 14:03 EST by Alex Afanasyev
Modified: 2013-10-28 17:51 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Afanasyev 2012-12-12 14:03:27 EST
I'm using a template-based model implementations, where in order to make logging work I need to use a little bit more complex logging initialization.  In particular, I have a static method in a class that constructs name on the fly, based on names of parent classes.  (You can check https://github.com/NDN-Routing/ndnSIM/blob/master/model/fw/per-fib-limits.h for my a slightly crazy example of that.)

Currently, when LogComponent is initialized, it stores a supplied pointer const char* to a member variable, without actually copying the string.  In all current cases this is fine (as it points to a static data), but in my case---dynamic string creationg---that caused a disaster.

So, my suggestion is to just change an internally stored type in LogComponent to std::string, which involves two small changes:

change in log.h
@@ -394,7 +394,7 @@ public:
 private:
   std::map<enum LogLevel, std::string> LevelLabels() const;
   int32_t     m_levels;
-  char const *m_name;
+  std::string m_name;
 };

change in log.cc
@@ -261,7 +261,7 @@ LogComponent::Disable (enum LogLevel level)
 char const *
 LogComponent::Name (void) const
 {
-  return m_name;
+  return m_name.c_str ();
 }

Do you think it is a reasonable change?
Comment 1 Mathieu Lacage 2012-12-12 15:01:56 EST
(In reply to comment #0)

> Do you think it is a reasonable change?

it looks reasonable. I can't find out what could go wrong here.
Comment 2 Peter Barnes 2013-10-28 17:51:01 EDT
Almost that simple.

Commit 8244f0b3ff1b