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(logStri ng,"%s", "Hello World, everything is OK");
myLogFunc(logSt ring);
....
}
*************** *****/
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 2228 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(logStri ng,"%s", "Hello World, everything is OK"); myLogFunc(logSt ring);
.... } *************** *****/
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(logFIL E, 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(logStri ng,"%s", "Hello World, everything is OK"); myLogFunc(logSt ring);
.... } *************** *****/
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("Hell o 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*********@su n.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(logStri ng,"%s", "Hello World, everything is OK"); myLogFunc(logSt ring);
.... } *************** *****/
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("Hell o 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*********@su n.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 dead stop.
I need to make a variable in function available outside the function, so
I used "global", however now the program won't run and gives me...
|
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
"Russian doll" style of schema design, where the nested type is
local
and anonymous (not a separate global type).
2. A complex type has nested...
|
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. I took some C (not C++) courses in college, this is
the only production C++ code I've worked with.
I'm trying to add some logging, and having...
|
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 strings, we would like to put the name
of the site, different contact e-mail etc. Would it be possible to make
these strings as global resources and...
|
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 who will find them very helpfull. gcc has
them as a non-standard option, but only when compiling C language code,
so I'm afraid there might be some...
| |
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 C# in some
way? Or maybe no, because it is similar to a global variable (with its
scope restricted) which C# is dead against?
Zytan
|
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 a function that is called thousands of times per second, it
seems to me that, performance-wise, it would be best to make all
variables used in it...
|
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: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
| |
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |