I have about twenty functions written in C.
Each time I call a function, it logs the status of the function
operation to a file. I use a central logging function which is called
from each function to report status into a log file. However, I am
using "sprintf" and formatting the log info into a string and sending
to my central logging routine myLogFunc().
Here is the skeleton code:
/*******************
void myFunc1()
{
char logString[100];
/* do some thing here.. */
....
....
sprintf(logString,"%s", "Hello World, everything is OK");
myLogFunc(logString);
....
}
********************/
Now question is that:
1) Should I declare the char array logString[] in every function as a
local variable ? or should I declare as a static global. I know that,
this array is local to each function and it would be disappear once the
function returns.
2) Could you please advice which one is better, local or global ? 5 2223 ak**************@gmail.com wrote On 03/10/06 11:16,: I have about twenty functions written in C. Each time I call a function, it logs the status of the function operation to a file. I use a central logging function which is called from each function to report status into a log file. However, I am using "sprintf" and formatting the log info into a string and sending to my central logging routine myLogFunc().
Here is the skeleton code:
/******************* void myFunc1() { char logString[100];
/* do some thing here.. */ .... ....
sprintf(logString,"%s", "Hello World, everything is OK"); myLogFunc(logString);
.... } ********************/
Now question is that: 1) Should I declare the char array logString[] in every function as a local variable ? or should I declare as a static global. I know that, this array is local to each function and it would be disappear once the function returns. 2) Could you please advice which one is better, local or global ?
Better than either, probably, would be to have
myLogFunc() handle the whole business:
void myFunc1() {
...
myLogFunc("%s", "Hello World, all's well");
...
}
void myLogFunc(const char *fmt, ...) {
va_arg ap;
fputs ("LOG ENTRY: ", logFILE);
va_start(ap, fmt);
vfprintf(logFILE, fmt, ap);
va_end(ap);
fputs (" (END OF LOG ENTRY)\n", logFILE);
}
-- Er*********@sun.com ak**************@gmail.com wrote: I have about twenty functions written in C. Each time I call a function, it logs the status of the function operation to a file. I use a central logging function which is called from each function to report status into a log file. However, I am using "sprintf" and formatting the log info into a string and sending to my central logging routine myLogFunc().
Here is the skeleton code:
/******************* void myFunc1() { char logString[100];
/* do some thing here.. */ .... ....
sprintf(logString,"%s", "Hello World, everything is OK"); myLogFunc(logString);
.... } ********************/
Now question is that: 1) Should I declare the char array logString[] in every function as a local variable ? or should I declare as a static global. I know that, this array is local to each function and it would be disappear once the function returns. 2) Could you please advice which one is better, local or global ?
Why not define myLogFunc() as void myLogFunc(const char *) and then simply
call it like this:
myLogFunc("Hello World, everything is OK");
Then ...
void myLogFunc(const char * message)
{
FILE * file;
file = fopen(...);
fputs(message, file);
fclose(file);
}
Then you've got no need to use sprintf.
If you want to pass a message with more detail, you could easily create a
variadic version of myLogFunc(), and do the only sprintf stuff in there
using something like vsnprintf.
--
==============
*Not a pedant*
==============
Eric Sosman <Er*********@sun.com> writes: void myFunc1() { ... myLogFunc("%s", "Hello World, all's well");
I realize that the OP used %s gratuitously, but I don't think
there's any reason to carry it through on the follow-up.
... }
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
pemo wrote: ak**************@gmail.com wrote: I have about twenty functions written in C. Each time I call a function, it logs the status of the function operation to a file. I use a central logging function which is called from each function to report status into a log file. However, I am using "sprintf" and formatting the log info into a string and sending to my central logging routine myLogFunc().
Here is the skeleton code:
/******************* void myFunc1() { char logString[100];
/* do some thing here.. */ .... ....
sprintf(logString,"%s", "Hello World, everything is OK"); myLogFunc(logString);
.... } ********************/
Now question is that: 1) Should I declare the char array logString[] in every function as a local variable ? or should I declare as a static global. I know that, this array is local to each function and it would be disappear once the function returns. 2) Could you please advice which one is better, local or global ?
Why not define myLogFunc() as void myLogFunc(const char *) and then simply call it like this:
myLogFunc("Hello World, everything is OK");
Then ...
void myLogFunc(const char * message) { FILE * file;
file = fopen(...);
fputs(message, file);
fclose(file); }
Then you've got no need to use sprintf.
If you want to pass a message with more detail, you could easily create a variadic version of myLogFunc(), and do the only sprintf stuff in there using something like vsnprintf.
Maybe something like this - *disclaimer*: not very well tested or thought
about!!!
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <stdarg.h>
void myLogFunc(const char * format, ...)
{
// size = guess how much we need.
//
int size = 10;
int n = 0;
char * p = NULL;
va_list va;
if((p = malloc(size)) != NULL)
{
for(;;)
{
// Try to print into what we've got.
//
va_start(va, format);
n = vsnprintf(p, size, format, va);
va_end(va);
// If it worked, god - write out the string.
//
if (n > -1 && n < size)
{
FILE * file = NULL;
if((file = fopen("text.txt", "a")) != NULL)
{
fputs(p, file);
fclose(file);
}
else
{
fprintf(stderr, "Couldn't write '%s' to the log file\n",
p);
}
free(p);
}
// Try again with more space.
//
else
{
if((p = realloc(p, ++size)) == NULL)
{
abort();
}
}
return;
}
}
else
{
// What else can we do!
//
abort();
}
}
int main(void)
{
myLogFunc("%d %s\n", 10, "Boo hoo hoo");
return 0;
}
--
==============
*Not a pedant*
==============
Ben Pfaff wrote On 03/10/06 12:23,: Eric Sosman <Er*********@sun.com> writes:
void myFunc1() { ... myLogFunc("%s", "Hello World, all's well");
I realize that the OP used %s gratuitously, but I don't think there's any reason to carry it through on the follow-up.
It's silly as used here, yes. But I kept it because
I wanted to show a myLogFunction() that could actually do
formatting, rather than just handle an already-formatted
single string.
-- Er*********@sun.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kyle Root |
last post by:
I'm writing a little program that compares two versions and tells you
whether you are upgrading, downgrading, or it's the same version.
Unfortunately, I haven't gotten very far, in fact I'm at a...
|
by: mjcsfo |
last post by:
I can't seem to find a reference nor any helpful threads on this
topic.
I've gotten the following error in two circumstances:
1. A complex type has nested within it another complex type, in the...
|
by: daniel.bron |
last post by:
I'm maintaining a C++ application written by a developer who has now
left. The app is a multithreaded client/server app for financial data.
My compiler is MS visual C++ 6.0.
I'm a C++ neophyte....
|
by: Peter Strřiman |
last post by:
Hi.
I have a web project, that is intended to be installed multiple times as
different product with different names. All strings in the web are placed in
local resource files. In some of the...
|
by: Timothy Madden |
last post by:
Hello all.
I program C++ since a lot of time now and I still don't know this simple
thing: what's the problem with local functions so they are not part of
C++ ?
There surely are many people...
| |
by: Shilpa |
last post by:
Hi,
I just wanted to know whether we can access global variable within a
local block , where both variables are having same name.
For ex:
int temp=5 ;
{
int temp=10;
|
by: Zytan |
last post by:
I see that static is more restricted in C# than in C++. It appears
usable only on classes and methods, and data members, but cannot be
created within a method itself. Surely this is possible in...
|
by: Sullivan WxPyQtKinter |
last post by:
I am confused by the following program:
def f():
print x
x=12345
f()
result is:
12345
|
by: danep2 |
last post by:
Let me start by saying that this is more a question about principle
than practice - with the speed of today's computers it's probably
rarely an actual issue. Still I'd like to know...
If I have...
|
by: Erwin Moller |
last post by:
Hi group,
Consider this simple script (tested on FF3):
<script type="text/javascript">
test = 'outer';
for (var i=0;i<2;i++){
alert(test);
var test = 'inner';
alert (test);
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |