Write info to Log

This is a discussion on Write info to Log within the C Programming forums, part of the General Programming Boards category; Hello All, Im trying to write a function to write some information into a log file and possible some debuging ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    41

    Write info to Log

    Hello All,

    Im trying to write a function to write some information into a log file and possible some debuging info too.

    I have writen:

    Code:
    int WriteLog(char *msg, int deb)
    {
    
            FILE *logfile;
            char buff[80];
            
            logfile = fopen("logfile.txt","a");
    
            if(DEBUG) {
                    printf("[debug] "); printf("%s\n",msg);
                    sprintf(buff,"%s%s\n","[debug] ",msg);
                    fprintf(logfile,buff);
                    fprintf(logfile,"\n");
            }
            else {
                    if(deb == 1) {
                            printf("%s\n",msg);
                    }
                    fprintf(logfile,msg);
                    fprintf(logfile,"\n");
            }
    
            fclose(logfile);
    
            return 1;
    }
    main has this line I want to print to log:

    Code:
    main()
    {
    
    char bu[]="192.168.0.1";
    
    printf(">> database: ServerIP: (%s)\n",bu);
    //WriteLog(?????,1);
    
    }
    I wanted pass this to the WriteLog function but I realy dont want to use some thing like sprintf...

    As Im newbie in C programing, can any guru help me ? Is there a smart way of doing this ?


    Thanks,

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What part don't you understand? How to pass the array to the function?
    Code:
    void foo( char *bar )
    {
        ...do whatever...
    }
    
    int main( void )
    {
        char buf[] = "Hello World!";
    
        foo( buf );
    }
    Why exactly don't you want to use sprintf? Consider looking into the other file functions, such as fputs.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Hi,

    no realy. how could i pass to the function a text + a string that is in a varible like this one:
    Code:
    printf(">> database: ServerIP: %s",bu);
    I can not send this info to the function like this, can I ?
    Code:
    WriteLog(">> database: ServerIP: "bu,1);
    Ive asked abou sprintf becouse I know I could do somethin like:
    Code:
    sprintf(buff,">> database: ServerIP: %s",bu);
    WriteLog(buff,1);
    But I dont *think* this is the best way... for each log (that has vars) entry I will have to put in a buffer and send to the function ?

    Thanks,

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Declare writelog like this
    Code:
    int WriteLog(int deb,char *msg, ...);
    //then
    WriteLog(1,">> database: ServerIP: (%s)\n","192.168.0.1");
    Then manipulate a variable number of arguments, and internally make the sprintf, or print each piece at a time.
    some links (google ):
    http://www.opengroup.org/onlinepubs/.../stdarg.h.html
    http://www.acm.uiuc.edu/webmonkeys/b...uide/2.10.html

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If they're having trouble passing arguments to functions, working with a variable number of arguments is going to be a bit much for them.

    What exactly is your log function going to do? Is there a constant format you're going to be outputting? Is it all just a bunch of random stuff?

    I feel you're overcomplicating things. You should just get a basic logging function working, and then try and improve it or tweak it to do what you want.

    I would suggest doing the following:

    1) Make your log function take a string to print to the file.
    2) Build the string outside of the function, and then pass it to the logging function.

    Again, I don't understand why you don't want to use sprintf. It is by far the easiest way to do what you're trying to do:
    Code:
    char buf[BUFSIZ] = {0};
    ...other needed variables and what not...
    
    sprintf( buf, ">> database: %s, server IP: %s\n", databasename, serverip );
    WriteLog( buf );
    You're really just over complicating things. Functions should be designed, ideally, to do one task and to do that one task well. As such, your log function should simply take some information and log it.

    The simplest way to do that, is as I've described above. Build the info to log, then pass that info to the logging function.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Well Quzah, now I think you probably should be right.
    Probably what I need is to do a simple and "static" function and just build the strings and then pass it to the function...

    I just didnt know if there was a "smarter" way of doing this... probably sprintf is the key for my "problem" !

    If making this in a php function, for example, i should just do some thing like:

    $string = "database: $dbname - server ip: $ip";
    WriteLog($string);

    For shure C is much more complex than that... in my point of view I just could make some thing like it but i see now that i cant.

    Thanks alot !

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, as I've illustrated, that is basicly what you are doing:
    Code:
    char buf[BUFSIZ];
    sprintf( buf, "database: $s - server ip: %s", dbname, ip );
    WriteLog( buf );
    This assumes that you have two strings: dbname, and ip some place, which is pretty much what your php code is doing too. The difference here is that you create the variable on a seperate line from which you fill it in, and that you use format specifiers followed by the actual varaibles.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    I've had this function in my toolkit for some time and find it useful:
    Code:
    char *tprintf(char *fmt, ...)
    {
      static char buff[BUFSIZ];
      va_list ap;
    
      va_start(ap, fmt);
      vsnprintf(buff, sizeof(buff), fmt, ap);
      va_end(ap);
    
      return buff;
    }
    Older compilers might not have the vsnprintf() function; it's part of the C99 standard.

    It does formatted processing just like the other printf() family of functions, but it returns the formatted output instead of storing it in a buffer you pass to it...so you could do:
    Code:
    WriteLog(tprintf("This is a string: %s", string));
    Because tprintf() utilizes a static buffer for its return value you won't be able to call it more than once per statement.
    Last edited by itsme86; 12-09-2004 at 08:16 PM.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    41
    Well, thank you all for the replays... i've tryed to use tprintf() (it realy would be a great solution) but it is not supported, as I saw.

    I just solved using only one function to all logging.... and I pass the final string to the function after using sprintf;

    Code:
    int WriteLog(char *msg)
    {
            FILE *logfile;
            char buff[BUFSIZ];
    
            logfile = fopen("logfile.txt","a");
                    sprintf(buff,"[%s] - [%s]\n",date_default,msg);
                    fprintf(logfile,buff);
            fclose(logfile);
    
            return 1;
    }
    Code:
    sprintf(buf,"database: ServerIP: %s",ip); WriteLog(buf);
    Thanks alot !

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write a log file
    By kocika73 in forum Networking/Device Communication
    Replies: 4
    Last Post: 04-20-2006, 06:58 AM
  2. Some humour...
    By Stan100 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-06-2003, 09:25 PM
  3. log and restore
    By TopJo in forum C Programming
    Replies: 3
    Last Post: 07-01-2003, 07:14 PM
  4. write in c
    By PutoAmo in forum C Programming
    Replies: 6
    Last Post: 04-03-2002, 06:53 PM
  5. This should be the Cprogramming.com anthem!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 01-20-2002, 11:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21