www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.log version 2

reply Jose Armando Garcia <jsancio gmail.com> writes:
Hi everyone,

I went back and incorporated a lot of the community requested changes.
The module has changed quite a bit. Some notable changes are:

* Got rid of all the *Config struct. You can now configure some parts
of std.log even after logging as started. See Configuration
* Got rid of the initLogging methods/templates they are not need
anymore. The module should work without initialization.
* Added the log severity critical which throws when logged.
* Gave in and made LogFilter a final class
* Fixed compile time disabling by using a template to get the
LogFilter. The new syntax is log!info.
* Added rich boolean support! More on that below.

code: https://github.com/jsancio/phobos/blob/master/std/log.d
doc: http://jsancio.github.com/phobos/phobos/std_log.html

Note: I had to make changes to std.datetime (was getting a segfault;
changes are in my repo) and druntime (patch attached).

Now on to rich boolean. I wanted to be able to print in the log
message why the when() expression is true. To achieve this I added a
struct Rich(Type)
which encapsulate the value and the reason for that value. Right now
the implementation is not every smart and just stores the reason as a
string.

For example if you write:

log!info.format("You passed %s argument(s)", args.length - 1);
log!info.when(richGreater(args.length, 1))("Arguments: ", args[1 .. $]);
log!info.when(richOr(richIsNull(obj), richEqual(10, 20)))("Something strange");

you get the following log lines:

