LINUX TIPS AND TRICKS --- November 10, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

------------------------------------------------------------------------

Syslog Continued
By Danny Kalev

This week I will present the rest of the functions declared 
in "sys/syslog.h", namely openlog(), closelog(), and setlogmask():

--OPENLOG
Although syslog() is sufficient for basic message logging, for more 
advanced uses you may need to call openlog() before syslog(). openlog() 
sets process attributes that affect subsequent calls to syslog(). Here 
is the prototype of this function:

  void openlog(const char *str, int options, int facility); 

The first argument is a string that is prepended to every message. The 
second argument describes various logging options that are indicated by 
combining zero or more of the following constants using the bitwise OR 
operator:

  LOG_PID 
Log the process ID with each message. This is useful for distinguishing 
between daemon processes that have forked. 

  LOG_CONS 
Direct messages to the system console if they cannot be sent to 
syslogd. 

  LOG_NDELAY 
Open the connection to syslogd immediately, without waiting for the 
first message to be logged.

  LOG_ODELAY 
Delay open until the first call to syslog(). 

  LOG_NOWAIT 
Do not wait for child processes that have been forked to log messages 
onto the console. 

The third argument of openlog() specifies a default facility to be 
assigned to all messages that do not include an explicit facility code. 
The following openlog() call ...

  openlog("mailer", LOG_PID, LOG_MAIL);

 ... prepends the string "mailer" to every log message, ensures that 
the message contains the process's ID, and -- if no explicit facility 
code is provided in the syslog() call -- it ensures that the message 
bears the LOG_MAIL facility code.

--SETLOGMASK
The function setlogmask() has the following prototype:

  int setlogmask(int maskpri); 

It sets the log priority mask for the current process to maskpri and 
returns the previous mask. If the maskpri argument is 0, the current 
log mask isn't changed. Any calls from the current process to syslog() 
with a priority code not set in maskpri are rejected. Use the macro 
LOG_MASK(p) to calculate the mask for an individual priority p. To 
obtain the mask for all priorities up to and including p_x, use the 
macro LOG_UPT(p_x). The default log mask allows all priorities to be 
logged. The following setlogmask() call ...

  setlogmask(LOG_UPTO(LOG_ERR));

 ... ensures that only messages whose priority is LOG_ERR and above, 
e.g., LOG_CRIT and LOG_EMERG, are logged; messages with lower 
priorities such as LOG_DEBUG, are rejected. 

--CLOSELOG
Finally, the function closelog() closes any open file descriptors 
allocated by previous calls to openlog() or syslog().

About the author(s)
----------------
Danny Kalev is a system analyst and software engineer with more than 10 
years of experience, specializing in C++ and object-oriented analysis 
and design on various platforms including VMS, DOS, Windows, Unix, and 
Linux. His technical interests involve code optimization, networking, 
and distributed computing. He is also a member of the ANSI C++ 
standardization committee and the author of ANSI/ISO C++ Professional 
Programmer's Handbook (Que, 1999). Contact him at linuxnl@excite.com. 

------------------------------------------------------------------------

ADDITIONAL RESOURCES

Small fry Unix commands can get the job done 
A look at the tail and date utilities

http://www.itworld.com/jsw/lintps_nl/swol-05-1998/swol-05-unix101.html

Testing, testing 1, 2, 3 
Learn how to effectively and efficiently test your applications

http://www.itworld.com/jsw/lintps_nl/swol-02-1999/
swol-02-itarchitect_p.html

Protecting your network with firewalls, featuring Sun's SunScreen EFS 
firewall

http://www.itworld.com/jsw/lintps_nl/swol-01-1998/swol-01-efs_p.html

------------------------------------------------------------------------

COMMUNITY DISCUSSION

Hone your Linux development skills, share your expertise, and put out 
the occasional call for help in this discussion for programmers of all 
levels. Moderated by Danny Kalev.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b652/175!skip=111

Ask questions, offer solutions, and tell your tales in this lively 
discussion of the good, bad, and ugly sides of managing Unix systems. 
Moderated Sandra Henry-Stocker.

http://www.itworld.com/jump/lintps_nl/forums.itworld.com/
webx?14@@.ee6b677/266!skip=215

------------------------------------------------------------------------

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
  Newsletters at: andrew_santosusso@itworld.com
* For advertising information, write Dan Chupka, Account Executive at:
  dan_chupka@itworld.com
* For recruitment advertising information, write Jamie Swartz, Eastern
  Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
  Western Regional Sales Manager at: paul_duthie@itworld.com
* For all other inquiries, write Jodie Naze, Product Manager, 
  Newsletters at: jodie_naze@itworld.com

------------------------------------------------------------------------

PRIVACY POLICY
http://www2.itworld.com/CDA/ITW_Privacy_Policy 

Copyright 2000 ITworld.com, Inc., All Rights Reserved.

http://www.itworld.com
