K
kj
I'd like to have this at the top level:
#include <stdio.h>
/* ... */
FILE *LOG = stdout; /* default output stream */
....but this does not work in general because stdout need not be a
constant. For example, with gcc 4.4.1 I get the error message:
foo.c:18: error: initializer element is not constant
The following top-level code also fails:
#include <stdio.h>
/* ... */
FILE *LOG;
LOG = stdout;
This time I get:
foo.c:19: warning: data definition has no type or storage class
....plus subsequent errors and warnings ensuing from this.
My aim here is to have a default global logging stream that can be
modified at runtime by some suitable set_log_stream function that
redefines the file-global variable LOG[1].
My first question is: how can I best achieve this?
More generally, this example illustrates a common problem: how to
set-up compilation-unit-wide default values that can only be known
at runtime.
One solution is to have an init function for the compilation unit;
this init function has to be called by main. I find this solution
very unsatisfying; I'd like such initialization should happen
automatically.
Another approach is to have another file-scoped variable, a boolean
flag that keeps track of whether the required initialization has
taken place. Then all the functions that depend on this initialization
first check the value of this flag, and call the init function if
it is false. The init function sets the flag to true before
returning.
This approach is at least automatic, but it adds a test to all the
functions, and, to boot, it is a test that will be true only once
during the code's execution.
Since this situation is pretty common, I'm hoping that there are
better ways to handle it. I look forward to reading your suggestions.
TIA!
~K
[1] Generally, I try to keep the scope of variables as small as
possible, which means that I avoid using file-scoped variables like
LOG above. But this code is part of a one-time research-oriented
data-analysis project; its expected shelf-life is about 4 weeks;
for this reason I'm not bothering with setting up a more sophisticated
logging scheme. Given the circumstances, I relax my rule against
file-scoped variables slightly, to allow this LOG variable.
#include <stdio.h>
/* ... */
FILE *LOG = stdout; /* default output stream */
....but this does not work in general because stdout need not be a
constant. For example, with gcc 4.4.1 I get the error message:
foo.c:18: error: initializer element is not constant
The following top-level code also fails:
#include <stdio.h>
/* ... */
FILE *LOG;
LOG = stdout;
This time I get:
foo.c:19: warning: data definition has no type or storage class
....plus subsequent errors and warnings ensuing from this.
My aim here is to have a default global logging stream that can be
modified at runtime by some suitable set_log_stream function that
redefines the file-global variable LOG[1].
My first question is: how can I best achieve this?
More generally, this example illustrates a common problem: how to
set-up compilation-unit-wide default values that can only be known
at runtime.
One solution is to have an init function for the compilation unit;
this init function has to be called by main. I find this solution
very unsatisfying; I'd like such initialization should happen
automatically.
Another approach is to have another file-scoped variable, a boolean
flag that keeps track of whether the required initialization has
taken place. Then all the functions that depend on this initialization
first check the value of this flag, and call the init function if
it is false. The init function sets the flag to true before
returning.
This approach is at least automatic, but it adds a test to all the
functions, and, to boot, it is a test that will be true only once
during the code's execution.
Since this situation is pretty common, I'm hoping that there are
better ways to handle it. I look forward to reading your suggestions.
TIA!
~K
[1] Generally, I try to keep the scope of variables as small as
possible, which means that I avoid using file-scoped variables like
LOG above. But this code is part of a one-time research-oriented
data-analysis project; its expected shelf-life is about 4 weeks;
for this reason I'm not bothering with setting up a more sophisticated
logging scheme. Given the circumstances, I relax my rule against
file-scoped variables slightly, to allow this LOG variable.