2 * (c) Copyright 1992 by Panagiotis Tsirigotis
3 * (c) Sections Copyright 1998-2001 by Rob Braun
4 * All rights reserved. The file named COPYRIGHT specifies the terms
5 * and conditions for redistribution.
26 static const struct name_value priorities[] =
28 { "WARNING", LOG_WARNING },
30 { "CRITICAL", LOG_CRIT },
31 { "NOTICE", LOG_NOTICE },
32 { "DEBUG", LOG_DEBUG },
40 #define DEFAULT_SYSLOG_LEVEL LOG_INFO
42 const char *msg_init(void)
47 bool_int facility_error = FALSE ;
48 const char *func = "msg_init" ;
52 type_of_xlog = XLOG_FILELOG ;
53 xh = xlog_create( type_of_xlog, program_name, XLOG_NOFLAGS,
54 "/dev/tty", O_APPEND + O_WRONLY, 0 ) ;
61 type_of_xlog = XLOG_FILELOG ;
62 xh = xlog_create( type_of_xlog, program_name,
63 XLOG_PRINT_ID + XLOG_PRINT_PID,
64 filelog_option_arg, LOG_OPEN_FLAGS, LOG_FILE_MODE ) ;
68 int facility = DEFAULT_SYSLOG_FACILITY ;
72 const struct name_value *nvp ;
74 nvp = nv_find_value( syslog_facilities, syslog_option_arg ) ;
76 facility = nvp->value ;
78 facility_error = TRUE ;
81 type_of_xlog = XLOG_SYSLOG ;
82 xh = xlog_create( type_of_xlog, program_name, XLOG_NOFLAGS,
83 facility, DEFAULT_SYSLOG_LEVEL ) ;
90 * This simply returns the most likely reason for failure.
91 * We can't do any better since xlog_create does not return an
94 if ( type_of_xlog == XLOG_SYSLOG )
95 return( "can't connect to syslog" ) ;
96 else if ( type_of_xlog == XLOG_FILELOG )
97 return( "can't open log file" ) ;
99 return( "unknown reason" ) ;
103 * XXX: we shouldn't have to check the xlog type.
104 * xlog_control should not succeed if the undelying logging
105 * object does not support the XLOG_GETFD operation.
107 if ( type_of_xlog == XLOG_FILELOG &&
108 xlog_control( xh, XLOG_GETFD, &fd ) == XLOG_ENOERROR )
110 if ( fcntl( fd, F_SETFD, FD_CLOEXEC ) == -1 )
113 return( "can't set close-on-exec flag of log file" ) ;
119 ps.rws.program_log = xh ;
121 if ( facility_error )
122 msg( LOG_ERR, func, "Bad syslog facility: %s", syslog_option_arg ) ;
123 return( CHAR_NULL ) ;
127 void msg_suspend(void)
129 (void) xlog_control( ps.rws.program_log, XLOG_PREEXEC ) ;
133 void msg_resume(void)
135 (void) xlog_control( ps.rws.program_log, XLOG_POSTEXEC ) ;
140 * The size argument is a value-result argument
144 __attribute__ ((format (printf, 5, 0)))
146 prepare_buffer( int level,
154 char *bufstart = buf ;
155 unsigned bytes_left = size ;
158 * Check if we need to print the level name
160 if ( debug.on || filelog_option )
162 cc = strx_nprint( bufstart, bytes_left,
163 "%s: ", nv_get_name( priorities, level ) ) ;
169 * Check if we need to print the function name
171 if ( debug.on || level == LOG_CRIT )
173 cc = strx_nprint( bufstart, bytes_left, "%d {%s} ", getpid(), func ) ;
178 cc = strx_nprintv( bufstart, bytes_left, fmt, ap ) ;
182 return( size - bytes_left ) ;
187 void msg( int level, const char *func, const char *fmt, ...)
190 char buf[ BUFSIZE ] ;
193 va_start( ap, fmt ) ;
194 len = prepare_buffer( level, func, buf, sizeof( buf ), fmt, ap ) ;
197 xlog_write( ps.rws.program_log, buf, len, XLOG_SET_LEVEL, level ) ;
203 * There are 2 differences from msg():
204 * 1) parsemsg() prints the line #
205 * 2) parsemsg() does not interpret %m
208 void parsemsg( int msg_level, const char *func, const char *fmt, ...)
211 char buf[ BUFSIZE ] ;
215 va_start( ap, fmt ) ;
216 len = prepare_buffer( msg_level, func, buf, sizeof( buf ), fmt, ap ) ;
219 cc = strx_nprint( &buf[ len ], sizeof(buf)-len,
220 " [file=%s] [line=%d]", current_file, line_count ) ;
223 xlog_write( ps.rws.program_log, buf, len,
224 XLOG_NO_ERRNO + XLOG_SET_LEVEL, msg_level ) ;