20110604T152711.0138317Z:93d780 src/justo/main.d:11 INFO You passed 4
argument(s)
20110604T152711.0142393Z:93d780 src/justo/main.d:12 INFO when(true =
(a > b) <a = '5', b = '1'>) Arguments: [--minloglevel=info,
--logtostderr, --stderrThreshold=info, --v=0]
20110604T152711.0150535Z:93d780 src/justo/main.d:14 INFO when(true =
(a || b) <a = 'true = (a is null) <a = 'null'>', b = 'false = (a == b)
<a = '10', b = '20'>'>) Something strange!

I am interested to hear people's option of this. How can it be improved?

Many thanks,
-Jose
Jun 04 2011
next sibling parent reply KennyTM~ <kennytm gmail.com> writes:
On Jun 5, 11 01:04, Jose Armando Garcia wrote:
 Now on to rich boolean. I wanted to be able to print in the log
 message why the when() expression is true. To achieve this I added a
 struct Rich(Type)
 which encapsulate the value and the reason for that value. Right now
 the implementation is not every smart and just stores the reason as a
 string.
This reminds me of assertPred :|
Jun 04 2011
next sibling parent reply Jose Armando Garcia <jsancio gmail.com> writes:
Is this an existing feature? a to be feature? or vaporware?

Grepping dmd, druntime, phobos yield no result.

On Sat, Jun 4, 2011 at 2:10 PM, KennyTM~ <kennytm gmail.com> wrote:
 On Jun 5, 11 01:04, Jose Armando Garcia wrote:
 Now on to rich boolean. I wanted to be able to print in the log
 message why the when() expression is true. To achieve this I added a
 struct Rich(Type)
 which encapsulate the value and the reason for that value. Right now
 the implementation is not every smart and just stores the reason as a
 string.
This reminds me of assertPred :|
Jun 04 2011
parent KennyTM~ <kennytm gmail.com> writes:
On Jun 5, 11 01:23, Jose Armando Garcia wrote:
 Is this an existing feature? a to be feature? or vaporware?

 Grepping dmd, druntime, phobos yield no result.

 On Sat, Jun 4, 2011 at 2:10 PM, KennyTM~<kennytm gmail.com>  wrote:
 On Jun 5, 11 01:04, Jose Armando Garcia wrote:
 Now on to rich boolean. I wanted to be able to print in the log
 message why the when() expression is true. To achieve this I added a
 struct Rich(Type)
 which encapsulate the value and the reason for that value. Right now
 the implementation is not every smart and just stores the reason as a
 string.
This reminds me of assertPred :|
A rejected proposal, e.g. see http://www.digitalmars.com/d/archives/digitalmars/D/std.unittests_updated_fo _review_127696.html and issue 5547.
Jun 04 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Isn't this a little better:

void main()
{
    foreach(i; 0 .. 10)
    {
        writeln(i);
        when(i == 9, log("wee"));
    }
}


bool log(string str)
{
    writeln(str);
    return true;
}

void when(bool delegate()[] dg...)
{
    if (dg[0]())
        dg[1]();
}
Jun 04 2011
prev sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Oops I forgot all about disabling this at compile time.
Jun 04 2011
prev sibling parent reply Mike Wey <mike-wey example.com> writes:
On 06/04/2011 07:04 PM, Jose Armando Garcia wrote:
 Hi everyone,

 I went back and incorporated a lot of the community requested changes.
 The module has changed quite a bit. Some notable changes are:

 * Got rid of all the *Config struct. You can now configure some parts
 of std.log even after logging as started. See Configuration
 * Got rid of the initLogging methods/templates they are not need
 anymore. The module should work without initialization.
 * Added the log severity critical which throws when logged.
 * Gave in and made LogFilter a final class
 * Fixed compile time disabling by using a template to get the
 LogFilter. The new syntax is log!info.
 * Added rich boolean support! More on that below.

 code: https://github.com/jsancio/phobos/blob/master/std/log.d
 doc: http://jsancio.github.com/phobos/phobos/std_log.html

 Note: I had to make changes to std.datetime (was getting a segfault;
 changes are in my repo) and druntime (patch attached).

 Now on to rich boolean. I wanted to be able to print in the log
 message why the when() expression is true. To achieve this I added a
 struct Rich(Type)
 which encapsulate the value and the reason for that value. Right now
 the implementation is not every smart and just stores the reason as a
 string.

 For example if you write:

 log!info.format("You passed %s argument(s)", args.length - 1);
 log!info.when(richGreater(args.length, 1))("Arguments: ", args[1 .. $]);
 log!info.when(richOr(richIsNull(obj), richEqual(10, 20)))("Something strange");

 you get the following log lines:

 20110604T152711.0138317Z:93d780 src/justo/main.d:11 INFO You passed 4
 argument(s)
 20110604T152711.0142393Z:93d780 src/justo/main.d:12 INFO when(true =
 (a>  b)<a = '5', b = '1'>) Arguments: [--minloglevel=info,
 --logtostderr, --stderrThreshold=info, --v=0]
 20110604T152711.0150535Z:93d780 src/justo/main.d:14 INFO when(true =
 (a || b)<a = 'true = (a is null)<a = 'null'>', b = 'false = (a == b)
 <a = '10', b = '20'>'>) Something strange!

 I am interested to hear people's option of this. How can it be improved?

 Many thanks,
 -Jose
It it possible to get a more Human readable date and time, i didn't see any options for it in the docs? -- Mike Wey
Jun 05 2011
parent Jose Armando Garcia <jsancio gmail.com> writes:
On Sun, Jun 5, 2011 at 11:37 AM, Mike Wey <mike-wey example.com> wrote:
 On 06/04/2011 07:04 PM, Jose Armando Garcia wrote:
 Hi everyone,

 I went back and incorporated a lot of the community requested changes.
 The module has changed quite a bit. Some notable changes are:

 * Got rid of all the *Config struct. You can now configure some parts
 of std.log even after logging as started. See Configuration
 * Got rid of the initLogging methods/templates they are not need
 anymore. The module should work without initialization.
 * Added the log severity critical which throws when logged.
 * Gave in and made LogFilter a final class
 * Fixed compile time disabling by using a template to get the
 LogFilter. The new syntax is log!info.
 * Added rich boolean support! More on that below.

 code: https://github.com/jsancio/phobos/blob/master/std/log.d
 doc: http://jsancio.github.com/phobos/phobos/std_log.html

 Note: I had to make changes to std.datetime (was getting a segfault;
 changes are in my repo) and druntime (patch attached).

 Now on to rich boolean. I wanted to be able to print in the log
 message why the when() expression is true. To achieve this I added a
 struct Rich(Type)
 which encapsulate the value and the reason for that value. Right now
 the implementation is not every smart and just stores the reason as a
 string.

 For example if you write:

 log!info.format("You passed %s argument(s)", args.length - 1);
 log!info.when(richGreater(args.length, 1))("Arguments: ", args[1 .. $]);
 log!info.when(richOr(richIsNull(obj), richEqual(10, 20)))("Something
 strange");

 you get the following log lines:

 20110604T152711.0138317Z:93d780 src/justo/main.d:11 INFO You passed 4
 argument(s)
 20110604T152711.0142393Z:93d780 src/justo/main.d:12 INFO when(true =3D
 (a> =A0b)<a =3D '5', b =3D '1'>) Arguments: [--minloglevel=3Dinfo,
 --logtostderr, --stderrThreshold=3Dinfo, --v=3D0]
 20110604T152711.0150535Z:93d780 src/justo/main.d:14 INFO when(true =3D
 (a || b)<a =3D 'true =3D (a is null)<a =3D 'null'>', b =3D 'false =3D (a=
=3D=3D b)
 <a =3D '10', b =3D '20'>'>) Something strange!

 I am interested to hear people's option of this. How can it be improved?

 Many thanks,
 -Jose
It it possible to get a more Human readable date and time, i didn't see a=
ny
 options for it in the docs?
At the moment no. I have a TODO for this. To allow the configuration of the log line and the log file names. The next version will have this!
 --
 Mike Wey
Jun 05 2011