www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Namespace clash between modules

reply Marek Janukowicz <marek janukowicz.net> writes:
I implemented some generic logging module and want to use single Logger 
object per module. Some simplified excerpt from my code:

module log;
mixin template makeLogger( params,,, name ) {
  Logger logger;
  static this () {
  .. logger initialization...
  }
}

-------------

module another;
import log;

mixin makeLogger!( ...., __MODULE__ );
logger( DEBUG, "aksjfakjdf" );

-------------

module yet_another;
import log;

mixin makeLogger!( ...., __MODULE__ );
logger( INFO, "sdfsdfsdf" );

This works quite well, but if I import one module into another I get an 
obvious name clash on the variable name "logger". I have many modules where 
I want to use logging, so things like static import are not an option.

Is there any pattern I could use here?

-- 
Marek Janukowicz
Aug 13 2013
next sibling parent reply "evilrat" <evilrat666 gmail.com> writes:
On Tuesday, 13 August 2013 at 16:10:19 UTC, Marek Janukowicz 
wrote:
 I implemented some generic logging module and want to use 
 single Logger
 object per module. Some simplified excerpt from my code:

 module log;
 mixin template makeLogger( params,,, name ) {
   Logger logger;
   static this () {
   .. logger initialization...
   }
 }

 -------------

 module another;
 import log;

 mixin makeLogger!( ...., __MODULE__ );
 logger( DEBUG, "aksjfakjdf" );

 -------------

 module yet_another;
 import log;

 mixin makeLogger!( ...., __MODULE__ );
 logger( INFO, "sdfsdfsdf" );

 This works quite well, but if I import one module into another 
 I get an
 obvious name clash on the variable name "logger". I have many 
 modules where
 I want to use logging, so things like static import are not an 
 option.

 Is there any pattern I could use here?
is it necessary use multiple loggers? maybe you should move ur Logger instance to module scope(log module) and just handle log sources some other way(i.e. add source string mapping in log module)? because i mean this clash is just due to multiple instantions of this template
Aug 13 2013
next sibling parent Marek Janukowicz <marek janukowicz.net> writes:
evilrat wrote:
 is it necessary use multiple loggers?
Of course - as the whole thing is about logging, which is not a critical part of the application - I could try another approach. However, I'd like to have per-module logger, because it's convenient to manage them this way (eg. I often need to change log level for particular module or I use different colors for a module).
 maybe you should move ur
 Logger instance to module scope(log module) and just handle log
 sources some other way(i.e. add source string mapping in log
 module)?
It would be a viable solution, although definitely it would be more complicated than what I have now.
 because i mean this clash is just due to multiple
 instantions of this template
Yes, I realize that multiple instantiations are the reason for this clash. However, being a D newbie I really wanted to ask the list first, hoping there might be some simple solution I don't know about. -- Marek Janukowicz
Aug 13 2013
prev sibling parent "evilrat" <evilrat666 gmail.com> writes:
On Tuesday, 13 August 2013 at 17:14:53 UTC, evilrat wrote:
 module log;
 mixin template makeLogger( params,,, name ) {
  Logger logger;
  static this () {
  .. logger initialization...
  }
 }
is it necessary use multiple loggers? maybe you should move ur Logger instance to module scope(log module) and just handle log sources some other way(i.e. add source string mapping in log module)? because i mean this clash is just due to multiple instantions of this template
omg sorry, i'm an idiot. i should stop posting stupud things when i already going to sleep. sorry, ur problem is circular dependencies, you import modules with static ctor which imports each other. to avoid this, remove module ctor or place it in log module for good.
Aug 13 2013
prev sibling next sibling parent "Dicebot" <public dicebot.lv> writes:
Sad that private symbols clash too because that would have been a 
proper solution here. Can't imagine any clear alternative right 
now.
Aug 13 2013
prev sibling parent "Jesse Phillips" <Jesse.K.Phillips+D gmail.com> writes:
On Tuesday, 13 August 2013 at 16:10:19 UTC, Marek Janukowicz 
wrote:
 I implemented some generic logging module and want to use 
 single Logger
 object per module. Some simplified excerpt from my code:

 module log;
 mixin template makeLogger( params,,, name ) {
   Logger logger;
   static this () {
   .. logger initialization...
   }
 }
I would expect that even if you solve the name clash you'll run into issue with having a static this() in every module. (in some situations D can't decide which static this() to execute first.)
Aug 13 2013