www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - App/lib config system

reply Jacob Carlborg <doob me.com> writes:
I'm looking for a way to configure applications and libraries. 
I'm imagining a way for libraries to hook into a single point to 
provide the configuration parameters the library provides. These 
parameters can then be changed in one location from the user of 
the libraries (usually an application). My main requirement is 
that it should be possible to configure compile time values. I 
think the best way to explain what I'm looking for is with an 
example:

module log;

import config;

void log(Level level, string message)
{
     static if (config.log.isEnabled)
     {
         if (config.log.level >= level)
             writeln(message);
     }
}

In the above example, the code would be part of the "log" 
library. The configuration parameters `config.log.isEnabled` and 
`config.log.level` can be changed by the application that is 
using the "log" library. `config.log.isEnabled` is a compile time 
value, that can only be changed at compile time. 
`config.log.level` is a runtime value that can be changed at 
runtime as well.

Here's a list of my ideal requirements:

* Should support both compile time and runtime config parameters
* Arbitrary libraries should be able to hook into config system 
to provide it with its config parameters
* There should be one place to change the config
* The "config file" should be a regular D module
* It should work with Dub. In the above example the "log" library 
and the application would be in separate Dub packages.

I've done some experiments and I have some pieces working, but 
have not been able to glue everything together. I don't mind if 
there are any dirty tricks behind the scenes but I would like it 
to be as easy as the above example to use it, if possible.

Does anyone have a system like this that is already available?

--
/Jacob Carlborg
Jul 08 2020
parent reply Kagamin <spam here.lot> writes:
If you suspect there's a contradiction in requirements, you need 
to specify them with better precision.
Jul 08 2020
parent reply Jacob Carlborg <doob me.com> writes:
On Thursday, 9 July 2020 at 06:57:22 UTC, Kagamin wrote:
 If you suspect there's a contradiction in requirements, you 
 need to specify them with better precision.
What are the contradictions in the requirements? I don't see any. -- /Jacob Carlborg
Jul 09 2020
next sibling parent Kagamin <spam here.lot> writes:
Contradictions that don't let you glue it all together.
Jul 10 2020
prev sibling parent Kagamin <spam here.lot> writes:
Without contradictions the solution is trivial:

module config;

version(LogEnabled) enum isEnabled=true;
else enum isEnabled=false;

shared int level;
Jul 10 2020