Go, CGO and Logging

533 views
Skip to first unread message

Francisco Lopes

unread,
Apr 9, 2015, 2:22:30 PM4/9/15
to golan...@googlegroups.com

Dear Gophers,

I was about to adopt “12-factor” and initially just use stdout for logging and
let rotation and redirection for the environment. But then I’m totally involved
with CGO, I have lots of goroutines and threads in the same physical process.

I even came to ask this on SO:

But then came to realize (with confirmation): Forget log synchronization, just
logically separate Go and C for logging.

It seems the least worse, but not optimal:

  • To get a single logging stream I’ll need to merge the two files. Not sure yet
    how it will proceed to tail it, or other tool for checking the two streams
    flowing at runtime when I need.
  • I’ll have to adopt two unrelated logging facilities, I’m about to adopt logxi
    and get pretty detailed output to console, as well as JSON, text, whatever
    when piping, but for C, I’ll adopt some other unrelated logging library
    (didn’t find none so far that follow logfmt!). No pretty console logging
    as far as I see.

Do you guys have experience on this topic to provide good advice and wisdom?

Yours truly,
Francisco Lopes

Francisco Lopes

unread,
Apr 9, 2015, 3:54:44 PM4/9/15
to golan...@googlegroups.com
Sorry, I meant that even though the library offers nice stdout logging, I'll have to dismiss
it by using two different streams for logging, most probably two logging files to be
managed/rotated outside.

Dustin

unread,
Apr 9, 2015, 9:09:35 PM4/9/15
to golan...@googlegroups.com
I can't really speak to what the "best practice" is, but I found the simplest solution for me (and has been working fine for my purposes) is just to export my Go logging function and call that (or a wrapper around it) from C.

Francisco Lopes

unread,
Apr 9, 2015, 11:54:14 PM4/9/15
to golan...@googlegroups.com


On Thursday, April 9, 2015 at 10:09:35 PM UTC-3, Dustin wrote:
I can't really speak to what the "best practice" is, but I found the simplest solution for me (and has been working fine for my purposes) is just to export my Go logging function and call that (or a wrapper around it) from C.

Hi Dustin, have you checked the SO question, I ended up entering into more details
about this option. What's a real concern to me is that, IF I'd do this, I dunno how
I would handle variadic functions as the ones exposed by the logging API's I'm interested.
I really want to stick with logfmt, but generally that means a variadic parameter interface
which I wouldn't have callback correspondence if I'm guessing right.

Dustin

unread,
Apr 10, 2015, 12:09:28 AM4/10/15
to golan...@googlegroups.com
I had the same issue, I got around it by writing a variadic function in C which runs C's sprintf on its args and then passes the single string result to my exported Go logging function.

Francisco Lopes

unread,
Apr 10, 2015, 12:58:44 AM4/10/15
to golan...@googlegroups.com


On Friday, April 10, 2015 at 1:09:28 AM UTC-3, Dustin wrote:
I had the same issue, I got around it by writing a variadic function in C which runs C's sprintf on its args and then passes the single string result to my exported Go logging function.

Ok but I guess this is reversed to my intention. It's the Go logging API that's variadic, and the one which I
won't have correspondence in C through a callback. The thing is, I'd like to make use of interface variadic
interface exposed by the Go library so that I get the benefit of adopting logfmt as well as clearly separated
key from values. This is available from Go's side. Using variadic from C side just to turn all of it in a single
string to get in Go-land, I miss passing the individual params to the Go logfmt facility. Only benefit I see
is that you may be trying to make the source code from C side to look like logfmt style.

Thanks

Dustin

unread,
Apr 10, 2015, 1:11:51 AM4/10/15
to golan...@googlegroups.com
Ah, I misunderstood. I'm not really familiar with logfmt and was thinking you were just trying to accomplish std logging.
However, might it be possible to serialize the C variadic args, pass them as a single string/value and deserialize on the Go side?

To help me better understand what you're trying to accomplish, which Go library are you using for logfmt?

Francisco Lopes

unread,
Apr 10, 2015, 1:50:49 AM4/10/15
to golan...@googlegroups.com


On Friday, April 10, 2015 at 2:11:51 AM UTC-3, Dustin wrote:
Ah, I misunderstood. I'm not really familiar with logfmt and was thinking you were just trying to accomplish std logging.
However, might it be possible to serialize the C variadic args, pass them as a single string/value and deserialize on the Go side?

To help me better understand what you're trying to accomplish, which Go library are you using for logfmt?

I guess I'll grab logxi or log15. I thought about the serialization/deserialization but I believe I would prefer to stick with
simpler separated log...

Dustin

unread,
Apr 10, 2015, 9:49:14 AM4/10/15
to golan...@googlegroups.com
One other option is too do something like what is done here: https://golang.org/src/syscall/dll_windows.go
Look at the Call function and Syscall# functions. Basically you would export a series of Go functions with increasing fixed # of args which get passed to your variadic Go func.
Just export up to the max # of args you will require in C (Syscall goes up to 15 which is probably more than enough).

Francisco Lopes

unread,
Apr 10, 2015, 10:21:33 AM4/10/15
to golan...@googlegroups.com
Thanks for mentioning it.
Reply all
Reply to author
Forward
0 new messages