www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.log version 2

reply Jose Armando Garcia <jsancio gmail.com> writes:
--bcaec51d2d7a5d79de04a4e5dbfe
Content-Type: text/plain; charset=ISO-8859-1

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

--bcaec51d2d7a5d79de04a4e5dbfe
Content-Type: application/octet-stream; name="druntime.patch"
Content-Disposition: attachment; filename="druntime.patch"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_goit97jf0

ZGlmZiAtLWdpdCBhL3NyYy9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gIGFsaWFzIHB0aHJlYWRfdCAgICAgVGhyZWFk
QWRkcjsKICAgICB9CiAKIAo=
--bcaec51d2d7a5d79de04a4e5dbfe--
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
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 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
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 next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Oops I forgot all about disabling this at compile time.
Jun 04 2011
prev sibling next sibling parent 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
prev sibling 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=


 <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=

 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