www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Converting from string to enum by name

reply "Jarl =?UTF-8?B?QW5kcsOpIg==?= <jarl.andre gmail.com> writes:
Hi

I have a project on github, 
https://github.com/jarlah/d2-simple-socket-server, where I have 
added very custom logger library. In this logger library I have 
an enum LogLevel that looks like enum LogLevel { ALL, INFO, 
WARNING etc }

Questions:

1. Is there a way to convert from string "INFO" to LogLevel.INFO, 
by name?
2. does it really not exist any functional logging libraries for 
D2?

I have made a very crude system where I specify the LogLevel that 
I want to be printed to stdout and have not yet begun to compare 
if loglevel is greater than or equal to whatever. thats my next 
assignment.
May 28 2012
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/28/2012 11:50 AM, "Jarl André" <jarl.andre gmail.com>" wrote:

 1. Is there a way to convert from string "INFO" to LogLevel.INFO, by 
name? conv.to can do that: import std.conv; enum LogLevel { ALL, INFO, WARNING } void main() { enum l = to!LogLevel("INFO"); assert(l == LogLevel.INFO); }
 2. does it really not exist any functional logging libraries for D2?
There has been discussions about that not too long ago. Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
May 28 2012
parent "Jarl =?UTF-8?B?QW5kcsOpIg==?= <jarl.andre gmail.com> writes:
On Monday, 28 May 2012 at 18:53:50 UTC, Ali Çehreli wrote:
 On 05/28/2012 11:50 AM, "Jarl André" <jarl.andre gmail.com>" 
 wrote:

 1. Is there a way to convert from string "INFO" to
LogLevel.INFO, by name? conv.to can do that: import std.conv; enum LogLevel { ALL, INFO, WARNING } void main() { enum l = to!LogLevel("INFO"); assert(l == LogLevel.INFO); }
 2. does it really not exist any functional logging libraries
for D2? There has been discussions about that not too long ago. Ali
Thanks. I have now implemented a logging library that actually looks to imitate log4j. If I log to any level then it checks if the current logLevel is equal to or lesser than the incoming loglevel. It might be using the wrong scale, e.g. ALL is 6 and OFF is 0, but it works. And its not really log4j so who cares ;)
May 29 2012