samples/c/0/log.c
int log_error_open(server *srv) {
	int fd;
	int close_stderr = 1;
	
	if (srv->srvconf.error_logfile->used) {
		const char *logfile = srv->srvconf.error_logfile->ptr;
		
		if (-1 == (srv->log_error_fd = open(logfile, O_APPEND | O_WRONLY | O_CREAT | O_LARGEFILE, 0644))) {
			log_error_write(srv, __FILE__, __LINE__, "SSSS", 
					"opening errorlog '", logfile,
					"' failed: ", strerror(errno));
			
			return -1;
		}
#ifdef FD_CLOEXEC
		/* close fd on exec (cgi) */
		fcntl(srv->log_error_fd, F_SETFD, FD_CLOEXEC);
#endif
	} else {
		srv->log_error_fd = -1;
#ifdef HAVE_SYSLOG_H
		/* syslog-mode */
		srv->log_using_syslog = 1;
		openlog("lighttpd", LOG_CONS, LOG_LOCAL0);
#endif
	}
	
	log_error_write(srv, __FILE__, __LINE__, "s", "server started");
	
#ifdef HAVE_VALGRIND_VALGRIND_H
	/* don't close stderr for debugging purposes if run in valgrind */
	if (RUNNING_ON_VALGRIND) close_stderr = 0;
#endif

	/* move stderr to /dev/null */
	if (close_stderr &&
	    -1 != (fd = open("/dev/null", O_WRONLY))) {
		close(STDERR_FILENO);
		dup2(fd, STDERR_FILENO);
		close(fd);
	}
	return 0;
}
/** 
 * open the errorlog
 * 
 * if the open failed, report to the user and die
 * if no filename is given, use syslog instead
 * 
 */
open the errorlog

if the open failed, report to the user and die
if no filename is given, use syslog instead
open the errorlog if the open failed report to the user and die if no filename is given use syslog instead
int log_error_open(server *srv) {
	int fd;
	int close_stderr = 1;
	if (srv->srvconf.error_logfile->used) {
		const char *logfile = srv->srvconf.error_logfile->ptr;
		if (-1 == (srv->log_error_fd = open(logfile, O_APPEND | O_WRONLY | O_CREAT | O_LARGEFILE, 0644))) {
			log_error_write(srv, __FILE__, __LINE__, "SSSS", 
					"opening errorlog '", logfile,
					"' failed: ", strerror(errno));
			return -1;
		}
#ifdef FD_CLOEXEC
		fcntl(srv->log_error_fd, F_SETFD, FD_CLOEXEC);
#endif
	} else {
		srv->log_error_fd = -1;
#ifdef HAVE_SYSLOG_H
		srv->log_using_syslog = 1;
		openlog("lighttpd", LOG_CONS, LOG_LOCAL0);
#endif
	}
	log_error_write(srv, __FILE__, __LINE__, "s", "server started");
#ifdef HAVE_VALGRIND_VALGRIND_H
	if (RUNNING_ON_VALGRIND) close_stderr = 0;
#endif
	if (close_stderr &&
	    -1 != (fd = open("/dev/null", O_WRONLY))) {
		close(STDERR_FILENO);
		dup2(fd, STDERR_FILENO);
		close(fd);
	}
	return 0;
}
int log_error_open ( server * srv ) { int fd ; int close_stderr = 1 ; if ( srv -> srvconf . error_logfile -> used ) { const char * logfile = srv -> srvconf . error_logfile -> ptr ; if ( -1 == ( srv -> log_error_fd = open ( logfile , O_APPEND | O_WRONLY | O_CREAT | O_LARGEFILE , 0644 ) ) ) { log_error_write ( srv , __FILE__ , __LINE__ , " " , " " , logfile , " " , strerror ( errno ) ) ; return -1 ; } #ifdef FD_CLOEXEC fcntl ( srv -> log_error_fd , F_SETFD , FD_CLOEXEC ) ; #endif } else { srv -> log_error_fd = -1 ; #ifdef HAVE_SYSLOG_H srv -> log_using_syslog = 1 ; openlog ( " " , LOG_CONS , LOG_LOCAL0 ) ; #endif } log_error_write ( srv , __FILE__ , __LINE__ , " " , " " ) ; #ifdef HAVE_VALGRIND_VALGRIND_H if ( RUNNING_ON_VALGRIND ) close_stderr = 0 ; #endif if ( close_stderr && -1 != ( fd = open ( " " , O_WRONLY ) ) ) { close ( STDERR_FILENO ) ; dup2 ( fd , STDERR_FILENO ) ; close ( fd ) ; } return 0 ; }
open the errorlog
open the errorlog
/* close fd on exec (cgi) */ /* syslog-mode */ /* don't close stderr for debugging purposes if run in valgrind */ /* move stderr to /dev/null */