Implementing std.log

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Mon May 9 10:58:09 PDT 2011


On 5/9/11 12:24 PM, Jacob Carlborg wrote:
> I
> have a function that gets the path of the current process:
>
> http://dsource.org/projects/tango/attachment/ticket/1536/process.d
>
> This links to a file attached to a Tango ticket but don't worry, I've
> written the whole file myself and I intend to change the license to the
> Boost License. It's written with D1 (obviously) but it's very easy to
> port to D2, the only dependencies are C functions.
>
> Feel free to port it to D2 and use it in you're logging library if you
> want to.

I looked over the code, it's quite nice and clean. If you or anyone else 
would want to create a github pull request, the function would be a 
valuable addition to std.process. Thanks Jacob for offering the code to 
Phobos!

What I think should change:

* Provide two overloads, one that attempts to reuse a buffer and one 
that always returns a new string:

char[] getProcessPath(char[] buf) { ... }
static if (is(typeof(getProcessPath(null)) == char[]))
{
   string getProcessPath() { return assumeUnique(getProcessPath(null)); }
}

* hoist version(OS) up to the function definition, e.g.

version (darwin) char[] getProcessPath (char[] buf) { ... }
version (freebsd) char[] getProcessPath (char[] buf) { ... }
...

Unsupported OSs will simply not define the function (as opposed to 
asserting at run time).

* Use size_t instead of uint throughout for 64-bit compatibility.

* Don't do this:

if (size > buf.length)
   buf ~= new char[size - buf.length];

because it allocates memory twice. Instead:

if (size > buf.length)
   buf.length = size;

which will allocate memory once or not at all.

* Check for all system calls for errors.



Again, thanks Jacob for your contribution!

Andrei


More information about the Digitalmars-d mailing list