How to write code for a Logging Event?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to write code for a Logging Event?
# 15  
Old 12-06-2011
Would you prefer unexplained crashes?

There's ways to reduce redundancy of course. It's useful that any failed system call, and quite a lot of things in stdio, set errno on error, so the same error routine can be used for many things:
Code:
// Opens a file, reads 'size' bytes from it, returns the data in new memory
void *read_data(const char *filename, int size)
{
        FILE *fp=fopen(filename, "r");
        void *mem=NULL;

        if(fp == NULL)
                goto PREPARE_ERROR;
        if((mem=malloc(size)) == NULL)
                goto PREPARE_ERROR;
        if(fread(mem, 1, size, fp) != size)
                goto PREPARE_ERROR;

        fclose(fp);
        return(mem);

PREPARE_ERROR:
        // fread, malloc, and fopen all set errno on error.
        perror("error in prepare_something:");

        if(mem) free(mem);
        if(fp)      fclose(fp);

        return(NULL);
}

...which just amounts to an extra jump-if instruction or so between function calls. The lion's share of work is done by functions or non-error code. And the benefit of error messages if your program actually does misbehave is enormous.

perror goes to standard error incidentally, so if you redirect FD #2 into a logfile, perror goes there.

Last edited by Corona688; 12-06-2011 at 02:36 PM..
This User Gave Thanks to Corona688 For This Post:
# 16  
Old 12-07-2011
Thanks for the Reply!

I would like to thank you again also for the help with emulation.

What is the technical term for writing the code that sources the log files like klogd and syslogd? I run my own code that oversees how my programs intentions panned out, then I use a logger command correct?

What system resources aide in determining if an error has occurred? Proc, Trace , OOP events and what else?


I expect to find a lot of intentional documents on this subject but it looks like everyone must just wing it.
# 17  
Old 12-07-2011
Quote:
Originally Posted by theKbStockpiler
What is the technical term for writing the code that sources the log files like klogd and syslogd?
You mean, writes the log files?

How about "error logging"?
Quote:
I run my own code that oversees how my programs intentions panned out, then I use a logger command correct?
Depends what you're trying to do.
Quote:
What system resources aide in determining if an error has occurred? Proc, Trace , OOP events and what else?
Generally none of the above. Unless something goes seriously wrong like a segfault, generally only your program knows if anything's going wrong.

An 'OOP event' in particular isn't a real thing. It's a programming method/buzzword. Object-oriented programs still deal with the same things.

In the end, checking for errors means checking if any data you receive makes sense, and checking if system calls you make succeed or not.
Quote:
I expect to find a lot of intentional documents on this subject but it looks like everyone must just wing it.
It sounds like you're reading papers describing what should be done but not how.

What, exactly, are you trying to do here? It sounds almost like you've been handed a list of substanceless buzzwords from on high and been told to obey them.

Last edited by Corona688; 12-07-2011 at 04:12 PM..
This User Gave Thanks to Corona688 For This Post:
# 18  
Old 12-07-2011
An understanding of this topic ties together other areas of computing that are otherwise nonsensical

If I could find a document on this subject I would read it but all I can do is try to poke at it and search terms on the Internet. A web search on "error logging" yields next to nothing btw.

If a search is done for "logging" all the information is from the "event code" the programmer writes (to) the separate logging application. Nothing further up the steam can be found in a structured document.

I have no doubt that a applications execution must be verified so it can keep on executing but I Can Not find any structured, predetermined approach to study the customary tools and methods used. I assume that there has to be a lot of O.S tools available or C library commands. The syslogd might be something to research because it is just really an application with oversight. I can't fathom that there is not entire books and coarses written on this subject so just brainstorming up methods to do this seems like reinventing the wheel. Augh! Experienced programmers Must have a procedure that they follow instead of doing it on the fly.

A book title of " How to write Code that produces the data held in Log files" would be the ticket.
# 19  
Old 12-07-2011
You're really overthinking this. It's like looking for a specification designed to print the letter 'A', and because there's no specifications specifically about printing the letter 'A', deciding that printing the letter 'A' is something very mysterious, complicated, and badly documented.

You can either write error messages to stderr, write them to a file, or use the system logger. Shell utilities usually use stderr. System daemons usually use files or the system logger. What an 'error' is, and what deserves logging, is up to you. syslog in particular lets you tell it how important the message is when you make it.

What's more useful to know is how other things return errors. See man errno, the global variable many things set on error. There's also a set of functions to use with it. Also see man 3 syslog for the "standard" way errors are recorded on a UNIX system -- it's not just a library call, there's log-related parts of the kernel it talks to.

Last edited by Corona688; 12-07-2011 at 05:45 PM..
This User Gave Thanks to Corona688 For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Linux

Syslog not logging successful logging while unlocking server's console

When unlocking a Linux server's console there's no event indicating successful logging Is there a way I can fix this ? I have the following in my rsyslog.conf auth.info /var/log/secure authpriv.info /var/log/secure (1 Reply)
Discussion started by: walterthered
1 Replies

2. Shell Programming and Scripting

Logging success event into file

Hi, I've the following code to log the errors any after the command is executed. # Ksh 88 Version log_path=/home/etc/fls/fls_log.log del_path=/home/etc/fls/to_day rm $del_path/* >> $log_path 2>&1 But I even want to log if the rm command is success without any error along with... (1 Reply)
Discussion started by: smile689
1 Replies

3. Shell Programming and Scripting

Event logging to file and display to console | tee command is not able to log all info.

My intention is to log the output to a file as well as it should be displayed on the console > I have used tee ( tee -a ${filename} ) command for this purpose. This is working as expected for first few outputs, after some event loggin nothing is gettting logged in to the file but It is displaying... (3 Replies)
Discussion started by: sanoop
3 Replies

4. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

5. UNIX for Dummies Questions & Answers

How does Unix write events into event log?

Hi to everyone. I'm new to this forum and also pretty new to Unix. I'm developing a cross-platform software and I need to register informational and error messages into the event log of the system. Under Windows, this is implemented so far, I need to implement it unded Unix by now. I do know that... (7 Replies)
Discussion started by: mariano_donati
7 Replies
Login or Register to Ask a Question