Skip to content

Commit

Permalink
[console] Add "log message" console usage and an internal syslog() call
Browse files Browse the repository at this point in the history
Provide an internal syslog() function (unrelated to the syslog
console) which can be used to create log messages with specified
priorities.

The build-time constant LOG_LEVEL can be used to select the minimum
required priority for log messages.  Any messages that do not have a
sufficient priority will be ignored (and will be optimised away at
compile-time).

The default LOG_LEVEL is LOG_NONE.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Mar 26, 2012
1 parent 64d17db commit 24b7296
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/config/console.h
Expand Up @@ -23,6 +23,8 @@ FILE_LICENCE ( GPL2_OR_LATER );

#define KEYBOARD_MAP us

#define LOG_LEVEL LOG_NONE

#include <config/local/console.h>

#endif /* CONFIG_CONSOLE_H */
62 changes: 62 additions & 0 deletions src/core/log.c
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

FILE_LICENCE ( GPL2_OR_LATER );

/** @file
*
* System logger
*
*/

#include <stdarg.h>
#include <syslog.h>
#include <ipxe/console.h>

/**
* Write message to system log
*
* @v fmt Format string
* @v args Arguments
*/
void log_vprintf ( const char *fmt, va_list args ) {
int saved_usage;

/* Mark console as in use for log messages */
saved_usage = console_set_usage ( CONSOLE_USAGE_LOG );

/* Print message */
vprintf ( fmt, args );

/* Restore console usage */
console_set_usage ( saved_usage );
}

/**
* Write message to system log
*
* @v fmt Format string
* @v ... Arguments
*/
void log_printf ( const char *fmt, ... ) {
va_list args;

va_start ( args, fmt );
log_vprintf ( fmt, args );
va_end ( args );
}
7 changes: 5 additions & 2 deletions src/include/ipxe/console.h
Expand Up @@ -120,9 +120,12 @@ struct console_driver {
/** Text-based user interface */
#define CONSOLE_USAGE_TUI 0x0004

/** Log messages */
#define CONSOLE_USAGE_LOG 0x0008

/** All console usages */
#define CONSOLE_USAGE_ALL \
( CONSOLE_USAGE_STDOUT | CONSOLE_USAGE_DEBUG | CONSOLE_USAGE_TUI )
#define CONSOLE_USAGE_ALL ( CONSOLE_USAGE_STDOUT | CONSOLE_USAGE_DEBUG | \
CONSOLE_USAGE_TUI | CONSOLE_USAGE_LOG )

/** @} */

Expand Down
4 changes: 3 additions & 1 deletion src/include/ipxe/syslog.h
Expand Up @@ -9,6 +9,8 @@

FILE_LICENCE ( GPL2_OR_LATER );

#include <syslog.h>

/** Syslog server port */
#define SYSLOG_PORT 514

Expand All @@ -28,7 +30,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
*
* This is a policy decision
*/
#define SYSLOG_SEVERITY 6 /* informational */
#define SYSLOG_SEVERITY LOG_INFO

/** Syslog priority */
#define SYSLOG_PRIORITY( facility, severity ) ( 8 * (facility) + (severity) )
Expand Down
84 changes: 84 additions & 0 deletions src/include/syslog.h
@@ -0,0 +1,84 @@
#ifndef _SYSLOG_H
#define _SYSLOG_H

/** @file
*
* System logger
*
*/

FILE_LICENCE ( GPL2_OR_LATER );

#include <stdarg.h>
#include <config/console.h>

/**
* @defgroup syslogpri Syslog priorities
*
* These values are chosen to match those used in the syslog network
* protocol (RFC 5424).
*
* @{
*/

/** Emergency: system is unusable */
#define LOG_EMERG 0

/** Alert: action must be taken immediately */
#define LOG_ALERT 1

/** Critical: critical conditions */
#define LOG_CRIT 2

/** Error: error conditions */
#define LOG_ERR 3

/** Warning: warning conditions */
#define LOG_WARNING 4

/** Notice: normal but significant conditions */
#define LOG_NOTICE 5

/** Informational: informational messages */
#define LOG_INFO 6

/** Debug: debug-level messages */
#define LOG_DEBUG 7

/** @} */

/** Do not log any messages */
#define LOG_NONE -1

extern void log_vprintf ( const char *fmt, va_list args );

extern void __attribute__ (( format ( printf, 1, 2 ) ))
log_printf ( const char *fmt, ... );

/**
* Write message to system log
*
* @v priority Message priority
* @v fmt Format string
* @v ... Arguments
*/
#define vsyslog( priority, fmt, args ) do { \
if ( (priority) <= LOG_LEVEL ) { \
log_vprintf ( fmt, (args) ); \
} \
} while ( 0 )

/**
* Write message to system log
*
* @v priority Message priority
* @v fmt Format string
* @v ... Arguments
*/
#define syslog( priority, fmt, ... ) do { \
if ( (priority) <= LOG_LEVEL ) { \
log_printf ( fmt, ##__VA_ARGS__ ); \
} \
} while ( 0 )

#endif /* _SYSLOG_H */

0 comments on commit 24b7296

Please sign in to comment.