Implementing std.log

Jacob Carlborg doob at me.com
Mon May 9 11:48:19 PDT 2011


On 2011-05-09 19:58, Andrei Alexandrescu wrote:
> 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)); }
> }

Not sure I understand this.

> * 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).

Ok.

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

I'm just following the function signature, it's declared to take an 
uint, like it or not: 
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/dyld.3.html

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

Ok, I'll see if I can find the time to create a pull request out of 
this. In what module should I put the function?

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list