How do you put log calls in constructors when they may be created in a static context?

aliak something at something.com
Fri Aug 10 10:42:33 UTC 2018


On Friday, 10 August 2018 at 09:29:04 UTC, Timoses wrote:
> On Wednesday, 8 August 2018 at 21:54:34 UTC, aliak wrote:
>> I'm trying to debug stuff, so I want to add verbose logging
>>
>> struct S(T) {
>>   this() {
>>     writeln("created S(T) with properties and ID");
>>   }
>> }
>>
>> static a = S!int(); // bah
>>
>> I guess users can call this code from any context, but when 
>> i'd also like to see the log output for debugging purposes. Is 
>> there a way around this?
>>
>> Can I maybe only do a writeln in a non compile-time context?
>>
>> Cheers,
>> - ali
>
> How about debug [1]?
>
> struct S()T {
>     this() {
>         debug { writeln("Created..."); }
>     }
> }
>
> and compile with the debug flag. You can also specify a debug 
> identifier or level.
>
> [1]: https://dlang.org/spec/version.html#debug

Very nice :D Thanks!


More information about the Digitalmars-d-learn mailing list