digitalmars.D.learn - Namespace clash between modules
- Marek Janukowicz (25/25) Aug 13 2013 I implemented some generic logging module and want to use single Logger
- evilrat (7/34) Aug 13 2013 is it necessary use multiple loggers? maybe you should move ur
- Marek Janukowicz (13/20) Aug 13 2013 Of course - as the whole thing is about logging, which is not a critical...
- evilrat (6/20) Aug 13 2013 omg sorry, i'm an idiot. i should stop posting stupud things when
- Dicebot (3/3) Aug 13 2013 Sad that private symbols clash too because that would have been a
- Jesse Phillips (5/15) Aug 13 2013 I would expect that even if you solve the name clash you'll run
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
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
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 templateYes, 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
On Tuesday, 13 August 2013 at 17:14:53 UTC, evilrat wrote: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.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
Aug 13 2013
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
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